]> arthur.barton.de Git - bup.git/blob - lib/bup/t/txstat.py
Always publish utimensat in helpers when available and fix type conversions.
[bup.git] / lib / bup / t / txstat.py
1 import math, tempfile, subprocess
2 from wvtest import *
3 import bup._helpers as _helpers
4 from bup import xstat
5
6 @wvtest
7 def test_fstime():
8     WVPASSEQ(xstat.timespec_to_nsecs((0, 0)), 0)
9     WVPASSEQ(xstat.timespec_to_nsecs((1, 0)), 10**9)
10     WVPASSEQ(xstat.timespec_to_nsecs((0, 10**9 / 2)), 500000000)
11     WVPASSEQ(xstat.timespec_to_nsecs((1, 10**9 / 2)), 1500000000)
12     WVPASSEQ(xstat.timespec_to_nsecs((-1, 0)), -10**9)
13     WVPASSEQ(xstat.timespec_to_nsecs((-1, 10**9 / 2)), -500000000)
14     WVPASSEQ(xstat.timespec_to_nsecs((-2, 10**9 / 2)), -1500000000)
15     WVEXCEPT(Exception, xstat.timespec_to_nsecs, (0, -1))
16     WVPASSEQ(type(xstat.timespec_to_nsecs((2, 22222222))), type(0))
17     WVPASSEQ(type(xstat.timespec_to_nsecs((-2, 22222222))), type(0))
18
19     WVPASSEQ(xstat.nsecs_to_timespec(0), (0, 0))
20     WVPASSEQ(xstat.nsecs_to_timespec(10**9), (1, 0))
21     WVPASSEQ(xstat.nsecs_to_timespec(500000000), (0, 10**9 / 2))
22     WVPASSEQ(xstat.nsecs_to_timespec(1500000000), (1, 10**9 / 2))
23     WVPASSEQ(xstat.nsecs_to_timespec(-10**9), (-1, 0))
24     WVPASSEQ(xstat.nsecs_to_timespec(-500000000), (-1, 10**9 / 2))
25     WVPASSEQ(xstat.nsecs_to_timespec(-1500000000), (-2, 10**9 / 2))
26     x = xstat.nsecs_to_timespec(1977777778)
27     WVPASSEQ(type(x[0]), type(0))
28     WVPASSEQ(type(x[1]), type(0))
29     x = xstat.nsecs_to_timespec(-1977777778)
30     WVPASSEQ(type(x[0]), type(0))
31     WVPASSEQ(type(x[1]), type(0))
32
33     WVPASSEQ(xstat.fstime_floor_secs(0), 0)
34     WVPASSEQ(xstat.fstime_floor_secs(10**9 / 2), 0)
35     WVPASSEQ(xstat.fstime_floor_secs(10**9), 1)
36     WVPASSEQ(xstat.fstime_floor_secs(-10**9 / 2), -1)
37     WVPASSEQ(xstat.fstime_floor_secs(-10**9), -1)
38     WVPASSEQ(type(xstat.fstime_floor_secs(10**9 / 2)), type(0))
39     WVPASSEQ(type(xstat.fstime_floor_secs(-10**9 / 2)), type(0))
40
41
42 @wvtest
43 def test_bup_utimensat():
44     if not xstat._bup_utimensat:
45         return
46     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
47     try:
48         path = tmpdir + '/foo'
49         open(path, 'w').close()
50         frac_ts = (0, 10**9 / 2)
51         xstat._bup_utimensat(_helpers.AT_FDCWD, path, (frac_ts, frac_ts), 0)
52         st = _helpers.stat(path)
53         atime_ts = st[8]
54         mtime_ts = st[9]
55         WVPASSEQ(atime_ts[0], 0)
56         WVPASS(atime_ts[1] == 0 or atime_ts[1] == frac_ts[1])
57         WVPASSEQ(mtime_ts[0], 0)
58         WVPASS(mtime_ts[1] == 0 or mtime_ts[1] == frac_ts[1])
59     finally:
60         subprocess.call(['rm', '-rf', tmpdir])
61
62
63 try:
64     _have_bup_utime_ns = _helpers.bup_utime_ns
65 except AttributeError, e:
66     _have_bup_utime_ns = False
67
68 @wvtest
69 def test_timespec_behavior():
70     if not _have_bup_utime_ns:
71         return
72     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
73     try:
74         path = tmpdir + '/foo'
75         open(path, 'w').close()
76         frac_ts = (0, 10**9 / 2)
77         _helpers.bup_utime_ns(path, (frac_ts, frac_ts))
78         st = _helpers.stat(path)
79         atime_ts = st[8]
80         mtime_ts = st[9]
81         WVPASSEQ(atime_ts[0], 0)
82         WVPASS(atime_ts[1] == 0 or atime_ts[1] == frac_ts[1])
83         WVPASSEQ(mtime_ts[0], 0)
84         WVPASS(mtime_ts[1] == 0 or mtime_ts[1] == frac_ts[1])
85     finally:
86         subprocess.call(['rm', '-rf', tmpdir])