summaryrefslogtreecommitdiff
path: root/morphlib/gitdir_tests.py
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2013-11-13 14:48:54 +0000
committerRichard Maw <richard.maw@codethink.co.uk>2013-11-22 13:49:26 +0000
commit7e6bdb49957008b3981d570cbe8033627125efed (patch)
tree08884bd4dd754ccc455b878232b7a7e82766834e /morphlib/gitdir_tests.py
parent6903bd78aa5082dbe3c2834967380ff49ebda3d5 (diff)
downloadmorph-7e6bdb49957008b3981d570cbe8033627125efed.tar.gz
GitDir: Add remote.push(RefSpec...)
Remotes have a push method, which takes multiple RefSpecs, runs git push using arguments derived from the set of refspecs, then returns the push's result. If it fails the push, it will return the result in the exception.
Diffstat (limited to 'morphlib/gitdir_tests.py')
-rw-r--r--morphlib/gitdir_tests.py131
1 files changed, 131 insertions, 0 deletions
diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py
index c7cb50f6..21a6b5b8 100644
--- a/morphlib/gitdir_tests.py
+++ b/morphlib/gitdir_tests.py
@@ -318,3 +318,134 @@ class GitDirectoryRemoteConfigTests(unittest.TestCase):
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)
+ 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'))