git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 1/4] test-lib: introduce API for verifying file mtime

From
Marc Strapetz via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 6, 2022, 22:34 UTC
Message-ID
<e6301e9d770bc7b6a2a3eeddcaf4e0123a0b23ab.1641508499.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1105.v3.git.1641508499.gitgitgadget@gmail.com>
From: Marc Strapetz <marc.strapetz@syntevo.com>

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>
---
 t/test-lib-functions.sh | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 389153e5916..01dd8c01f59 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1806,3 +1806,31 @@ test_region () {
 test_readlink () {
 	perl -le 'print readlink($_) for @ARGV' "$@"
 }
+
+# Set a fixed "magic" mtime to the given file,
+# with an optional increment specified as second argument.
+# Use in combination with test_is_magic_mtime.
+test_set_magic_mtime () {
+	# We are using 1234567890 because it's a common timestamp used in
+	# various tests. It represents date 2009-02-13 which should be safe
+	# to use as long as the filetime clock is reasonably accurate.
+	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,
+# with an optional increment specified as second argument.
+# Use in combination with 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 .git/test-mtime-expect
+	rm .git/test-mtime-actual
+	return $ret
+}
-- 
gitgitgadget
Previous: Marc Strapetz via GitGitGadgetNext: Junio C Hamano
Message 11 of 20 in “update-index: refresh should rewrite index in case of racy timestamps”
  1. update-index: refresh should rewrite index in case of racy timestampsMarc Strapetz via GitGitGadget, Dec 22, 2021
  2. Junio C HamanoDec 22, 2021
  3. Marc StrapetzDec 23, 2021
  4. 0/2 update-index: refresh should rewrite index in case of racy timestampsMarc Strapetz via GitGitGadget, Jan 5, 2022
  5. 1/2 t7508: add tests capturing racy timestamp handlingMarc Strapetz via GitGitGadget, Jan 5, 2022
  6. Junio C HamanoJan 5, 2022
  7. Marc StrapetzJan 6, 2022
  8. 2/2 update-index: refresh should rewrite index in case of racy timestampsMarc Strapetz via GitGitGadget, Jan 5, 2022
  9. Junio C HamanoJan 5, 2022
  10. 0/4 update-index: refresh should rewrite index in case of racy timestampsMarc Strapetz via GitGitGadget, Jan 6, 2022
  11. 1/4 test-lib: introduce API for verifying file mtimeMarc Strapetz via GitGitGadget, Jan 6, 2022
  12. Junio C HamanoJan 6, 2022
  13. 2/4 t7508: fix bogus mtime verificationMarc Strapetz via GitGitGadget, Jan 6, 2022
  14. 3/4 t7508: add tests capturing racy timestamp handlingMarc Strapetz via GitGitGadget, Jan 6, 2022
  15. 4/4 update-index: refresh should rewrite index in case of racy timestampsMarc Strapetz via GitGitGadget, Jan 6, 2022
  16. 0/4 update-index: refresh should rewrite index in case of racy timestampsMarc Strapetz via GitGitGadget, Jan 7, 2022
  17. 1/4 test-lib: introduce API for verifying file mtimeMarc Strapetz via GitGitGadget, Jan 7, 2022
  18. 2/4 t7508: fix bogus mtime verificationMarc Strapetz via GitGitGadget, Jan 7, 2022
  19. 3/4 t7508: add tests capturing racy timestamp handlingMarc Strapetz via GitGitGadget, Jan 7, 2022
  20. 4/4 update-index: refresh should rewrite index in case of racy timestampsMarc Strapetz via GitGitGadget, Jan 7, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.