]> arthur.barton.de Git - bup.git/blob - lib/bup/t/txstat.py
Don't require non-negative timespec ns; fix stat timestamp 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     WVPASSEQ(xstat.timespec_to_nsecs((0, -1)), -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.nsecs_to_timeval(0), (0, 0))
34     WVPASSEQ(xstat.nsecs_to_timeval(10**9), (1, 0))
35     WVPASSEQ(xstat.nsecs_to_timeval(500000000), (0, (10**9 / 2) / 1000))
36     WVPASSEQ(xstat.nsecs_to_timeval(1500000000), (1, (10**9 / 2) / 1000))
37     WVPASSEQ(xstat.nsecs_to_timeval(-10**9), (-1, 0))
38     WVPASSEQ(xstat.nsecs_to_timeval(-500000000), (-1, (10**9 / 2) / 1000))
39     WVPASSEQ(xstat.nsecs_to_timeval(-1500000000), (-2, (10**9 / 2) / 1000))
40     x = xstat.nsecs_to_timeval(1977777778)
41     WVPASSEQ(type(x[0]), type(0))
42     WVPASSEQ(type(x[1]), type(0))
43     x = xstat.nsecs_to_timeval(-1977777778)
44     WVPASSEQ(type(x[0]), type(0))
45     WVPASSEQ(type(x[1]), type(0))
46
47     WVPASSEQ(xstat.fstime_floor_secs(0), 0)
48     WVPASSEQ(xstat.fstime_floor_secs(10**9 / 2), 0)
49     WVPASSEQ(xstat.fstime_floor_secs(10**9), 1)
50     WVPASSEQ(xstat.fstime_floor_secs(-10**9 / 2), -1)
51     WVPASSEQ(xstat.fstime_floor_secs(-10**9), -1)
52     WVPASSEQ(type(xstat.fstime_floor_secs(10**9 / 2)), type(0))
53     WVPASSEQ(type(xstat.fstime_floor_secs(-10**9 / 2)), type(0))
54
55
56 @wvtest
57 def test_bup_utimensat():
58     if not xstat._bup_utimensat:
59         return
60     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
61     try:
62         path = tmpdir + '/foo'
63         open(path, 'w').close()
64         frac_ts = (0, 10**9 / 2)
65         xstat._bup_utimensat(_helpers.AT_FDCWD, path, (frac_ts, frac_ts), 0)
66         st = _helpers.stat(path)
67         atime_ts = st[8]
68         mtime_ts = st[9]
69         WVPASSEQ(atime_ts[0], 0)
70         WVPASS(atime_ts[1] == 0 or atime_ts[1] == frac_ts[1])
71         WVPASSEQ(mtime_ts[0], 0)
72         WVPASS(mtime_ts[1] == 0 or mtime_ts[1] == frac_ts[1])
73     finally:
74         subprocess.call(['rm', '-rf', tmpdir])
75
76
77 @wvtest
78 def test_bup_utimes():
79     if not xstat._bup_utimes:
80         return
81     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
82     try:
83         path = tmpdir + '/foo'
84         open(path, 'w').close()
85         frac_ts = (0, 10**6 / 2)
86         xstat._bup_utimes(path, (frac_ts, frac_ts))
87         st = _helpers.stat(path)
88         atime_ts = st[8]
89         mtime_ts = st[9]
90         WVPASSEQ(atime_ts[0], 0)
91         WVPASS(atime_ts[1] == 0 or atime_ts[1] == frac_ts[1] * 1000)
92         WVPASSEQ(mtime_ts[0], 0)
93         WVPASS(mtime_ts[1] == 0 or mtime_ts[1] == frac_ts[1] * 1000)
94     finally:
95         subprocess.call(['rm', '-rf', tmpdir])
96
97
98 @wvtest
99 def test_bup_lutimes():
100     if not xstat._bup_lutimes:
101         return
102     tmpdir = tempfile.mkdtemp(prefix='bup-tmetadata-')
103     try:
104         path = tmpdir + '/foo'
105         open(path, 'w').close()
106         frac_ts = (0, 10**6 / 2)
107         xstat._bup_lutimes(path, (frac_ts, frac_ts))
108         st = _helpers.stat(path)
109         atime_ts = st[8]
110         mtime_ts = st[9]
111         WVPASSEQ(atime_ts[0], 0)
112         WVPASS(atime_ts[1] == 0 or atime_ts[1] == frac_ts[1] * 1000)
113         WVPASSEQ(mtime_ts[0], 0)
114         WVPASS(mtime_ts[1] == 0 or mtime_ts[1] == frac_ts[1] * 1000)
115     finally:
116         subprocess.call(['rm', '-rf', tmpdir])