4 import bup._helpers as _helpers
5 from bup.helpers import *
11 # Test whatever you end up with for next() after import '*'.
12 WVPASSEQ(next(iter([]), None), None)
14 WVPASSEQ(next(x, None), 1)
15 WVPASSEQ(next(x, None), None)
17 WVPASSEQ(next(x, 'x'), 1)
18 WVPASSEQ(next(x, 'x'), 'x')
19 WVEXCEPT(StopIteration, next, iter([]))
22 WVEXCEPT(StopIteration, next, x)
26 def test_fallback_next():
29 next = helpers._fallback_next
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))
47 def test_detect_fakeroot():
48 if os.getenv('FAKEROOTKEY'):
49 WVPASS(detect_fakeroot())
51 WVPASS(not detect_fakeroot())
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')
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', [])
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'),
87 ('baz', '/foo/bar/baz'),
88 ('bax', '/foo/bar/baz/bax')])
89 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
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')],
101 ('bar', '/foo/bar')])
102 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
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', [])
113 x = readpipe(['echo', '42'])
116 readpipe(['bash', '-c', 'exit 42'])
117 except Exception, ex:
118 WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
122 def test_batchpipe():
123 for chunk in batchpipe(['echo'], []):
126 for chunk in batchpipe(['echo'], ['42']):
128 WVPASSEQ(out, '42\n')
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.
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)