7 import bup._helpers as _helpers
8 from bup.helpers import *
11 bup_tmp = os.path.realpath('../../../t/tmp')
16 # Test whatever you end up with for next() after import '*'.
17 WVPASSEQ(next(iter([]), None), None)
19 WVPASSEQ(next(x, None), 1)
20 WVPASSEQ(next(x, None), None)
22 WVPASSEQ(next(x, 'x'), 1)
23 WVPASSEQ(next(x, 'x'), 'x')
24 WVEXCEPT(StopIteration, next, iter([]))
27 WVEXCEPT(StopIteration, next, x)
31 def test_fallback_next():
34 next = helpers._fallback_next
46 WVPASSEQ(pn('1.5k'), 1536)
47 WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
48 WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
49 WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
52 def test_detect_fakeroot():
53 if os.getenv('FAKEROOTKEY'):
54 WVPASS(detect_fakeroot())
56 WVPASS(not detect_fakeroot())
59 def test_path_components():
60 WVPASSEQ(path_components('/'), [('', '/')])
61 WVPASSEQ(path_components('/foo'), [('', '/'), ('foo', '/foo')])
62 WVPASSEQ(path_components('/foo/'), [('', '/'), ('foo', '/foo')])
63 WVPASSEQ(path_components('/foo/bar'),
64 [('', '/'), ('foo', '/foo'), ('bar', '/foo/bar')])
65 WVEXCEPT(Exception, path_components, 'foo')
69 def test_stripped_path_components():
70 WVPASSEQ(stripped_path_components('/', []), [('', '/')])
71 WVPASSEQ(stripped_path_components('/', ['']), [('', '/')])
72 WVPASSEQ(stripped_path_components('/', ['/']), [('', '/')])
73 WVPASSEQ(stripped_path_components('/', ['/foo']), [('', '/')])
74 WVPASSEQ(stripped_path_components('/foo', ['/bar']),
75 [('', '/'), ('foo', '/foo')])
76 WVPASSEQ(stripped_path_components('/foo', ['/foo']), [('', '/foo')])
77 WVPASSEQ(stripped_path_components('/foo/bar', ['/foo']),
78 [('', '/foo'), ('bar', '/foo/bar')])
79 WVPASSEQ(stripped_path_components('/foo/bar', ['/bar', '/foo', '/baz']),
80 [('', '/foo'), ('bar', '/foo/bar')])
81 WVPASSEQ(stripped_path_components('/foo/bar/baz', ['/foo/bar/baz']),
82 [('', '/foo/bar/baz')])
83 WVEXCEPT(Exception, stripped_path_components, 'foo', [])
87 def test_grafted_path_components():
88 WVPASSEQ(grafted_path_components([('/chroot', '/')], '/foo'),
89 [('', '/'), ('foo', '/foo')])
90 WVPASSEQ(grafted_path_components([('/foo/bar', '/')], '/foo/bar/baz/bax'),
92 ('baz', '/foo/bar/baz'),
93 ('bax', '/foo/bar/baz/bax')])
94 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
97 ('bax', '/foo/bar/baz'),
98 ('1', '/foo/bar/baz/1'),
99 ('2', '/foo/bar/baz/1/2')])
100 WVPASSEQ(grafted_path_components([('/foo', '/bar/baz/bax')],
106 ('bar', '/foo/bar')])
107 WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
109 [('', None), ('a', None), ('b', None), ('c', '/foo/bar/baz')])
110 WVPASSEQ(grafted_path_components([('/', '/a/b/c/')], '/foo/bar'),
111 [('', None), ('a', None), ('b', None), ('c', '/'),
112 ('foo', '/foo'), ('bar', '/foo/bar')])
113 WVEXCEPT(Exception, grafted_path_components, 'foo', [])
118 x = readpipe(['echo', '42'])
121 readpipe(['bash', '-c', 'exit 42'])
122 except Exception, ex:
123 WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
127 def test_batchpipe():
128 for chunk in batchpipe(['echo'], []):
131 for chunk in batchpipe(['echo'], ['42']):
133 WVPASSEQ(out, '42\n')
135 batchpipe(['bash', '-c'], ['exit 42'])
136 except Exception, ex:
137 WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")
138 args = [str(x) for x in range(6)]
139 # Force batchpipe to break the args into batches of 3. This
140 # approach assumes all args are the same length.
142 helpers._argmax_base(['echo']) + helpers._argmax_args_size(args[:3])
143 batches = batchpipe(['echo'], args, arg_max=arg_max)
144 WVPASSEQ(next(batches), '0 1 2\n')
145 WVPASSEQ(next(batches), '3 4 5\n')
146 WVPASSEQ(next(batches, None), None)
147 batches = batchpipe(['echo'], [str(x) for x in range(5)], arg_max=arg_max)
148 WVPASSEQ(next(batches), '0 1 2\n')
149 WVPASSEQ(next(batches), '3 4\n')
150 WVPASSEQ(next(batches, None), None)
154 def test_atomically_replaced_file():
155 tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-thelper-')
156 target_file = os.path.join(tmpdir, 'test-atomic-write')
157 initial_failures = wvfailure_count()
159 with atomically_replaced_file(target_file, mode='w') as f:
161 WVPASSEQ(f.mode, 'w')
162 f = open(target_file, 'r')
163 WVPASSEQ(f.read(), 'asdf')
166 with atomically_replaced_file(target_file, mode='w') as f:
171 with open(target_file) as f:
172 WVPASSEQ(f.read(), 'asdf')
174 with atomically_replaced_file(target_file, mode='wb') as f:
175 f.write(os.urandom(20))
176 WVPASSEQ(f.mode, 'wb')
178 if wvfailure_count() == initial_failures:
179 subprocess.call(['rm', '-rf', tmpdir])