]> arthur.barton.de Git - bup.git/blob - lib/bup/t/txstat.py
Replace _helpers.utimensat() with utime() and lutime().
[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 try:
43     _have_bup_utime_ns = _helpers.bup_utime_ns
44 except AttributeError, e:
45     _have_bup_utime_ns = False
46
47 @wvtest
48 def test_timespec_behavior():
49     if not _have_bup_utime_ns:
50         return
51     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
52     try:
53         path = tmpdir + '/foo'
54         open(path, 'w').close()
55         frac_ts = (0, 10**9 / 2)
56         _helpers.bup_utime_ns(path, (frac_ts, frac_ts))
57         st = _helpers.stat(path)
58         atime_ts = st[8]
59         mtime_ts = st[9]
60         WVPASSEQ(atime_ts[0], 0)
61         WVPASS(atime_ts[1] == 0 or atime_ts[1] == frac_ts[1])
62         WVPASSEQ(mtime_ts[0], 0)
63         WVPASS(mtime_ts[1] == 0 or mtime_ts[1] == frac_ts[1])
64         if(mtime_ts[1] == frac_ts[1]):
65             # Sub-second resolution -- check behavior of negative timespecs.
66             neg_ts = (-43, 10**9 / 2)
67             _helpers.bup_utime_ns(path, (neg_ts, neg_ts))
68             st = _helpers.stat(path)
69             atime_ts = st[8]
70             mtime_ts = st[9]
71             WVPASSEQ(atime_ts, neg_ts)
72             WVPASSEQ(mtime_ts, neg_ts)
73     finally:
74         subprocess.call(['rm', '-rf', tmpdir])