diff tests/lib-stat.py @ 273:6abd41f7d879

Unit-tests for almost all currently implemented features of the timestamps extension. BUGS: Missing testing commits with explicit files given.
author Franz Glasner <hg@dom66.de>
date Fri, 04 Jan 2019 22:09:06 +0100
parents
children 7ec353866f70
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib-stat.py	Fri Jan 04 22:09:06 2019 +0100
@@ -0,0 +1,56 @@
+from __future__ import absolute_import, print_function
+
+
+import sys
+import os
+import datetime
+
+
+class FloatTimesInStat(object):
+    """Context manager to ensure that stat returns float values.
+
+    For 3.7 <= Mercurial < 4.6: Mercurial calls :func:`os.stat_float_times`
+    for `stat` and friends to return :class:`int` values.
+
+    Temporarily fix this.
+
+    """
+
+    __slots__ = ("_do_reset",)
+
+    def __init__(self):
+        self._do_reset = False
+
+    def __enter__(self):
+        if not os.stat_float_times():
+            os.stat_float_times(True)
+            self._do_reset = True
+        return self
+
+    def __exit__(self, *args):
+        if self._do_reset:
+            os.stat_float_times(False)
+
+
+def to_isoformat(t):
+    """Return the POSIX timestamp `t` formatted in full ISO format.
+
+    `t` is expected to be in the UTC timezone.
+
+    """
+    dt = datetime.datetime.utcfromtimestamp(t)
+    if dt.utcoffset() is None:
+        return dt.isoformat() + "Z"
+    else:
+        return dt.isoformat()
+
+
+def main():
+    with FloatTimesInStat():
+        for f in sys.argv[1:]:
+            st = os.lstat(f)
+            print(to_isoformat(st.st_mtime))
+
+
+if __name__ == "__main__":
+    main()