]> arthur.barton.de Git - bup.git/blob - lib/bup/t/thelpers.py
Add helpers.fstat and _helpers._have_ns_fs_timestamps.
[bup.git] / lib / bup / t / thelpers.py
1 import os, math
2 import bup._helpers as _helpers
3
4 from bup.helpers import *
5 from wvtest import *
6
7 @wvtest
8 def test_parse_num():
9     pn = parse_num
10     WVPASSEQ(pn('1'), 1)
11     WVPASSEQ(pn('0'), 0)
12     WVPASSEQ(pn('1.5k'), 1536)
13     WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
14     WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
15     WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
16
17
18 @wvtest
19 def test_detect_fakeroot():
20     if os.getenv('FAKEROOTKEY'):
21         WVPASS(detect_fakeroot())
22     else:
23         WVPASS(not detect_fakeroot())
24
25
26 def _test_fstime():
27     def approx_eq(x, y):
28         return math.fabs(x - y) < 1 / 10e8
29     def ts_eq_ish(x, y):
30         return approx_eq(x[0], y[0]) and approx_eq(x[1], y[1])
31     def fst_eq_ish(x, y):
32         return approx_eq(x.approx_secs(), y.approx_secs())
33     def s_ns_eq_ish(fst, s, ns):
34         (fst_s, fst_ns) = fst.secs_nsecs()
35         return approx_eq(fst_s, s) and approx_eq(fst_ns, ns)
36     from_secs = FSTime.from_secs
37     from_ts = FSTime.from_timespec
38     WVPASS(from_secs(0) == from_secs(0))
39     WVPASS(from_secs(0) < from_secs(1))
40     WVPASS(from_secs(-1) < from_secs(1))
41     WVPASS(from_secs(1) > from_secs(0))
42     WVPASS(from_secs(1) > from_secs(-1))
43
44     WVPASS(fst_eq_ish(from_secs(0), from_ts((0, 0))))
45     WVPASS(fst_eq_ish(from_secs(1), from_ts((1, 0))))
46     WVPASS(fst_eq_ish(from_secs(-1), from_ts((-1, 0))))
47     WVPASS(fst_eq_ish(from_secs(-0.5), from_ts((-1, 10**9 / 2))))
48     WVPASS(fst_eq_ish(from_secs(-1.5), from_ts((-2, 10**9 / 2))))
49     WVPASS(fst_eq_ish(from_secs(1 / 10e8), from_ts((0, 1))))
50     WVPASS(fst_eq_ish(from_secs(-1 / 10e8), from_ts((-1, 10**9 - 1))))
51
52     WVPASS(ts_eq_ish(from_secs(0).to_timespec(), (0, 0)))
53     WVPASS(ts_eq_ish(from_secs(1).to_timespec(), (1, 0)))
54     WVPASS(ts_eq_ish(from_secs(-1).to_timespec(), (-1, 0)))
55     WVPASS(ts_eq_ish(from_secs(-0.5).to_timespec(), (-1, 10**9 / 2)))
56     WVPASS(ts_eq_ish(from_secs(-1.5).to_timespec(), (-2, 10**9 / 2)))
57     WVPASS(ts_eq_ish(from_secs(1 / 10e8).to_timespec(), (0, 1)))
58     WVPASS(ts_eq_ish(from_secs(-1 / 10e8).to_timespec(), (-1, 10**9 - 1)))
59
60     WVPASS(s_ns_eq_ish(from_secs(0), 0, 0))
61     WVPASS(s_ns_eq_ish(from_secs(1), 1, 0))
62     WVPASS(s_ns_eq_ish(from_secs(-1), -1, 0))
63     WVPASS(s_ns_eq_ish(from_secs(-0.5), 0, - 10**9 / 2))
64     WVPASS(s_ns_eq_ish(from_secs(-1.5), -1, - 10**9 / 2))
65     WVPASS(s_ns_eq_ish(from_secs(-1 / 10e8), 0, -1))
66
67 @wvtest
68 def test_fstime():
69     _test_fstime();
70     if _helpers._have_ns_fs_timestamps: # Test native python timestamp rep too.
71         orig = _helpers._have_ns_fs_timestamps
72         try:
73             _helpers._have_ns_fs_timestamps = None
74             _test_fstime();
75         finally:
76             _helpers._have_ns_fs_timestamps = orig