]> arthur.barton.de Git - bup.git/blob - lib/bup/t/thelpers.py
38596d7447e1e33c416cb399748506a2613e232a
[bup.git] / lib / bup / t / thelpers.py
1 import config
2 import helpers
3 import math
4 import os
5 import bup._helpers as _helpers
6 from bup.helpers import *
7 from wvtest import *
8
9
10 @wvtest
11 def test_next():
12     # Test whatever you end up with for next() after import '*'.
13     WVPASSEQ(next(iter([]), None), None)
14     x = iter([1])
15     WVPASSEQ(next(x, None), 1)
16     WVPASSEQ(next(x, None), None)
17     x = iter([1])
18     WVPASSEQ(next(x, 'x'), 1)
19     WVPASSEQ(next(x, 'x'), 'x')
20     WVEXCEPT(StopIteration, next, iter([]))
21     x = iter([1])
22     WVPASSEQ(next(x), 1)
23     WVEXCEPT(StopIteration, next, x)
24
25
26 @wvtest
27 def test_fallback_next():
28     global next
29     orig = next
30     next = helpers._fallback_next
31     try:
32         test_next()
33     finally:
34         next = orig
35
36
37 @wvtest
38 def test_parse_num():
39     pn = parse_num
40     WVPASSEQ(pn('1'), 1)
41     WVPASSEQ(pn('0'), 0)
42     WVPASSEQ(pn('1.5k'), 1536)
43     WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
44     WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
45     WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
46
47 @wvtest
48 def test_detect_fakeroot():
49     if os.getenv('FAKEROOTKEY'):
50         WVPASS(detect_fakeroot())
51     else:
52         WVPASS(not detect_fakeroot())
53
54 @wvtest
55 def test_path_components():
56     WVPASSEQ(path_components('/'), [('', '/')])
57     WVPASSEQ(path_components('/foo'), [('', '/'), ('foo', '/foo')])
58     WVPASSEQ(path_components('/foo/'), [('', '/'), ('foo', '/foo')])
59     WVPASSEQ(path_components('/foo/bar'),
60              [('', '/'), ('foo', '/foo'), ('bar', '/foo/bar')])
61     WVEXCEPT(Exception, path_components, 'foo')
62
63
64 @wvtest
65 def test_stripped_path_components():
66     WVPASSEQ(stripped_path_components('/', []), [('', '/')])
67     WVPASSEQ(stripped_path_components('/', ['']), [('', '/')])
68     WVPASSEQ(stripped_path_components('/', ['/']), [('', '/')])
69     WVPASSEQ(stripped_path_components('/', ['/foo']), [('', '/')])
70     WVPASSEQ(stripped_path_components('/foo', ['/bar']),
71              [('', '/'), ('foo', '/foo')])
72     WVPASSEQ(stripped_path_components('/foo', ['/foo']), [('', '/foo')])
73     WVPASSEQ(stripped_path_components('/foo/bar', ['/foo']),
74              [('', '/foo'), ('bar', '/foo/bar')])
75     WVPASSEQ(stripped_path_components('/foo/bar', ['/bar', '/foo', '/baz']),
76              [('', '/foo'), ('bar', '/foo/bar')])
77     WVPASSEQ(stripped_path_components('/foo/bar/baz', ['/foo/bar/baz']),
78              [('', '/foo/bar/baz')])
79     WVEXCEPT(Exception, stripped_path_components, 'foo', [])
80
81
82 @wvtest
83 def test_grafted_path_components():
84     WVPASSEQ(grafted_path_components([('/chroot', '/')], '/foo'),
85              [('', '/'), ('foo', '/foo')])
86     WVPASSEQ(grafted_path_components([('/foo/bar', '/')], '/foo/bar/baz/bax'),
87              [('', '/foo/bar'),
88               ('baz', '/foo/bar/baz'),
89               ('bax', '/foo/bar/baz/bax')])
90     WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
91                                      '/foo/bar/baz/1/2'),
92              [('', None),
93               ('bax', '/foo/bar/baz'),
94               ('1', '/foo/bar/baz/1'),
95               ('2', '/foo/bar/baz/1/2')])
96     WVPASSEQ(grafted_path_components([('/foo', '/bar/baz/bax')],
97                                      '/foo/bar'),
98              [('', None),
99               ('bar', None),
100               ('baz', None),
101               ('bax', '/foo'),
102               ('bar', '/foo/bar')])
103     WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
104                                      '/foo/bar/baz'),
105              [('', None), ('a', None), ('b', None), ('c', '/foo/bar/baz')])
106     WVPASSEQ(grafted_path_components([('/', '/a/b/c/')], '/foo/bar'),
107              [('', None), ('a', None), ('b', None), ('c', '/'),
108               ('foo', '/foo'), ('bar', '/foo/bar')])
109     WVEXCEPT(Exception, grafted_path_components, 'foo', [])
110
111
112 @wvtest
113 def test_readpipe():
114     x = readpipe(['echo', '42'])
115     WVPASSEQ(x, '42\n')
116     try:
117         readpipe(['bash', '-c', 'exit 42'])
118     except Exception, ex:
119         WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
120
121
122 @wvtest
123 def test_batchpipe():
124     for chunk in batchpipe(['echo'], []):
125         WVPASS(False)
126     out = ''
127     for chunk in batchpipe(['echo'], ['42']):
128         out += chunk
129     WVPASSEQ(out, '42\n')
130     try:
131         batchpipe(['bash', '-c'], ['exit 42'])
132     except Exception, ex:
133         WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
134     oldmax = config.arg_max
135     args = [str(x) for x in range(6)]
136     # Force batchpipe to break the args into batches of 3.  This
137     # approach assumes all args are the same length.
138     config.arg_max = \
139         helpers._argmax_base(['echo']) + helpers._argmax_args_size(args[:3])
140     batches = batchpipe(['echo'], args)
141     WVPASSEQ(next(batches), '0 1 2\n')
142     WVPASSEQ(next(batches), '3 4 5\n')
143     WVPASSEQ(next(batches, None), None)
144     batches = batchpipe(['echo'], [str(x) for x in range(5)])
145     WVPASSEQ(next(batches), '0 1 2\n')
146     WVPASSEQ(next(batches), '3 4\n')
147     WVPASSEQ(next(batches, None), None)
148     config.arg_max = oldmax