]> arthur.barton.de Git - bup.git/blob - lib/bup/t/thelpers.py
helpers.py: use returncode to get the subprocess exit code in readpipe().
[bup.git] / lib / bup / t / thelpers.py
1 import math
2 import os
3 import bup._helpers as _helpers
4 from bup.helpers import *
5 from wvtest import *
6
7 @wvtest
8 def test_parse_num():
9     pn = parse_num
10     WVPASSEQ(pn('1'), 1)
11     WVPASSEQ(pn('0'), 0)
12     WVPASSEQ(pn('1.5k'), 1536)
13     WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
14     WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
15     WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
16
17 @wvtest
18 def test_detect_fakeroot():
19     if os.getenv('FAKEROOTKEY'):
20         WVPASS(detect_fakeroot())
21     else:
22         WVPASS(not detect_fakeroot())
23
24 @wvtest
25 def test_path_components():
26     WVPASSEQ(path_components('/'), [('', '/')])
27     WVPASSEQ(path_components('/foo'), [('', '/'), ('foo', '/foo')])
28     WVPASSEQ(path_components('/foo/'), [('', '/'), ('foo', '/foo')])
29     WVPASSEQ(path_components('/foo/bar'),
30              [('', '/'), ('foo', '/foo'), ('bar', '/foo/bar')])
31     WVEXCEPT(Exception, path_components, 'foo')
32
33
34 @wvtest
35 def test_stripped_path_components():
36     WVPASSEQ(stripped_path_components('/', []), [('', '/')])
37     WVPASSEQ(stripped_path_components('/', ['']), [('', '/')])
38     WVPASSEQ(stripped_path_components('/', ['/']), [('', '/')])
39     WVPASSEQ(stripped_path_components('/', ['/foo']), [('', '/')])
40     WVPASSEQ(stripped_path_components('/foo', ['/bar']),
41              [('', '/'), ('foo', '/foo')])
42     WVPASSEQ(stripped_path_components('/foo', ['/foo']), [('', '/foo')])
43     WVPASSEQ(stripped_path_components('/foo/bar', ['/foo']),
44              [('', '/foo'), ('bar', '/foo/bar')])
45     WVPASSEQ(stripped_path_components('/foo/bar', ['/bar', '/foo', '/baz']),
46              [('', '/foo'), ('bar', '/foo/bar')])
47     WVPASSEQ(stripped_path_components('/foo/bar/baz', ['/foo/bar/baz']),
48              [('', '/foo/bar/baz')])
49     WVEXCEPT(Exception, stripped_path_components, 'foo', [])
50
51
52 @wvtest
53 def test_grafted_path_components():
54     WVPASSEQ(grafted_path_components([('/chroot', '/')], '/foo'),
55              [('', '/'), ('foo', '/foo')])
56     WVPASSEQ(grafted_path_components([('/foo/bar', '/')], '/foo/bar/baz/bax'),
57              [('', '/foo/bar'),
58               ('baz', '/foo/bar/baz'),
59               ('bax', '/foo/bar/baz/bax')])
60     WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
61                                      '/foo/bar/baz/1/2'),
62              [('', None),
63               ('bax', '/foo/bar/baz'),
64               ('1', '/foo/bar/baz/1'),
65               ('2', '/foo/bar/baz/1/2')])
66     WVPASSEQ(grafted_path_components([('/foo', '/bar/baz/bax')],
67                                      '/foo/bar'),
68              [('', None),
69               ('bar', None),
70               ('baz', None),
71               ('bax', '/foo'),
72               ('bar', '/foo/bar')])
73     WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
74                                      '/foo/bar/baz'),
75              [('', None), ('a', None), ('b', None), ('c', '/foo/bar/baz')])
76     WVPASSEQ(grafted_path_components([('/', '/a/b/c/')], '/foo/bar'),
77              [('', None), ('a', None), ('b', None), ('c', '/'),
78               ('foo', '/foo'), ('bar', '/foo/bar')])
79     WVEXCEPT(Exception, grafted_path_components, 'foo', [])
80
81
82 @wvtest
83 def test_readpipe():
84     x = readpipe(['echo', '42'])
85     WVPASSEQ(x, '42\n')
86     try:
87         readpipe(['bash', '-c', 'exit 42'])
88     except Exception, ex:
89         WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")