From f6ea1229abf0cd075b5d88182f31cdd21f18c710 Mon Sep 17 00:00:00 2001
From: zhoushuiqing <zhoushuiqing321@outlook.com>
Date: Thu, 23 Jun 2022 15:17:23 +0800
Subject: [PATCH] tests: add tests for the 'find -newer' family

* tests/find/newer.sh: Add to improve test coverage.
* tests/local.mk (all_tests): Reference the test.
---
 tests/find/newer.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++
 tests/local.mk      |  1 +
 2 files changed, 66 insertions(+)
 create mode 100755 tests/find/newer.sh

diff --git a/tests/find/newer.sh b/tests/find/newer.sh
new file mode 100755
index 00000000..1ae6b5b3
--- /dev/null
+++ b/tests/find/newer.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+# Exercise -anewer -cnewer -newer -newerXY.
+
+# Copyright (C) 2022 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"; fu_path_prepend_
+print_ver_ find
+
+# Verify the -newer test family - omitting birth time as support for this
+# is not common enough yet.
+
+# Although 'touch -t' is standardized by POSIX, that would not set the ctime
+# to the given timestamp.  Therefore, we have to use sleep(1) to prepare the
+# test files 1..3.
+touch file1 \
+  && sleep 1 \
+  && touch file2 \
+  && sleep 1 \
+  && touch file3 \
+  || skip_ "creating files with different timestamp failed"
+
+# Stat the test files for debug purposes.
+stat file? || : ignored
+
+echo "./file3" > exp || framework_failure_
+
+# Exercise -new options using a reference file.
+for x in \
+  -anewer -cnewer -newer \
+  -neweraa -newerac -neweram \
+  -newerca -newercc -newercm \
+  -newerma -newermc -newermm \
+  ; do
+  rm -f out || framework_failure
+  find . $x file2 -name 'file*' > out || fail=1
+  compare exp out || fail=1
+done
+
+# Exercise -new options using a reference timestamp (if possible).
+tref="$( stat -c '%y' file2 )" || tref=''
+
+if test "${tref}"; then
+  for x in -newerat -newerct -newermt; do
+    rm -f out || framework_failure
+    find . $x "${tref}" -name 'file*' > out || fail=1
+    compare exp out || fail=1
+  done
+else
+  echo "Determining reference timestamp failed - skipping this part."
+fi
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 4e8efd41..739936c3 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -120,6 +120,7 @@ all_tests = \
   tests/find/refuse-noop.sh \
   tests/find/debug-missing-arg.sh \
   tests/find/used.sh \
+  tests/find/newer.sh \
   tests/xargs/conflicting_opts.sh \
   tests/xargs/verbose-quote.sh \
   $(all_root_tests)
-- 
2.36.1

