]> arthur.barton.de Git - bup.git/blob - lib/bup/t/thelpers.py
Merge remote branch 'origin/master' into meta
[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_strip_path():
26     prefix = "/var/backup/daily.0/localhost"
27     empty_prefix = ""
28     non_matching_prefix = "/home"
29     path = "/var/backup/daily.0/localhost/etc/"
30
31     WVPASSEQ(strip_path(prefix, path), '/etc')
32     WVPASSEQ(strip_path(empty_prefix, path), path)
33     WVPASSEQ(strip_path(non_matching_prefix, path), path)
34     WVEXCEPT(Exception, strip_path, None, path)
35
36 @wvtest
37 def test_strip_base_path():
38     path = "/var/backup/daily.0/localhost/etc/"
39     base_paths = ["/var", "/var/backup", "/var/backup/daily.0/localhost"]
40     WVPASSEQ(strip_base_path(path, base_paths), '/etc')
41
42 @wvtest
43 def test_strip_symlinked_base_path():
44     tmpdir = os.path.join(os.getcwd(),"test_strip_symlinked_base_path.tmp")
45     symlink_src = os.path.join(tmpdir, "private", "var")
46     symlink_dst = os.path.join(tmpdir, "var")
47     path = os.path.join(symlink_dst, "a")
48
49     os.mkdir(tmpdir)
50     os.mkdir(os.path.join(tmpdir, "private"))
51     os.mkdir(symlink_src)
52     os.symlink(symlink_src, symlink_dst)
53
54     result = strip_base_path(path, [symlink_dst])
55
56     os.remove(symlink_dst)
57     os.rmdir(symlink_src)
58     os.rmdir(os.path.join(tmpdir, "private"))
59     os.rmdir(tmpdir)
60
61     WVPASSEQ(result, "/a")
62
63 @wvtest
64 def test_graft_path():
65     middle_matching_old_path = "/user"
66     non_matching_old_path = "/usr"
67     matching_old_path = "/home"
68     matching_full_path = "/home/user"
69     new_path = "/opt"
70
71     all_graft_points = [(middle_matching_old_path, new_path),
72                         (non_matching_old_path, new_path),
73                         (matching_old_path, new_path)]
74
75     path = "/home/user/"
76
77     WVPASSEQ(graft_path([(middle_matching_old_path, new_path)], path),
78                         "/home/user")
79     WVPASSEQ(graft_path([(non_matching_old_path, new_path)], path),
80                         "/home/user")
81     WVPASSEQ(graft_path([(matching_old_path, new_path)], path), "/opt/user")
82     WVPASSEQ(graft_path(all_graft_points, path), "/opt/user")
83     WVPASSEQ(graft_path([(matching_full_path, new_path)], path),
84                         "/opt")