test-lib: introduce API for verifying file mtime
Add functions `test_set_magic_mtime` and `test_is_magic_mtime` which can be used to (re)set the mtime of a file to a predefined ("magic") timestamp, then perform some operations and finally check for mtime changes of the file. The core implementation follows the suggestion from the mailing list [1]. [1] https://lore.kernel.org/git/xmqqczl5hpaq.fsf@gitster.g/T/#u Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Marc Strapetz committed
Jan 7, 2022 at 11:17 UTC
ab6245bdeed46e1ff3bd378690066abd2ed0f2ef
1 file changed
+33
t/test-lib-functions.sh
+33
@@ -1806,3 +1806,36 @@ test_region () {
1806
test_readlink () {
1807
perl -le 'print readlink($_) for @ARGV' "$@"
1808
}
1809
+
1810
+# Set mtime to a fixed "magic" timestamp in mid February 2009, before we
1811
+# run an operation that may or may not touch the file. If the file was
1812
+# touched, its timestamp will not accidentally have such an old timestamp,
1813
+# as long as your filesystem clock is reasonably correct. To verify the
1814
+# timestamp, follow up with test_is_magic_mtime.
1815
+#
1816
+# An optional increment to the magic timestamp may be specified as second
1817
+# argument.
1818
+test_set_magic_mtime () {
1819
+ local inc=${2:-0} &&
1820
+ local mtime=$((1234567890 + $inc)) &&
1821
+ test-tool chmtime =$mtime "$1" &&
1822
+ test_is_magic_mtime "$1" $inc
1823
+}
1824
+
1825
+# Test whether the given file has the "magic" mtime set. This is meant to
1826
+# be used in combination with test_set_magic_mtime.
1827
+#
1828
+# An optional increment to the magic timestamp may be specified as second
1829
+# argument. Usually, this should be the same increment which was used for
1830
+# the associated test_set_magic_mtime.
1831
+test_is_magic_mtime () {
1832
+ local inc=${2:-0} &&
1833
+ local mtime=$((1234567890 + $inc)) &&
1834
+ echo $mtime >.git/test-mtime-expect &&
1835
+ test-tool chmtime --get "$1" >.git/test-mtime-actual &&
1836
+ test_cmp .git/test-mtime-expect .git/test-mtime-actual
1837
+ local ret=$?
1838
+ rm -f .git/test-mtime-expect
1839
+ rm -f .git/test-mtime-actual
1840
+ return $ret
1841
+}