]> arthur.barton.de Git - bup.git/blob - lib/bup/t/thelpers.py
Merge branch 'next' into 'master'
[bup.git] / lib / bup / t / thelpers.py
1 import os
2 from bup.helpers import *
3 from wvtest import *
4
5 @wvtest
6 def test_parse_num():
7     pn = parse_num
8     WVPASSEQ(pn('1'), 1)
9     WVPASSEQ(pn('0'), 0)
10     WVPASSEQ(pn('1.5k'), 1536)
11     WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
12     WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
13     WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
14
15 @wvtest
16 def test_strip_path():
17     prefix = "/var/backup/daily.0/localhost"
18     empty_prefix = ""
19     non_matching_prefix = "/home"
20     path = "/var/backup/daily.0/localhost/etc/"
21
22     WVPASSEQ(strip_path(prefix, path), '/etc')
23     WVPASSEQ(strip_path(empty_prefix, path), path)
24     WVPASSEQ(strip_path(non_matching_prefix, path), path)
25     WVEXCEPT(Exception, strip_path, None, path)
26
27 @wvtest
28 def test_strip_base_path():
29     path = "/var/backup/daily.0/localhost/etc/"
30     base_paths = ["/var", "/var/backup", "/var/backup/daily.0/localhost"]
31     WVPASSEQ(strip_base_path(path, base_paths), '/etc')
32
33 @wvtest
34 def test_strip_symlinked_base_path():
35     tmpdir = os.path.join(os.getcwd(),"test_strip_symlinked_base_path.tmp")
36     symlink_src = os.path.join(tmpdir, "private", "var")
37     symlink_dst = os.path.join(tmpdir, "var")
38     path = os.path.join(symlink_dst, "a")
39
40     os.mkdir(tmpdir)
41     os.mkdir(os.path.join(tmpdir, "private"))
42     os.mkdir(symlink_src)
43     os.symlink(symlink_src, symlink_dst)
44
45     result = strip_base_path(path, [symlink_dst])
46
47     os.remove(symlink_dst)
48     os.rmdir(symlink_src)
49     os.rmdir(os.path.join(tmpdir, "private"))
50     os.rmdir(tmpdir)
51
52     WVPASSEQ(result, "/a")
53
54 @wvtest
55 def test_graft_path():
56     middle_matching_old_path = "/user"
57     non_matching_old_path = "/usr"
58     matching_old_path = "/home"
59     matching_full_path = "/home/user"
60     new_path = "/opt"
61
62     all_graft_points = [(middle_matching_old_path, new_path),
63                         (non_matching_old_path, new_path),
64                         (matching_old_path, new_path)]
65
66     path = "/home/user/"
67
68     WVPASSEQ(graft_path([(middle_matching_old_path, new_path)], path),
69                         "/home/user")
70     WVPASSEQ(graft_path([(non_matching_old_path, new_path)], path),
71                         "/home/user")
72     WVPASSEQ(graft_path([(matching_old_path, new_path)], path), "/opt/user")
73     WVPASSEQ(graft_path(all_graft_points, path), "/opt/user")
74     WVPASSEQ(graft_path([(matching_full_path, new_path)], path),
75                         "/opt")