diff tests/lib-stat.py @ 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 6abd41f7d879
children 379050873141
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())