]> arthur.barton.de Git - bup.git/blob - lib/bup/t/thelpers.py
68004b72f03e69633fa765d776144a90c3e90b42
[bup.git] / lib / bup / t / thelpers.py
1 import helpers
2 import math
3 import os
4 import os.path
5 import tempfile
6 import stat
7 import bup._helpers as _helpers
8 from bup.helpers import *
9 from wvtest import *
10
11 bup_tmp = os.path.realpath('../../../t/tmp')
12 mkdirp(bup_tmp)
13
14 @wvtest
15 def test_next():
16     # Test whatever you end up with for next() after import '*'.
17     WVPASSEQ(next(iter([]), None), None)
18     x = iter([1])
19     WVPASSEQ(next(x, None), 1)
20     WVPASSEQ(next(x, None), None)
21     x = iter([1])
22     WVPASSEQ(next(x, 'x'), 1)
23     WVPASSEQ(next(x, 'x'), 'x')
24     WVEXCEPT(StopIteration, next, iter([]))
25     x = iter([1])
26     WVPASSEQ(next(x), 1)
27     WVEXCEPT(StopIteration, next, x)
28
29
30 @wvtest
31 def test_fallback_next():
32     global next
33     orig = next
34     next = helpers._fallback_next
35     try:
36         test_next()
37     finally:
38         next = orig
39
40
41 @wvtest
42 def test_parse_num():
43     pn = parse_num
44     WVPASSEQ(pn('1'), 1)
45     WVPASSEQ(pn('0'), 0)
46     WVPASSEQ(pn('1.5k'), 1536)
47     WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
48     WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
49     WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
50
51 @wvtest
52 def test_detect_fakeroot():
53     if os.getenv('FAKEROOTKEY'):
54         WVPASS(detect_fakeroot())
55     else:
56         WVPASS(not detect_fakeroot())
57
58 @wvtest
59 def test_path_components():
60     WVPASSEQ(path_components('/'), [('', '/')])
61     WVPASSEQ(path_components('/foo'), [('', '/'), ('foo', '/foo')])
62     WVPASSEQ(path_components('/foo/'), [('', '/'), ('foo', '/foo')])
63     WVPASSEQ(path_components('/foo/bar'),
64              [('', '/'), ('foo', '/foo'), ('bar', '/foo/bar')])
65     WVEXCEPT(Exception, path_components, 'foo')
66
67
68 @wvtest
69 def test_stripped_path_components():
70     WVPASSEQ(stripped_path_components('/', []), [('', '/')])
71     WVPASSEQ(stripped_path_components('/', ['']), [('', '/')])
72     WVPASSEQ(stripped_path_components('/', ['/']), [('', '/')])
73     WVPASSEQ(stripped_path_components('/', ['/foo']), [('', '/')])
74     WVPASSEQ(stripped_path_components('/foo', ['/bar']),
75              [('', '/'), ('foo', '/foo')])
76     WVPASSEQ(stripped_path_components('/foo', ['/foo']), [('', '/foo')])
77     WVPASSEQ(stripped_path_components('/foo/bar', ['/foo']),
78              [('', '/foo'), ('bar', '/foo/bar')])
79     WVPASSEQ(stripped_path_components('/foo/bar', ['/bar', '/foo', '/baz']),
80              [('', '/foo'), ('bar', '/foo/bar')])
81     WVPASSEQ(stripped_path_components('/foo/bar/baz', ['/foo/bar/baz']),
82              [('', '/foo/bar/baz')])
83     WVEXCEPT(Exception, stripped_path_components, 'foo', [])
84
85
86 @wvtest
87 def test_grafted_path_components():
88     WVPASSEQ(grafted_path_components([('/chroot', '/')], '/foo'),
89              [('', '/'), ('foo', '/foo')])
90     WVPASSEQ(grafted_path_components([('/foo/bar', '/')], '/foo/bar/baz/bax'),
91              [('', '/foo/bar'),
92               ('baz', '/foo/bar/baz'),
93               ('bax', '/foo/bar/baz/bax')])
94     WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
95                                      '/foo/bar/baz/1/2'),
96              [('', None),
97               ('bax', '/foo/bar/baz'),
98               ('1', '/foo/bar/baz/1'),
99               ('2', '/foo/bar/baz/1/2')])
100     WVPASSEQ(grafted_path_components([('/foo', '/bar/baz/bax')],
101                                      '/foo/bar'),
102              [('', None),
103               ('bar', None),
104               ('baz', None),
105               ('bax', '/foo'),
106               ('bar', '/foo/bar')])
107     WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
108                                      '/foo/bar/baz'),
109              [('', None), ('a', None), ('b', None), ('c', '/foo/bar/baz')])
110     WVPASSEQ(grafted_path_components([('/', '/a/b/c/')], '/foo/bar'),
111              [('', None), ('a', None), ('b', None), ('c', '/'),
112               ('foo', '/foo'), ('bar', '/foo/bar')])
113     WVEXCEPT(Exception, grafted_path_components, 'foo', [])
114
115
116 @wvtest
117 def test_readpipe():
118     x = readpipe(['echo', '42'])
119     WVPASSEQ(x, '42\n')
120     try:
121         readpipe(['bash', '-c', 'exit 42'])
122     except Exception, ex:
123         WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
124
125
126 @wvtest
127 def test_batchpipe():
128     for chunk in batchpipe(['echo'], []):
129         WVPASS(False)
130     out = ''
131     for chunk in batchpipe(['echo'], ['42']):
132         out += chunk
133     WVPASSEQ(out, '42\n')
134     try:
135         batchpipe(['bash', '-c'], ['exit 42'])
136     except Exception, ex:
137         WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
138     args = [str(x) for x in range(6)]
139     # Force batchpipe to break the args into batches of 3.  This
140     # approach assumes all args are the same length.
141     arg_max = \
142         helpers._argmax_base(['echo']) + helpers._argmax_args_size(args[:3])
143     batches = batchpipe(['echo'], args, arg_max=arg_max)
144     WVPASSEQ(next(batches), '0 1 2\n')
145     WVPASSEQ(next(batches), '3 4 5\n')
146     WVPASSEQ(next(batches, None), None)
147     batches = batchpipe(['echo'], [str(x) for x in range(5)], arg_max=arg_max)
148     WVPASSEQ(next(batches), '0 1 2\n')
149     WVPASSEQ(next(batches), '3 4\n')
150     WVPASSEQ(next(batches, None), None)
151
152
153 @wvtest
154 def test_atomically_replaced_file():
155     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-thelper-')
156     target_file = os.path.join(tmpdir, 'test-atomic-write')
157     initial_failures = wvfailure_count()
158
159     with atomically_replaced_file(target_file, mode='w') as f:
160         f.write('asdf')
161         WVPASSEQ(f.mode, 'w')
162     f = open(target_file, 'r')
163     WVPASSEQ(f.read(), 'asdf')
164
165     try:
166         with atomically_replaced_file(target_file, mode='w') as f:
167             f.write('wxyz')
168             raise Exception()
169     except:
170         pass
171     with open(target_file) as f:
172         WVPASSEQ(f.read(), 'asdf')
173
174     with atomically_replaced_file(target_file, mode='wb') as f:
175         f.write(os.urandom(20))
176         WVPASSEQ(f.mode, 'wb')
177
178     if wvfailure_count() == initial_failures:
179         subprocess.call(['rm', '-rf', tmpdir])