]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/t/tgit.py
Use CatPipe, not show, in git_commit_dates()
[bup.git] / lib / bup / t / tgit.py
index b564736879756bff7c5e7cdabced646a7e23683a..3a8378b0275605744a361fb1cf34ca7bd21c617e 100644 (file)
@@ -3,6 +3,8 @@ from bup import git
 from bup.helpers import *
 from wvtest import *
 
+bup_tmp = os.path.realpath('../../../t/tmp')
+mkdirp(bup_tmp)
 
 @wvtest
 def testmangle():
@@ -50,15 +52,18 @@ def testencode():
 
 @wvtest
 def testpacks():
-    subprocess.call(['rm','-rf', 'pybuptest.tmp'])
-    git.init_repo('pybuptest.tmp')
+    initial_failures = wvfailure_count()
+    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
+    os.environ['BUP_MAIN_EXE'] = bupmain = '../../../bup'
+    os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
+    git.init_repo(bupdir)
     git.verbose = 1
 
     w = git.PackWriter()
     w.new_blob(os.urandom(100))
     w.new_blob(os.urandom(100))
     w.abort()
-    
+
     w = git.PackWriter()
     hashes = []
     nobj = 1000
@@ -84,17 +89,19 @@ def testpacks():
 
     WVFAIL(r.find_offset('\0'*20))
 
-    r = git.PackIdxList('pybuptest.tmp/objects/pack')
+    r = git.PackIdxList(bupdir + '/objects/pack')
     WVPASS(r.exists(hashes[5]))
     WVPASS(r.exists(hashes[6]))
     WVFAIL(r.exists('\0'*20))
-
+    if wvfailure_count() == initial_failures:
+        subprocess.call(['rm', '-rf', tmpdir])
 
 @wvtest
 def test_pack_name_lookup():
+    initial_failures = wvfailure_count()
+    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
     os.environ['BUP_MAIN_EXE'] = bupmain = '../../../bup'
-    os.environ['BUP_DIR'] = bupdir = 'pybuptest.tmp'
-    subprocess.call(['rm','-rf', bupdir])
+    os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
     git.init_repo(bupdir)
     git.verbose = 1
     packdir = git.repo('objects/pack')
@@ -114,10 +121,17 @@ def test_pack_name_lookup():
     for e,idxname in enumerate(idxnames):
         for i in range(e*2, (e+1)*2):
             WVPASSEQ(r.exists(hashes[i], want_source=True), idxname)
+    if wvfailure_count() == initial_failures:
+        subprocess.call(['rm', '-rf', tmpdir])
 
 
 @wvtest
 def test_long_index():
+    initial_failures = wvfailure_count()
+    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
+    os.environ['BUP_MAIN_EXE'] = bupmain = '../../../bup'
+    os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
+    git.init_repo(bupdir)
     w = git.PackWriter()
     obj_bin = struct.pack('!IIIII',
             0x00112233, 0x44556677, 0x88990011, 0x22334455, 0x66778899)
@@ -139,28 +153,99 @@ def test_long_index():
     WVPASSEQ(i.find_offset(obj_bin), 0xfffffffff)
     WVPASSEQ(i.find_offset(obj2_bin), 0xffffffffff)
     WVPASSEQ(i.find_offset(obj3_bin), 0xff)
-    os.remove(name)
+    if wvfailure_count() == initial_failures:
+        os.remove(name)
+        subprocess.call(['rm', '-rf', tmpdir])
 
 
 @wvtest
 def test_check_repo_or_die():
-    git.check_repo_or_die()
-    WVPASS('check_repo_or_die')  # if we reach this point the call above passed
-
-    os.rename('pybuptest.tmp/objects/pack', 'pybuptest.tmp/objects/pack.tmp')
-    open('pybuptest.tmp/objects/pack', 'w').close()
+    initial_failures = wvfailure_count()
+    orig_cwd = os.getcwd()
+    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
+    os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
     try:
+        os.chdir(tmpdir)
+        git.init_repo(bupdir)
         git.check_repo_or_die()
-    except SystemExit, e:
-        WVPASSEQ(e.code, 14)
-    else:
-        WVFAIL()
-    os.unlink('pybuptest.tmp/objects/pack')
-    os.rename('pybuptest.tmp/objects/pack.tmp', 'pybuptest.tmp/objects/pack')
+        WVPASS('check_repo_or_die')  # if we reach this point the call above passed
+
+        os.rename(bupdir + '/objects/pack', bupdir + '/objects/pack.tmp')
+        open(bupdir + '/objects/pack', 'w').close()
+        try:
+            git.check_repo_or_die()
+        except SystemExit, e:
+            WVPASSEQ(e.code, 14)
+        else:
+            WVFAIL()
+        os.unlink(bupdir + '/objects/pack')
+        os.rename(bupdir + '/objects/pack.tmp', bupdir + '/objects/pack')
+
+        try:
+            git.check_repo_or_die('nonexistantbup.tmp')
+        except SystemExit, e:
+            WVPASSEQ(e.code, 15)
+        else:
+            WVFAIL()
+    finally:
+        os.chdir(orig_cwd)
+    if wvfailure_count() == initial_failures:
+        subprocess.call(['rm', '-rf', tmpdir])
 
+
+@wvtest
+def test_commit_parsing():
+    def showval(commit, val):
+        return readpipe(['git', 'show', '-s',
+                         '--pretty=format:%s' % val, commit]).strip()
+    initial_failures = wvfailure_count()
+    orig_cwd = os.getcwd()
+    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
+    workdir = tmpdir + "/work"
+    repodir = workdir + '/.git'
     try:
-        git.check_repo_or_die('nonexistantbup.tmp')
-    except SystemExit, e:
-        WVPASSEQ(e.code, 15)
-    else:
-        WVFAIL()
+        readpipe(['git', 'init', workdir])
+        os.environ['GIT_DIR'] = os.environ['BUP_DIR'] = repodir
+        git.check_repo_or_die(repodir)
+        os.chdir(workdir)
+        with open('foo', 'w') as f:
+            print >> f, 'bar'
+        readpipe(['git', 'add', '.'])
+        readpipe(['git', 'commit', '-am', 'Do something',
+                  '--author', 'Someone <someone@somewhere>',
+                  '--date', 'Sat Oct 3 19:48:49 2009 -0400'])
+        commit = readpipe(['git', 'show-ref', '-s', 'master']).strip()
+        parents = showval(commit, '%P')
+        tree = showval(commit, '%T')
+        cname = showval(commit, '%cn')
+        cmail = showval(commit, '%ce')
+        cdate = showval(commit, '%ct')
+        coffs = showval(commit, '%ci')
+        coffs = coffs[-5:]
+        coff = (int(coffs[-4:-2]) * 60 * 60) + (int(coffs[-2:]) * 60)
+        if coffs[-5] == '-':
+            coff = - coff
+        commit_items = git.get_commit_items(commit, git.cp())
+        WVPASSEQ(commit_items.parents, [])
+        WVPASSEQ(commit_items.tree, tree)
+        WVPASSEQ(commit_items.author_name, 'Someone')
+        WVPASSEQ(commit_items.author_mail, 'someone@somewhere')
+        WVPASSEQ(commit_items.author_sec, 1254613729)
+        WVPASSEQ(commit_items.author_offset, -(4 * 60 * 60))
+        WVPASSEQ(commit_items.committer_name, cname)
+        WVPASSEQ(commit_items.committer_mail, cmail)
+        WVPASSEQ(commit_items.committer_sec, int(cdate))
+        WVPASSEQ(commit_items.committer_offset, coff)
+        WVPASSEQ(commit_items.message, 'Do something\n')
+        with open('bar', 'w') as f:
+            print >> f, 'baz'
+        readpipe(['git', 'add', '.'])
+        readpipe(['git', 'commit', '-am', 'Do something else'])
+        child = readpipe(['git', 'show-ref', '-s', 'master']).strip()
+        parents = showval(child, '%P')
+        commit_items = git.get_commit_items(child, git.cp())
+        WVPASSEQ(commit_items.parents, [commit])
+    finally:
+        os.chdir(orig_cwd)
+    if wvfailure_count() == initial_failures:
+        subprocess.call(['rm', '-rf', tmpdir])