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