Skip to content

Commit ab6245b

Browse files
mstrapgitster
authored andcommitted
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/[email protected]/T/#u Signed-off-by: Marc Strapetz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dcc0cd0 commit ab6245b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

t/test-lib-functions.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,3 +1806,36 @@ test_region () {
18061806
test_readlink () {
18071807
perl -le 'print readlink($_) for @ARGV' "$@"
18081808
}
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+
}

0 commit comments

Comments
 (0)