diff options
Diffstat (limited to 'morphlib/gitdir_tests.py')
-rw-r--r-- | morphlib/gitdir_tests.py | 315 |
1 files changed, 306 insertions, 9 deletions
diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 803f1b3e..21a6b5b8 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -16,6 +16,7 @@ # =*= License: GPL-2 =*= +import datetime import os import shutil import tempfile @@ -54,15 +55,11 @@ class GitDirectoryTests(unittest.TestCase): gitdir.set_config('foo.bar', 'yoyo') self.assertEqual(gitdir.get_config('foo.bar'), 'yoyo') - def test_sets_remote(self): + def test_gets_index(self): os.mkdir(self.dirname) gitdir = morphlib.gitdir.init(self.dirname) - self.assertEqual(gitdir.get_remote_fetch_url('origin'), None) + self.assertIsInstance(gitdir.get_index(), morphlib.gitindex.GitIndex) - gitdir._runcmd(['git', 'remote', 'add', 'origin', 'foobar']) - url = 'git://git.example.com/foo' - gitdir.set_remote_fetch_url('origin', url) - self.assertEqual(gitdir.get_remote_fetch_url('origin'), url) class GitDirectoryContentsTests(unittest.TestCase): @@ -148,7 +145,307 @@ class GitDirectoryContentsTests(unittest.TestCase): gd.checkout('foo') self.assertEqual(gd.HEAD, 'foo') - def test_uncommitted_changes(self): + def test_resolve_ref(self): + # Just tests that you get an object IDs back and that the + # commit and tree IDs are different, since checking the actual + # value of the commit requires foreknowledge of the result or + # re-implementing the body in the test. + gd = morphlib.gitdir.GitDirectory(self.dirname) + commit = gd.resolve_ref_to_commit(gd.HEAD) + self.assertEqual(len(commit), 40) + tree = gd.resolve_ref_to_tree(gd.HEAD) + self.assertEqual(len(tree), 40) + self.assertNotEqual(commit, tree) + + def test_store_blob_with_string(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + sha1 = gd.store_blob('test string') + self.assertEqual('test string', gd.get_blob_contents(sha1)) + + def test_store_blob_with_file(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + with open(os.path.join(self.tempdir, 'blob'), 'w') as f: + f.write('test string') + with open(os.path.join(self.tempdir, 'blob'), 'r') as f: + sha1 = gd.store_blob(f) + self.assertEqual('test string', gd.get_blob_contents(sha1)) + + def test_commit_tree(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + parent = gd.resolve_ref_to_commit(gd.HEAD) + tree = gd.resolve_ref_to_tree(parent) + aname = 'Author Name' + aemail = 'author@email' + cname = 'Committer Name' + cemail = 'committer@email' + pseudo_now = datetime.datetime.fromtimestamp(683074800) + + now_str = "683074800" + message= 'MESSAGE' + expected = [ + "tree %(tree)s", + "parent %(parent)s", + "author %(aname)s <%(aemail)s> %(now_str)s +0000", + "committer %(cname)s <%(cemail)s> %(now_str)s +0000", + "", + "%(message)s", + "", + ] + expected = [l % locals() for l in expected] + commit = gd.commit_tree(tree, parent, message=message, + committer_name=cname, + committer_email=cemail, + committer_date=pseudo_now, + author_name=aname, + author_email=aemail, + author_date=pseudo_now, + ) + self.assertEqual(expected, gd.get_commit_contents(commit).split('\n')) + + +class GitDirectoryRefTwiddlingTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.dirname = os.path.join(self.tempdir, 'foo') + os.mkdir(self.dirname) + gd = morphlib.gitdir.init(self.dirname) + with open(os.path.join(self.dirname, 'foo'), 'w') as f: + f.write('dummy text\n') + gd._runcmd(['git', 'add', '.']) + gd._runcmd(['git', 'commit', '-m', 'Initial commit']) + # Add a second commit for update_ref test, so it has another + # commit to roll back from + with open(os.path.join(self.dirname, 'bar'), 'w') as f: + f.write('dummy text\n') + gd._runcmd(['git', 'add', '.']) + gd._runcmd(['git', 'commit', '-m', 'Second commit']) + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def test_expects_sha1s(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + self.assertRaises(morphlib.gitdir.ExpectedSha1Error, + gd.add_ref, 'refs/heads/foo', 'HEAD') + self.assertRaises(morphlib.gitdir.ExpectedSha1Error, + gd.update_ref, 'refs/heads/foo', 'HEAD', 'HEAD') + self.assertRaises(morphlib.gitdir.ExpectedSha1Error, + gd.update_ref, 'refs/heads/master', + gd._rev_parse(gd.HEAD), 'HEAD') + self.assertRaises(morphlib.gitdir.ExpectedSha1Error, + gd.update_ref, 'refs/heads/master', + 'HEAD', gd._rev_parse(gd.HEAD)) + self.assertRaises(morphlib.gitdir.ExpectedSha1Error, + gd.delete_ref, 'refs/heads/master', 'HEAD') + + def test_add_ref(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + head_commit = gd.resolve_ref_to_commit(gd.HEAD) + gd.add_ref('refs/heads/foo', head_commit) + self.assertEqual(gd.resolve_ref_to_commit('refs/heads/foo'), + head_commit) + + def test_add_ref_fail(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + head_commit = gd.resolve_ref_to_commit('refs/heads/master') + self.assertRaises(morphlib.gitdir.RefAddError, + gd.add_ref, 'refs/heads/master', head_commit) + + def test_update_ref(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + head_commit = gd._rev_parse('refs/heads/master') + prev_commit = gd._rev_parse('refs/heads/master^') + gd.update_ref('refs/heads/master', prev_commit, head_commit) + self.assertEqual(gd._rev_parse('refs/heads/master'), prev_commit) + + def test_update_ref_fail(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + head_commit = gd._rev_parse('refs/heads/master') + prev_commit = gd._rev_parse('refs/heads/master^') + gd.delete_ref('refs/heads/master', head_commit) + with self.assertRaises(morphlib.gitdir.RefUpdateError): + gd.update_ref('refs/heads/master', prev_commit, head_commit) + + def test_delete_ref(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + head_commit = gd._rev_parse('refs/heads/master') + gd.delete_ref('refs/heads/master', head_commit) + self.assertRaises(morphlib.gitdir.InvalidRefError, + gd._rev_parse, 'refs/heads/master') + + def test_delete_ref_fail(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + prev_commit = gd._rev_parse('refs/heads/master^') + with self.assertRaises(morphlib.gitdir.RefDeleteError): + gd.delete_ref('refs/heads/master', prev_commit) + + +class GitDirectoryRemoteConfigTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.dirname = os.path.join(self.tempdir, 'foo') + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def test_sets_urls(self): + os.mkdir(self.dirname) + gitdir = morphlib.gitdir.init(self.dirname) + remote = gitdir.get_remote('origin') + self.assertEqual(remote.get_fetch_url(), None) + self.assertEqual(remote.get_push_url(), None) + + gitdir._runcmd(['git', 'remote', 'add', 'origin', 'foobar']) + fetch_url = 'git://git.example.com/foo.git' + push_url = 'ssh://git@git.example.com/foo.git' + remote.set_fetch_url(fetch_url) + remote.set_push_url(push_url) + self.assertEqual(remote.get_fetch_url(), fetch_url) + self.assertEqual(remote.get_push_url(), push_url) + + def test_nascent_remote_fetch(self): + os.mkdir(self.dirname) + gitdir = morphlib.gitdir.init(self.dirname) + remote = gitdir.get_remote(None) + self.assertEqual(remote.get_fetch_url(), None) + self.assertEqual(remote.get_push_url(), None) + + fetch_url = 'git://git.example.com/foo.git' + push_url = 'ssh://git@git.example.com/foo.git' + remote.set_fetch_url(fetch_url) + remote.set_push_url(push_url) + self.assertEqual(remote.get_fetch_url(), fetch_url) + self.assertEqual(remote.get_push_url(), push_url) + + +class RefSpecTests(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + @staticmethod + def refspec(*args, **kwargs): + return morphlib.gitdir.RefSpec(*args, **kwargs) + + def test_input(self): + with self.assertRaises(morphlib.gitdir.InvalidRefSpecError): + morphlib.gitdir.RefSpec() + + def test_rs_from_source(self): + rs = self.refspec(source='master') + self.assertEqual(rs.push_args, ('master:master',)) + + def test_rs_from_target(self): + rs = self.refspec(target='master') + self.assertEqual(rs.push_args, ('%s:master' % ('0' * 40),)) + + def test_rs_with_target_and_source(self): + rs = self.refspec(source='foo', target='master') + self.assertEqual(rs.push_args, ('foo:master',)) + + def test_rs_with_source_and_force(self): + rs = self.refspec('master', force=True) + self.assertEqual(rs.push_args, ('+master:master',)) + + def test_rs_revert_from_source(self): + revert = self.refspec(source='master').revert() + self.assertEqual(revert.push_args, ('%s:master' % ('0' * 40),)) + + def test_rs_revert_inc_require(self): + revert = self.refspec(source='master', require=('beef'*5)).revert() + self.assertEqual(revert.push_args, ('%s:master' % ('beef' * 5),)) + + def test_rs_double_revert(self): + rs = self.refspec(target='master').revert().revert() + self.assertEqual(rs.push_args, ('%s:master' % ('0' * 40),)) + + +class GitDirectoryRemotePushTests(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.mkdtemp() + self.dirname = os.path.join(self.tempdir, 'foo') + os.mkdir(self.dirname) + gd = morphlib.gitdir.init(self.dirname) + with open(os.path.join(self.dirname, 'foo'), 'w') as f: + f.write('dummy text\n') + gd._runcmd(['git', 'add', '.']) + gd._runcmd(['git', 'commit', '-m', 'Initial commit']) + gd._runcmd(['git', 'checkout', '-b', 'foo']) + with open(os.path.join(self.dirname, 'foo'), 'w') as f: + f.write('updated text\n') + gd._runcmd(['git', 'add', '.']) + gd._runcmd(['git', 'commit', '-m', 'Second commit']) + self.mirror = os.path.join(self.tempdir, 'mirror') + gd._runcmd(['git', 'init', '--bare', self.mirror]) + + def tearDown(self): + shutil.rmtree(self.tempdir) + + def test_push_needs_refspecs(self): + gd = morphlib.gitdir.GitDirectory(self.dirname) + r = gd.get_remote() + r.set_push_url(self.mirror) + self.assertRaises(morphlib.gitdir.NoRefspecsError, r.push) + + def test_push_new(self): + push_master = morphlib.gitdir.RefSpec('master') + gd = morphlib.gitdir.GitDirectory(self.dirname) + r = gd.get_remote() + r.set_push_url(self.mirror) + self.assertEqual(sorted(r.push(push_master)), + [('*', 'refs/heads/master', 'refs/heads/master', + '[new branch]', None)]) + + def test_double_push(self): + push_master = morphlib.gitdir.RefSpec('master') + gd = morphlib.gitdir.GitDirectory(self.dirname) + r = gd.get_remote() + r.set_push_url(self.mirror) + r.push(push_master) + self.assertEqual(sorted(r.push(push_master)), + [('=', 'refs/heads/master', 'refs/heads/master', + '[up to date]', None)]) + + def test_push_update(self): + push_master = morphlib.gitdir.RefSpec('master') + push_foo = morphlib.gitdir.RefSpec(source='foo', target='master') + gd = morphlib.gitdir.GitDirectory(self.dirname) + r = gd.get_remote() + r.set_push_url(self.mirror) + r.push(push_master) + flag, ref_from, ref_to, summary, reason = \ + list(r.push(push_foo))[0] + self.assertEqual((flag, ref_from, ref_to), + (' ', 'refs/heads/foo', 'refs/heads/master')) + + def test_rewind_fail(self): + push_master = morphlib.gitdir.RefSpec('master') + push_foo = morphlib.gitdir.RefSpec(source='foo', target='master') + gd = morphlib.gitdir.GitDirectory(self.dirname) + r = gd.get_remote() + r.set_push_url(self.mirror) + r.push(push_foo) + with self.assertRaises(morphlib.gitdir.PushFailureError) as push_fail: + r.push(push_master) + self.assertEqual(sorted(push_fail.exception.results), + [('!', 'refs/heads/master', 'refs/heads/master', + '[rejected]', 'non-fast-forward')]) + + def test_force_push(self): + push_master = morphlib.gitdir.RefSpec('master', force=True) + push_foo = morphlib.gitdir.RefSpec(source='foo', target='master') gd = morphlib.gitdir.GitDirectory(self.dirname) - self.assertEqual(sorted(gd.get_uncommitted_changes()), - [(' D', 'foo', None)]) + r = gd.get_remote() + r.set_push_url(self.mirror) + r.push(push_foo) + flag, ref_from, ref_to, summary, reason = \ + list(r.push(push_master))[0] + self.assertEqual((flag, ref_from, ref_to, reason), + ('+', 'refs/heads/master', 'refs/heads/master', + 'forced update')) |