* tests/misc/empty-backup-suffix.sh: New file.
* tests/local.mk (all_tests): Add the new test.
---
 tests/local.mk                    |  1 +
 tests/misc/empty-backup-suffix.sh | 92 +++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100755 tests/misc/empty-backup-suffix.sh

diff --git a/tests/local.mk b/tests/local.mk
index b256e79d0..5be625f8a 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -262,6 +262,7 @@ all_tests =                                 \
   tests/fmt/non-space.sh                       \
   tests/fmt/width.sh                           \
   tests/misc/echo.sh                           \
+  tests/misc/empty-backup-suffix.sh            \
   tests/env/env.sh                             \
   tests/env/env-signal-handler.sh              \
   tests/ptx/ptx.pl                             \
diff --git a/tests/misc/empty-backup-suffix.sh 
b/tests/misc/empty-backup-suffix.sh
new file mode 100755
index 000000000..281cd784b
--- /dev/null
+++ b/tests/misc/empty-backup-suffix.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+# Test that 'cp', 'mv', and 'install' don't have data loss with an empty
+# backup suffix.
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ cp ginstall mv
+
+# Setup the files we expect to see.
+echo a > exp-a || framework_failure_
+echo b > exp-b || framework_failure_
+
+# Remove any previously created backups, then create the source
+# and target files.
+reset_files ()
+{
+  rm -f b~ b.~1~ b.~2~ && echo a > a && echo b > b || fail=1
+}
+
+# Check that we don't have data loss with an empty backup suffix.
+for prog in cp ginstall mv; do
+  for control in t nil never; do
+    for backup_opt in VERSION_CONTROL --backup; do
+      case $backup_opt in
+        -*) backup_env=; backup_opt=$backup_opt=$control ;;
+        *) backup_env=$backup_opt=$control; backup_opt=--backup ;;
+      esac
+      for suffix_opt in SIMPLE_BACKUP_SUFFIX --suffix; do
+        case $suffix_opt in
+          -*) suffix_env=; suffix_opt=$suffix_opt= ;;
+          *) suffix_env=$suffix_opt=; suffix_opt= ;;
+        esac
+        reset_files
+        env $backup_env $suffix_env $prog $suffix_opt $backup_opt a b \
+            >out >err || fail=1
+        compare /dev/null out || fail=1
+        compare /dev/null err || fail=1
+        # Check that the source exists, unless we are using 'mv'.
+        if test -f a; then
+          test $prog != mv || fail=1
+          compare exp-a a || fail=1
+        else
+          test $prog = mv || fail=1
+        fi
+        # Check that the backup exists with the correct content.
+        if test $control = t; then
+          suffix='.~1~';
+        else
+          suffix='~'
+        fi
+        compare exp-b b$suffix || fail=1
+        # Check that the target exists with the correct content.
+        compare exp-a b || fail=1
+        if test $control = nil; then
+          echo b > a && mv b~ b.~1~ || framework_failure_
+          env $backup_env $suffix_env $prog $suffix_opt $backup_opt a b \
+            >out >err || fail=1
+          compare /dev/null out || fail=1
+          compare /dev/null err || fail=1
+          # Check that the source exists, unless we are using 'mv'.
+          if test -f a; then
+            test $prog != mv || fail=1
+            compare exp-b a || fail=1
+          else
+            test $prog = mv || fail=1
+          fi
+          # Check that the backups exist with the correct content.
+          compare exp-b b.~1~ || fail=1
+          compare exp-a b.~2~ || fail=1
+          # Check that the target exists with the correct content.
+          compare exp-b b || fail=1
+        fi
+      done
+    done
+  done
+done
+
+Exit $fail
-- 
2.55.0


Reply via email to