5 import bup._helpers as _helpers
6 from bup.helpers import *
12 # Test whatever you end up with for next() after import '*'.
13 WVPASSEQ(next(iter([]), None), None)
15 WVPASSEQ(next(x, None), 1)
16 WVPASSEQ(next(x, None), None)
18 WVPASSEQ(next(x, 'x'), 1)
19 WVPASSEQ(next(x, 'x'), 'x')
20 WVEXCEPT(StopIteration, next, iter([]))
23 WVEXCEPT(StopIteration, next, x)
27 def test_fallback_next():
30 next = helpers._fallback_next
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))
48 def test_detect_fakeroot():
49 if os.getenv('FAKEROOTKEY'):
50 WVPASS(detect_fakeroot())
52 WVPASS(not detect_fakeroot())
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')
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', [])
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'),
88 ('baz', '/foo/bar/baz'),
89 ('bax', '/foo/bar/baz/bax')])
90 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
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')],
102 ('bar', '/foo/bar')])
103 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
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', [])
114 x = readpipe(['echo', '42'])
117 readpipe(['bash', '-c', 'exit 42'])
118 except Exception, ex:
119 WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
123 def test_batchpipe():
124 for chunk in batchpipe(['echo'], []):
127 for chunk in batchpipe(['echo'], ['42']):
129 WVPASSEQ(out, '42\n')
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.
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