Index: tests/git_common_test.py |
diff --git a/tests/git_common_test.py b/tests/git_common_test.py |
index 711543222734440c8b852ff3f3e6c284a7f5b489..a5e9c893813c393d33c75659b5f8e002686705f5 100755 |
--- a/tests/git_common_test.py |
+++ b/tests/git_common_test.py |
@@ -27,6 +27,7 @@ class GitCommonTestBase(unittest.TestCase): |
super(GitCommonTestBase, cls).setUpClass() |
import git_common |
cls.gc = git_common |
+ cls.gc.TEST_MODE = True |
class Support(GitCommonTestBase): |
@@ -63,6 +64,20 @@ class Support(GitCommonTestBase): |
def testMemoizeOneThreadsafe(self): |
self._testMemoizeOneBody(threadsafe=True) |
+ def testOnce(self): |
+ testlist = [] |
+ |
+ @self.gc.once |
+ def add_to_list(): |
+ testlist.append('dog') |
+ |
+ add_to_list() |
+ add_to_list() |
+ add_to_list() |
+ add_to_list() |
+ |
+ self.assertEquals(testlist, ['dog']) |
+ |
def slow_square(i): |
"""Helper for ScopedPoolTest. |
@@ -134,15 +149,15 @@ class ProgressPrinterTest(GitCommonTestBase): |
time.sleep(0.02) |
inc() |
- filtered = set(x.strip() for x in stream.data) |
- rslt = set(fmt % {'count': i} for i in xrange(11)) |
+ filtered = {x.strip() for x in stream.data} |
+ rslt = {fmt % {'count': i} for i in xrange(11)} |
self.assertSetEqual(filtered, rslt) |
self.assertGreaterEqual(stream.count, 10) |
class GitReadOnlyFunctionsTest(git_test_utils.GitRepoReadOnlyTestBase, |
GitCommonTestBase): |
- REPO = """ |
+ REPO_SCHEMA = """ |
A B C D |
B E D |
""" |
@@ -194,7 +209,7 @@ class GitReadOnlyFunctionsTest(git_test_utils.GitRepoReadOnlyTestBase, |
items = set(self.repo.commit_map.itervalues()) |
def testfn(): |
- for line in self.gc.stream('log', '--format=%H').xreadlines(): |
+ for line in self.gc.run_stream('log', '--format=%H').xreadlines(): |
line = line.strip() |
self.assertIn(line, items) |
items.remove(line) |
@@ -202,16 +217,22 @@ class GitReadOnlyFunctionsTest(git_test_utils.GitRepoReadOnlyTestBase, |
self.repo.run(testfn) |
def testCurrentBranch(self): |
+ def cur_branch_out_of_git(): |
+ os.chdir('..') |
+ return self.gc.current_branch() |
+ self.assertIsNone(self.repo.run(cur_branch_out_of_git)) |
+ |
self.repo.git('checkout', 'branch_D') |
self.assertEqual(self.repo.run(self.gc.current_branch), 'branch_D') |
def testBranches(self): |
self.assertEqual(self.repo.run(set, self.gc.branches()), |
- set(('branch_D', 'root_A'))) |
+ {'master', 'branch_D', 'root_A'}) |
- def testTags(self): |
- self.assertEqual(set(self.repo.run(self.gc.tags)), |
- {'tag_'+l for l in 'ABCDE'}) |
+ def testDormant(self): |
+ self.assertFalse(self.repo.run(self.gc.is_dormant, 'master')) |
+ self.repo.git('config', 'branch.master.dormant', 'true') |
+ self.assertTrue(self.repo.run(self.gc.is_dormant, 'master')) |
def testParseCommitrefs(self): |
ret = self.repo.run( |
@@ -234,6 +255,10 @@ class GitReadOnlyFunctionsTest(git_test_utils.GitRepoReadOnlyTestBase, |
with self.assertRaisesRegexp(Exception, r"one of \('master', 'bananas'\)"): |
self.repo.run(self.gc.parse_commitrefs, 'master', 'bananas') |
+ def testTags(self): |
+ self.assertEqual(set(self.repo.run(self.gc.tags)), |
+ {'tag_'+l for l in 'ABCDE'}) |
+ |
def testTree(self): |
tree = self.repo.run(self.gc.tree, 'master:some/files') |
file1 = self.COMMIT_A['some/files/file1']['data'] |
@@ -279,7 +304,7 @@ class GitReadOnlyFunctionsTest(git_test_utils.GitRepoReadOnlyTestBase, |
class GitMutableFunctionsTest(git_test_utils.GitRepoReadWriteTestBase, |
GitCommonTestBase): |
- REPO = '' |
+ REPO_SCHEMA = '' |
def _intern_data(self, data): |
with tempfile.TemporaryFile() as f: |
@@ -311,6 +336,25 @@ class GitMutableFunctionsTest(git_test_utils.GitRepoReadWriteTestBase, |
self.assertEquals(self.repo.run(self.gc.config_list, 'happy.derpies'), |
['food', 'cat']) |
+ self.assertEquals('cat', self.repo.run(self.gc.config, 'dude.bob', 'cat')) |
+ |
+ self.repo.run(self.gc.set_config, 'dude.bob', 'dog') |
+ |
+ self.assertEquals('dog', self.repo.run(self.gc.config, 'dude.bob', 'cat')) |
+ |
+ self.repo.run(self.gc.del_config, 'dude.bob') |
+ |
+ # This should work without raising an exception |
+ self.repo.run(self.gc.del_config, 'dude.bob') |
+ |
+ self.assertEquals('cat', self.repo.run(self.gc.config, 'dude.bob', 'cat')) |
+ |
+ self.assertEquals('origin/master', self.repo.run(self.gc.root)) |
+ |
+ self.repo.git('config', 'depot-tools.upstream', 'catfood') |
+ |
+ self.assertEquals('catfood', self.repo.run(self.gc.root)) |
+ |
def testUpstream(self): |
self.repo.git('commit', '--allow-empty', '-am', 'foooooo') |
self.assertEquals(self.repo.run(self.gc.upstream, 'bobly'), None) |
@@ -320,6 +364,209 @@ class GitMutableFunctionsTest(git_test_utils.GitRepoReadWriteTestBase, |
'master') |
+class GitMutableStructuredTest(git_test_utils.GitRepoReadWriteTestBase, |
+ GitCommonTestBase): |
+ REPO_SCHEMA = """ |
+ A B C D E F G |
+ B H I J K |
+ J L |
+ |
+ X Y Z |
+ """ |
+ |
+ COMMIT_B = {'file': {'data': 'B'}} |
+ COMMIT_H = {'file': {'data': 'H'}} |
+ COMMIT_I = {'file': {'data': 'I'}} |
+ COMMIT_J = {'file': {'data': 'J'}} |
+ COMMIT_K = {'file': {'data': 'K'}} |
+ COMMIT_L = {'file': {'data': 'L'}} |
+ |
+ def setUp(self): |
+ super(GitMutableStructuredTest, self).setUp() |
+ self.repo.git('branch', '--set-upstream-to', 'root_X', 'branch_Z') |
+ self.repo.git('branch', '--set-upstream-to', 'branch_G', 'branch_K') |
+ self.repo.git('branch', '--set-upstream-to', 'branch_K', 'branch_L') |
+ self.repo.git('branch', '--set-upstream-to', 'root_A', 'branch_G') |
+ self.repo.git('branch', '--set-upstream-to', 'root_X', 'root_A') |
+ |
+ def testMergeBase(self): |
+ self.repo.git('checkout', 'branch_K') |
+ |
+ self.assertEqual( |
+ self.repo['B'], |
+ self.repo.run(self.gc.get_or_create_merge_base, 'branch_K', 'branch_G') |
+ ) |
+ |
+ self.assertEqual( |
+ self.repo['J'], |
+ self.repo.run(self.gc.get_or_create_merge_base, 'branch_L', 'branch_K') |
+ ) |
+ |
+ self.assertEqual( |
+ self.repo['B'], self.repo.run(self.gc.config, 'branch.branch_K.base') |
+ ) |
+ |
+ # deadbeef is a bad hash, so this will result in repo['B'] |
+ self.repo.run(self.gc.manual_merge_base, 'branch_K', 'deadbeef') |
+ |
+ self.assertEqual( |
+ self.repo['B'], |
+ self.repo.run(self.gc.get_or_create_merge_base, 'branch_K', 'branch_G') |
+ ) |
+ |
+ # but if we pick a real ancestor, then it'll work |
+ self.repo.run(self.gc.manual_merge_base, 'branch_K', self.repo['I']) |
+ |
+ self.assertEqual( |
+ self.repo['I'], |
+ self.repo.run(self.gc.get_or_create_merge_base, 'branch_K', 'branch_G') |
+ ) |
+ |
+ self.assertEqual({'branch_K': self.repo['I'], 'branch_L': self.repo['J']}, |
+ self.repo.run(self.gc.branch_config_map, 'base')) |
+ |
+ self.repo.run(self.gc.remove_merge_base, 'branch_K') |
+ self.repo.run(self.gc.remove_merge_base, 'branch_L') |
+ |
+ self.assertEqual(None, |
+ self.repo.run(self.gc.config, 'branch.branch_K.base')) |
+ |
+ self.assertEqual({}, self.repo.run(self.gc.branch_config_map, 'base')) |
+ |
+ def testGetBranchTree(self): |
+ skipped, tree = self.repo.run(self.gc.get_branch_tree) |
+ self.assertEqual(skipped, {'master', 'root_X'}) |
+ self.assertEqual(tree, { |
+ 'branch_G': 'root_A', |
+ 'root_A': 'root_X', |
+ 'branch_K': 'branch_G', |
+ 'branch_L': 'branch_K', |
+ 'branch_Z': 'root_X' |
+ }) |
+ |
+ topdown = list(self.gc.topo_iter(tree)) |
+ bottomup = list(self.gc.topo_iter(tree, top_down=False)) |
+ |
+ self.assertEqual(topdown, [ |
+ ('branch_Z', 'root_X'), |
+ ('root_A', 'root_X'), |
+ ('branch_G', 'root_A'), |
+ ('branch_K', 'branch_G'), |
+ ('branch_L', 'branch_K'), |
+ ]) |
+ |
+ self.assertEqual(bottomup, [ |
+ ('branch_L', 'branch_K'), |
+ ('branch_Z', 'root_X'), |
+ ('branch_K', 'branch_G'), |
+ ('branch_G', 'root_A'), |
+ ('root_A', 'root_X'), |
+ ]) |
+ |
+ def testSquashBranch(self): |
+ self.repo.git('checkout', 'branch_K') |
+ |
+ self.repo.run(self.gc.squash_current_branch, 'cool message') |
+ |
+ lines = ['cool message', ''] |
+ for l in 'HIJK': |
+ lines.extend((self.repo[l], l, '')) |
+ lines.pop() |
+ msg = '\n'.join(lines) |
+ |
+ self.assertEquals(self.repo.run(self.gc.run, 'log', '-n1', '--format=%B'), |
+ msg) |
+ |
+ self.assertEquals( |
+ self.repo.git('cat-file', 'blob', 'branch_K:file').stdout, |
+ 'K' |
+ ) |
+ |
+ def testRebase(self): |
+ graphlines = lambda: [ |
+ l.strip() for l in self.repo.git( |
+ 'log', '--graph', '--format=%s', *['branch_'+b for b in 'GKL'] |
+ ).stdout.splitlines() |
+ ] |
+ self.assertEqual( |
+ graphlines(), [ |
+ '* G', |
+ '* F', |
+ '* E', |
+ '* D', |
+ '* C', |
+ '| * L', |
+ '| | * K', |
+ '| |/', |
+ '| * J', |
+ '| * I', |
+ '| * H', |
+ '|/', |
+ '* B', |
+ '* A', |
+ ] |
+ ) |
+ |
+ rslt = self.repo.run( |
+ self.gc.rebase, 'branch_G', 'branch_K~4', 'branch_K') |
+ self.assertTrue(rslt.success) |
+ |
+ self.assertEqual( |
+ graphlines(), [ |
+ '* L', |
+ '* J', |
+ '* I', |
+ '* H', |
+ '| * K', |
+ '| * J', |
+ '| * I', |
+ '| * H', |
+ '| * G', |
+ '| * F', |
+ '| * E', |
+ '| * D', |
+ '| * C', |
+ '|/', |
+ '* B', |
+ '* A', |
+ ] |
+ ) |
+ |
+ rslt = self.repo.run( |
+ self.gc.rebase, 'branch_K', 'branch_L~1', 'branch_L', abort=True) |
+ self.assertFalse(rslt.success) |
+ |
+ self.assertFalse(self.repo.run(self.gc.in_rebase)) |
+ |
+ rslt = self.repo.run( |
+ self.gc.rebase, 'branch_K', 'branch_L~1', 'branch_L', abort=False) |
+ self.assertFalse(rslt.success) |
+ |
+ self.assertTrue(self.repo.run(self.gc.in_rebase)) |
+ |
+ self.assertEqual(self.repo.git('status', '--porcelain').stdout, 'UU file\n') |
+ self.repo.git('checkout', '--theirs', 'file') |
+ self.repo.git('add', 'file') |
+ self.repo.git('rebase', '--continue') |
+ |
+ self.assertEqual( |
+ graphlines(), [ |
+ '* L', |
+ '* K', |
+ '* J', |
+ '* I', |
+ '* H', |
+ '* G', |
+ '* F', |
+ '* E', |
+ '* D', |
+ '* C', |
+ '* B', |
+ '* A', |
+ ] |
+ ) |
+ |
+ |
if __name__ == '__main__': |
sys.exit(coverage_utils.covered_main( |
os.path.join(DEPOT_TOOLS_ROOT, 'git_common.py') |