Pádraig Brady <[email protected]> writes:

> 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.

Yep, I figured -ef was best to leave for another patch. I'll write that
one in a bit.

> 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.

Right, we can just create files with different timestamps...

I was thinking of adding a sleep since I recall Autoconf's test suite
trying to figure out what resolution the file system supports and then
using that. But Autoconf probably has to assume the system has an
inadequate 'touch' command.

I pushed the attatched patch instead. Thanks for the review.

Collin

>From e1bd591bc29e9f89e2f444a838d50eceedfe72ba Mon Sep 17 00:00:00 2001
Message-ID: <e1bd591bc29e9f89e2f444a838d50eceedfe72ba.1762973703.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 11 Nov 2025 20:07:52 -0800
Subject: [PATCH v2] tests: test: add test cases for -nt and -ot

* tests/test/test-file.sh: Check that -nt and -ot work properly when
files may or may not exist.
---
 tests/test/test-file.sh | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/tests/test/test-file.sh b/tests/test/test-file.sh
index 0bc917348..d5fb44967 100755
--- a/tests/test/test-file.sh
+++ b/tests/test/test-file.sh
@@ -37,4 +37,28 @@ 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
+
+# Create two files with modification times an hour apart.
+t1='2025-10-23 03:00'
+t2='2025-10-23 04:00'
+touch -m -d "$t1" file1 || framework_failure_
+touch -m -d "$t2" file2 || framework_failure_
+
+# Test "-nt" on two existing files.
+env test file2 -nt file1 || fail=1
+returns_ 1 env test file1 -nt file2 || fail=1
+
+# Test "-ot" on two existing files.
+env test file1 -ot file2 || fail=1
+returns_ 1 env test file2 -ot file1 || fail=1
+
 Exit $fail
-- 
2.51.1

Reply via email to