Mercurial > hgrepos > DevTools > mercurial-extensions
changeset 278:7ec353866f70
Extend the lib-stat.py script to be able to assert timestamp differences.
This is used now for asserting **changed** timestamps of files.
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Sat, 05 Jan 2019 12:12:42 +0100 |
| parents | 5e5d11417138 |
| children | fd0f5631d11a |
| files | tests/lib-stat.py tests/test-timestamps.t |
| diffstat | 2 files changed, 71 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/tests/lib-stat.py Fri Jan 04 22:45:34 2019 +0100 +++ b/tests/lib-stat.py Sat Jan 05 12:12:42 2019 +0100 @@ -1,9 +1,27 @@ +"""Cross-platform file modification check. + +This is because the stat(1) utility has incompatible cross-platform usage. + +Usage: lib-stat.py [ OPTIONS ] FILE ... + +Options: + + -c TS, --check=TS Assert that the given timestamp TS matches the given + FILE's modtime + -d TS, --differs=TS Assert that the given timestamp TS differs from the + given FILE's modtime + +If no option is given print the modification time for each FILE to stdout. + +""" + from __future__ import absolute_import, print_function -import sys +import datetime +import getopt import os -import datetime +import sys class FloatTimesInStat(object): @@ -46,11 +64,53 @@ def main(): - with FloatTimesInStat(): - for f in sys.argv[1:]: - st = os.lstat(f) - print(to_isoformat(st.st_mtime)) + opt_check = None + opt_differs = None + + opts, args = getopt.getopt(sys.argv[1:], + "c:d:", + ["check=", + "differs="]) + for opt, val in opts: + if opt in ("-c", "--check"): + opt_check = val + elif opt in ("-d", "--differs"): + opt_differs = val + else: + assert False + + if opt_check: + with FloatTimesInStat(): + for f in args: + st = os.lstat(f) + tss = to_isoformat(st.st_mtime) + if tss != opt_check: + print( + "mtime does not match for file `{filename}': expected {expected}, current: {current}".format( + filename=f, + expected=opt_check, + current=tss), + file=sys.stderr) + return 1 + elif opt_differs: + with FloatTimesInStat(): + for f in args: + st = os.lstat(f) + tss = to_isoformat(st.st_mtime) + if tss == opt_differs: + print( + "mtime does not differ for file `{filename}': {timestamp}".format( + filename=f, + timestamp=tss), + file=sys.stderr) + return 1 + else: + with FloatTimesInStat(): + for f in args: + st = os.lstat(f) + print(to_isoformat(st.st_mtime)) + return 0 if __name__ == "__main__": - main() + sys.exit(main())
--- a/tests/test-timestamps.t Fri Jan 04 22:45:34 2019 +0100 +++ b/tests/test-timestamps.t Sat Jan 05 12:12:42 2019 +0100 @@ -127,12 +127,12 @@ $ hg revert -C files/test1.txt $ hg status M files/test2.txt - $ "$TSPY" "$TESTDIR/lib-stat.py" files/test1.txt - 2017-12-01T02:03:04Z + $ "$TSPY" "$TESTDIR/lib-stat.py" --check=2017-12-01T02:03:04Z files/test1.txt + $ "$TSPY" "$TESTDIR/lib-stat.py" --differs=2017-12-01T08:09:10Z files/test2.txt $ hg revert -C files/test2.txt $ hg status - $ "$TSPY" "$TESTDIR/lib-stat.py" files/test2.txt - 2017-12-01T08:09:10Z + $ "$TSPY" "$TESTDIR/lib-stat.py" --check=2017-12-01T08:09:10Z files/test2.txt + Committing an explicit list of files
