commit: 4b2cccbd8ab9a6fefd4d905365c744a039897ffe
parent b085c6e3023a4d8297cdb9e878e5329e18035ff3
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 3 Jun 2023 03:04:57 +0200
test-cmd/touch: Fix for FreeBSD
Diffstat:
4 files changed, 95 insertions(+), 54 deletions(-)
diff --git a/test-cmd/Kyuafile b/test-cmd/Kyuafile
@@ -30,7 +30,7 @@ atf_test_program{name="sizeof", required_files=basedir.."/cmd/sizeof", timeout=1
atf_test_program{name="sname", required_files=basedir.."/cmd/sname", timeout=1}
atf_test_program{name="strings", required_files=basedir.."/cmd/strings", timeout=1}
atf_test_program{name="tee", required_files=basedir.."/cmd/tee", timeout=1}
-atf_test_program{name="touch", required_files=basedir.."/cmd/touch", timeout=1}
+atf_test_program{name="touch", required_files=basedir.."/cmd/touch", timeout=3}
atf_test_program{name="true", required_files=basedir.."/cmd/true", timeout=1}
atf_test_program{name="tty", required_files=basedir.."/cmd/tty", timeout=1}
atf_test_program{name="unlink", required_files=basedir.."/cmd/unlink", timeout=1}
diff --git a/test-cmd/stat_atime b/test-cmd/stat_atime
@@ -0,0 +1,7 @@
+#!/bin/sh
+# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
+case "$(uname -o)" in
+ FreeBSD|NetBSD) stat -f%Sa -t'%F %T%z' "$@" ;;
+ *) stat -c%x "$@" ;;
+esac
diff --git a/test-cmd/stat_mtime b/test-cmd/stat_mtime
@@ -0,0 +1,7 @@
+#!/bin/sh
+# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
+case "$(uname -o)" in
+ FreeBSD|NetBSD) stat -f%Sm -t'%F %T%z' "$@" ;;
+ *) stat -c%y "$@" ;;
+esac
diff --git a/test-cmd/touch b/test-cmd/touch
@@ -5,137 +5,157 @@
atf_test_case noargs
noargs_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
}
atf_test_case ref_noargs
ref_noargs_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
- atf_check -o "inline:$(stat -c%x ../cmd/touch)\n" stat -c'%x' ./foo
- atf_check -o "inline:$(stat -c%y ../cmd/touch)\n" stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
+ atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
+ atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
}
atf_test_case mtime
mtime_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch -m ./foo
- atf_check -o "inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
+ atf_check -o "inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
}
atf_test_case ref_mtime
ref_mtime_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch -m -r ../cmd/touch ./foo
- atf_check -o "inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
- atf_check -o "not-inline:$(stat -c%x ../cmd/touch)\n" stat -c'%x' ./foo
- atf_check -o "inline:$(stat -c%y ../cmd/touch)\n" stat -c'%y' ./foo
+ atf_check -o "inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
+ atf_check -o "not-inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
+ atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
}
atf_test_case atime
atime_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch -a ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "inline:${mtime}\n" stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
}
atf_test_case ref_atime
ref_atime_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch -a -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "inline:${mtime}\n" stat -c'%y' ./foo
- atf_check -o "inline:$(stat -c%x ../cmd/touch)\n" stat -c'%x' ./foo
- atf_check -o "not-inline:$(stat -c%y ../cmd/touch)\n" stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
+ atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
+ atf_check -o "not-inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
}
atf_test_case amtime
amtime_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch -a -m ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
}
atf_test_case ref_amtime
ref_amtime_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
atf_check ../cmd/touch -a -m -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
- atf_check -o "inline:$(stat -c%x ../cmd/touch)\n" stat -c'%x' ./foo
- atf_check -o "inline:$(stat -c%y ../cmd/touch)\n" stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
+ atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
+ atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
}
atf_test_case optd
optd_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
unset TZ
atf_check ../cmd/touch -d 2003-06-02T13:37:42Z ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' stat -c'%x' ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
+ atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
+ atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
}
atf_test_case optd_frac
optd_frac_body() {
atf_check touch -a ./foo
+ maybe_sleep
atf_check touch -m ./foo
- atime="$(stat -c'%x' ./foo)"
- mtime="$(stat -c'%y' ./foo)"
+ maybe_sleep
+ atime="$(./stat_atime ./foo)"
+ mtime="$(./stat_mtime ./foo)"
unset TZ
atf_check ../cmd/touch -d 2003-06-02T13:37:42.713Z ./foo
- atf_check -o "not-inline:${atime}\n" stat -c'%x' ./foo
- atf_check -o "not-inline:${mtime}\n" stat -c'%y' ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' stat -c'%x' ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' stat -c'%y' ./foo
+ atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
+ atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
+ atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
+ atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
}
atf_init_test_cases() {
@@ -152,5 +172,12 @@ atf_init_test_cases() {
atf_add_test_case ref_amtime
atf_add_test_case optd
- atf_add_test_case optd_frac
+
+ # No support for displaying fractional seconds on BSD stat(1)
+ if uname -o | grep -iq BSD; then
+ maybe_sleep() { sleep 1; }
+ else
+ atf_add_test_case optd_frac
+ maybe_sleep() { sleep .1; }
+ fi
}