On 12/11/2025 04:10, Collin Funk wrote:
* tests/test/test-file.sh: Check that -nt and -ot work properly when
files may or may not exist.
---
  tests/test/test-file.sh | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)

diff --git a/tests/test/test-file.sh b/tests/test/test-file.sh
index 0bc917348..469f5ca9b 100755
--- a/tests/test/test-file.sh
+++ b/tests/test/test-file.sh
@@ -37,4 +37,25 @@ returns_ 1 env test -c file || fail=1
  returns_ 1 env test -b file || fail=1
  returns_ 1 env test -p file || fail=1
+# Check that 'test' returns true if the file name following "-nt" does not
+# resolve.
+env test file -nt missing || fail=1
+returns_ 1 env test missing -nt file || fail=1
+
+# Check that 'test' returns true if the file name preceding "-ot" does not
+# resolve.
+env test missing -ot file || fail=1
+returns_ 1 env test file -ot missing || fail=1
+
+# At this point the "newfile" should have a later timestamp than "file".
+touch newfile || framework_failure_
+
+# Test "-nt" on two existing files.
+env test newfile -nt file || fail=1
+returns_ 1 env test file -nt newfile || fail=1
+
+# Test "-ot" on two existing files.
+env test file -ot newfile || fail=1
+returns_ 1 env test newfile -ot file || fail=1
+
  Exit $fail

Right. Non existent files are considered infinitely old.

I see I recently generated this test-file.sh based on a coverage report,
however I missed the fact that the -nt and -ot options were also not covered,
which I checked just now. I also notice that the -ef option is not covered
either, so we should add a case for that also.

As for the test implementation above, I don't think we can rely on
"newfile" being newer, as it's dependent on file system time resolution.
I think we'd have to explicitly set the dates as is done in
tests/date/reference.sh for example.

thanks,
Padraig

Reply via email to