7 import bup._helpers as _helpers
8 from bup.helpers import *
14 # Test whatever you end up with for next() after import '*'.
15 WVPASSEQ(next(iter([]), None), None)
17 WVPASSEQ(next(x, None), 1)
18 WVPASSEQ(next(x, None), None)
20 WVPASSEQ(next(x, 'x'), 1)
21 WVPASSEQ(next(x, 'x'), 'x')
22 WVEXCEPT(StopIteration, next, iter([]))
25 WVEXCEPT(StopIteration, next, x)
29 def test_fallback_next():
32 next = helpers._fallback_next
44 WVPASSEQ(pn('1.5k'), 1536)
45 WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
46 WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
47 WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
50 def test_detect_fakeroot():
51 if os.getenv('FAKEROOTKEY'):
52 WVPASS(detect_fakeroot())
54 WVPASS(not detect_fakeroot())
57 def test_path_components():
58 WVPASSEQ(path_components('/'), [('', '/')])
59 WVPASSEQ(path_components('/foo'), [('', '/'), ('foo', '/foo')])
60 WVPASSEQ(path_components('/foo/'), [('', '/'), ('foo', '/foo')])
61 WVPASSEQ(path_components('/foo/bar'),
62 [('', '/'), ('foo', '/foo'), ('bar', '/foo/bar')])
63 WVEXCEPT(Exception, path_components, 'foo')
67 def test_stripped_path_components():
68 WVPASSEQ(stripped_path_components('/', []), [('', '/')])
69 WVPASSEQ(stripped_path_components('/', ['']), [('', '/')])
70 WVPASSEQ(stripped_path_components('/', ['/']), [('', '/')])
71 WVPASSEQ(stripped_path_components('/', ['/foo']), [('', '/')])
72 WVPASSEQ(stripped_path_components('/foo', ['/bar']),
73 [('', '/'), ('foo', '/foo')])
74 WVPASSEQ(stripped_path_components('/foo', ['/foo']), [('', '/foo')])
75 WVPASSEQ(stripped_path_components('/foo/bar', ['/foo']),
76 [('', '/foo'), ('bar', '/foo/bar')])
77 WVPASSEQ(stripped_path_components('/foo/bar', ['/bar', '/foo', '/baz']),
78 [('', '/foo'), ('bar', '/foo/bar')])
79 WVPASSEQ(stripped_path_components('/foo/bar/baz', ['/foo/bar/baz']),
80 [('', '/foo/bar/baz')])
81 WVEXCEPT(Exception, stripped_path_components, 'foo', [])
85 def test_grafted_path_components():
86 WVPASSEQ(grafted_path_components([('/chroot', '/')], '/foo'),
87 [('', '/'), ('foo', '/foo')])
88 WVPASSEQ(grafted_path_components([('/foo/bar', '/')], '/foo/bar/baz/bax'),
90 ('baz', '/foo/bar/baz'),
91 ('bax', '/foo/bar/baz/bax')])
92 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
95 ('bax', '/foo/bar/baz'),
96 ('1', '/foo/bar/baz/1'),
97 ('2', '/foo/bar/baz/1/2')])
98 WVPASSEQ(grafted_path_components([('/foo', '/bar/baz/bax')],
104 ('bar', '/foo/bar')])
105 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
107 [('', None), ('a', None), ('b', None), ('c', '/foo/bar/baz')])
108 WVPASSEQ(grafted_path_components([('/', '/a/b/c/')], '/foo/bar'),
109 [('', None), ('a', None), ('b', None), ('c', '/'),
110 ('foo', '/foo'), ('bar', '/foo/bar')])
111 WVEXCEPT(Exception, grafted_path_components, 'foo', [])
116 x = readpipe(['echo', '42'])
119 readpipe(['bash', '-c', 'exit 42'])
120 except Exception, ex:
121 WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
125 def test_batchpipe():
126 for chunk in batchpipe(['echo'], []):
129 for chunk in batchpipe(['echo'], ['42']):
131 WVPASSEQ(out, '42\n')
133 batchpipe(['bash', '-c'], ['exit 42'])
134 except Exception, ex:
135 WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
136 args = [str(x) for x in range(6)]
137 # Force batchpipe to break the args into batches of 3. This
138 # approach assumes all args are the same length.
140 helpers._argmax_base(['echo']) + helpers._argmax_args_size(args[:3])
141 batches = batchpipe(['echo'], args, arg_max=arg_max)
142 WVPASSEQ(next(batches), '0 1 2\n')
143 WVPASSEQ(next(batches), '3 4 5\n')
144 WVPASSEQ(next(batches, None), None)
145 batches = batchpipe(['echo'], [str(x) for x in range(5)], arg_max=arg_max)
146 WVPASSEQ(next(batches), '0 1 2\n')
147 WVPASSEQ(next(batches), '3 4\n')
148 WVPASSEQ(next(batches, None), None)
152 def test_atomically_replaced_file():
153 target_file = os.path.join(tempfile.gettempdir(),
156 with atomically_replaced_file(target_file, mode='w') as f:
158 WVPASSEQ(f.mode, 'w')
159 f = open(target_file, 'r')
160 WVPASSEQ(f.read(), 'asdf')
162 with atomically_replaced_file(target_file, mode='wb') as f:
163 f.write(os.urandom(20))
164 WVPASSEQ(f.mode, 'wb')