]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/t/thelpers.py
helpers.py: use returncode to get the subprocess exit code in readpipe().
[bup.git] / lib / bup / t / thelpers.py
index 89cccdaebad7214472c319e6206eb6854b209385..fd041ffb792e3586259581dd70719ccf7b4c867a 100644 (file)
@@ -1,4 +1,6 @@
+import math
 import os
+import bup._helpers as _helpers
 from bup.helpers import *
 from wvtest import *
 
@@ -13,63 +15,75 @@ def test_parse_num():
     WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
 
 @wvtest
-def test_strip_path():
-    prefix = "/var/backup/daily.0/localhost"
-    empty_prefix = ""
-    non_matching_prefix = "/home"
-    path = "/var/backup/daily.0/localhost/etc/"
-
-    WVPASSEQ(strip_path(prefix, path), '/etc')
-    WVPASSEQ(strip_path(empty_prefix, path), path)
-    WVPASSEQ(strip_path(non_matching_prefix, path), path)
-    WVEXCEPT(Exception, strip_path, None, path)
-
-@wvtest
-def test_strip_base_path():
-    path = "/var/backup/daily.0/localhost/etc/"
-    base_paths = ["/var", "/var/backup", "/var/backup/daily.0/localhost"]
-    WVPASSEQ(strip_base_path(path, base_paths), '/etc')
+def test_detect_fakeroot():
+    if os.getenv('FAKEROOTKEY'):
+        WVPASS(detect_fakeroot())
+    else:
+        WVPASS(not detect_fakeroot())
 
 @wvtest
-def test_strip_symlinked_base_path():
-    tmpdir = os.path.join(os.getcwd(),"test_strip_symlinked_base_path.tmp")
-    symlink_src = os.path.join(tmpdir, "private", "var")
-    symlink_dst = os.path.join(tmpdir, "var")
-    path = os.path.join(symlink_dst, "a")
-
-    os.mkdir(tmpdir)
-    os.mkdir(os.path.join(tmpdir, "private"))
-    os.mkdir(symlink_src)
-    os.symlink(symlink_src, symlink_dst)
+def test_path_components():
+    WVPASSEQ(path_components('/'), [('', '/')])
+    WVPASSEQ(path_components('/foo'), [('', '/'), ('foo', '/foo')])
+    WVPASSEQ(path_components('/foo/'), [('', '/'), ('foo', '/foo')])
+    WVPASSEQ(path_components('/foo/bar'),
+             [('', '/'), ('foo', '/foo'), ('bar', '/foo/bar')])
+    WVEXCEPT(Exception, path_components, 'foo')
 
-    result = strip_base_path(path, [symlink_dst])
 
-    os.remove(symlink_dst)
-    os.rmdir(symlink_src)
-    os.rmdir(os.path.join(tmpdir, "private"))
-    os.rmdir(tmpdir)
+@wvtest
+def test_stripped_path_components():
+    WVPASSEQ(stripped_path_components('/', []), [('', '/')])
+    WVPASSEQ(stripped_path_components('/', ['']), [('', '/')])
+    WVPASSEQ(stripped_path_components('/', ['/']), [('', '/')])
+    WVPASSEQ(stripped_path_components('/', ['/foo']), [('', '/')])
+    WVPASSEQ(stripped_path_components('/foo', ['/bar']),
+             [('', '/'), ('foo', '/foo')])
+    WVPASSEQ(stripped_path_components('/foo', ['/foo']), [('', '/foo')])
+    WVPASSEQ(stripped_path_components('/foo/bar', ['/foo']),
+             [('', '/foo'), ('bar', '/foo/bar')])
+    WVPASSEQ(stripped_path_components('/foo/bar', ['/bar', '/foo', '/baz']),
+             [('', '/foo'), ('bar', '/foo/bar')])
+    WVPASSEQ(stripped_path_components('/foo/bar/baz', ['/foo/bar/baz']),
+             [('', '/foo/bar/baz')])
+    WVEXCEPT(Exception, stripped_path_components, 'foo', [])
 
-    WVPASSEQ(result, "/a")
 
 @wvtest
-def test_graft_path():
-    middle_matching_old_path = "/user"
-    non_matching_old_path = "/usr"
-    matching_old_path = "/home"
-    matching_full_path = "/home/user"
-    new_path = "/opt"
-
-    all_graft_points = [(middle_matching_old_path, new_path),
-                        (non_matching_old_path, new_path),
-                        (matching_old_path, new_path)]
+def test_grafted_path_components():
+    WVPASSEQ(grafted_path_components([('/chroot', '/')], '/foo'),
+             [('', '/'), ('foo', '/foo')])
+    WVPASSEQ(grafted_path_components([('/foo/bar', '/')], '/foo/bar/baz/bax'),
+             [('', '/foo/bar'),
+              ('baz', '/foo/bar/baz'),
+              ('bax', '/foo/bar/baz/bax')])
+    WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/bax')],
+                                     '/foo/bar/baz/1/2'),
+             [('', None),
+              ('bax', '/foo/bar/baz'),
+              ('1', '/foo/bar/baz/1'),
+              ('2', '/foo/bar/baz/1/2')])
+    WVPASSEQ(grafted_path_components([('/foo', '/bar/baz/bax')],
+                                     '/foo/bar'),
+             [('', None),
+              ('bar', None),
+              ('baz', None),
+              ('bax', '/foo'),
+              ('bar', '/foo/bar')])
+    WVPASSEQ(grafted_path_components([('/foo/bar/baz', '/a/b/c')],
+                                     '/foo/bar/baz'),
+             [('', None), ('a', None), ('b', None), ('c', '/foo/bar/baz')])
+    WVPASSEQ(grafted_path_components([('/', '/a/b/c/')], '/foo/bar'),
+             [('', None), ('a', None), ('b', None), ('c', '/'),
+              ('foo', '/foo'), ('bar', '/foo/bar')])
+    WVEXCEPT(Exception, grafted_path_components, 'foo', [])
 
-    path = "/home/user/"
 
-    WVPASSEQ(graft_path([(middle_matching_old_path, new_path)], path),
-                        "/home/user")
-    WVPASSEQ(graft_path([(non_matching_old_path, new_path)], path),
-                        "/home/user")
-    WVPASSEQ(graft_path([(matching_old_path, new_path)], path), "/opt/user")
-    WVPASSEQ(graft_path(all_graft_points, path), "/opt/user")
-    WVPASSEQ(graft_path([(matching_full_path, new_path)], path),
-                        "/opt")
+@wvtest
+def test_readpipe():
+    x = readpipe(['echo', '42'])
+    WVPASSEQ(x, '42\n')
+    try:
+        readpipe(['bash', '-c', 'exit 42'])
+    except Exception, ex:
+        WVPASSEQ(str(ex), "subprocess 'bash -c exit 42' failed with status 42")