summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Marc Strapetz <marc.strapetz@syntevo.com>2022-01-07 11:17:28 +0000
committerLibravatar Junio C Hamano <gitster@pobox.com>2022-01-07 12:37:30 -0800
commitab6245bdeed46e1ff3bd378690066abd2ed0f2ef (patch)
treeeff4125a81937777d716db8dc841d22d87ba3bb8
parentThe sixth batch (diff)
downloadtgif-ab6245bdeed46e1ff3bd378690066abd2ed0f2ef.tar.xz
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>
-rw-r--r--t/test-lib-functions.sh33
1 files changed, 33 insertions, 0 deletions
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 389153e591..c1afa0884b 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1806,3 +1806,36 @@ test_region () {
test_readlink () {
perl -le 'print readlink($_) for @ARGV' "$@"
}
+
+# Set mtime to a fixed "magic" timestamp in mid February 2009, before we
+# run an operation that may or may not touch the file. If the file was
+# touched, its timestamp will not accidentally have such an old timestamp,
+# as long as your filesystem clock is reasonably correct. To verify the
+# timestamp, follow up with test_is_magic_mtime.
+#
+# An optional increment to the magic timestamp may be specified as second
+# argument.
+test_set_magic_mtime () {
+ local inc=${2:-0} &&
+ local mtime=$((1234567890 + $inc)) &&
+ test-tool chmtime =$mtime "$1" &&
+ test_is_magic_mtime "$1" $inc
+}
+
+# Test whether the given file has the "magic" mtime set. This is meant to
+# be used in combination with test_set_magic_mtime.
+#
+# An optional increment to the magic timestamp may be specified as second
+# argument. Usually, this should be the same increment which was used for
+# the associated test_set_magic_mtime.
+test_is_magic_mtime () {
+ local inc=${2:-0} &&
+ local mtime=$((1234567890 + $inc)) &&
+ echo $mtime >.git/test-mtime-expect &&
+ test-tool chmtime --get "$1" >.git/test-mtime-actual &&
+ test_cmp .git/test-mtime-expect .git/test-mtime-actual
+ local ret=$?
+ rm -f .git/test-mtime-expect
+ rm -f .git/test-mtime-actual
+ return $ret
+}