diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2022-05-18 07:43:53 +0800 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2022-05-18 07:43:53 +0800 |
commit | 21ec529987d10e0010badd37f8da3274167d436f (patch) | |
tree | a3394cfe902ce7edd07c89420c21c13274a2d295 /test/test_repo.py | |
parent | b30720ee4d9762a03eae4fa7cfa4b0190d81784d (diff) | |
download | gitpython-21ec529987d10e0010badd37f8da3274167d436f.tar.gz |
Run everything through 'black'
That way people who use it won't be deterred, while it unifies style
everywhere.
Diffstat (limited to 'test/test_repo.py')
-rw-r--r-- | test/test_repo.py | 477 |
1 files changed, 286 insertions, 191 deletions
diff --git a/test/test_repo.py b/test/test_repo.py index c5b2680d..7cffbbd8 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -30,17 +30,13 @@ from git import ( GitCmdObjectDB, Remote, BadName, - GitCommandError + GitCommandError, ) from git.exc import ( BadObject, ) from git.repo.fun import touch -from test.lib import ( - TestBase, - with_rw_repo, - fixture -) +from test.lib import TestBase, with_rw_repo, fixture from git.util import HIDE_WINDOWS_KNOWN_ERRORS, cygpath from test.lib import with_rw_directory from git.util import join_path_native, rmtree, rmfile, bin_to_hex @@ -58,7 +54,7 @@ def flatten(lol): return list(iter_flatten(lol)) -_tc_lock_fpaths = osp.join(osp.dirname(__file__), '../../.git/*.lock') +_tc_lock_fpaths = osp.join(osp.dirname(__file__), "../../.git/*.lock") def _rm_lock_files(): @@ -67,15 +63,17 @@ def _rm_lock_files(): class TestRepo(TestBase): - def setUp(self): _rm_lock_files() def tearDown(self): for lfp in glob.glob(_tc_lock_fpaths): if osp.isfile(lfp): - raise AssertionError('Previous TC left hanging git-lock file: {}'.format(lfp)) + raise AssertionError( + "Previous TC left hanging git-lock file: {}".format(lfp) + ) import gc + gc.collect() def test_new_should_raise_on_invalid_repo_location(self): @@ -84,15 +82,15 @@ class TestRepo(TestBase): def test_new_should_raise_on_non_existent_path(self): self.assertRaises(NoSuchPathError, Repo, "repos/foobar") - @with_rw_repo('0.3.2.1') + @with_rw_repo("0.3.2.1") def test_repo_creation_from_different_paths(self, rw_repo): r_from_gitdir = Repo(rw_repo.git_dir) self.assertEqual(r_from_gitdir.git_dir, rw_repo.git_dir) - assert r_from_gitdir.git_dir.endswith('.git') - assert not rw_repo.git.working_dir.endswith('.git') + assert r_from_gitdir.git_dir.endswith(".git") + assert not rw_repo.git.working_dir.endswith(".git") self.assertEqual(r_from_gitdir.git.working_dir, rw_repo.git.working_dir) - @with_rw_repo('0.3.2.1') + @with_rw_repo("0.3.2.1") def test_repo_creation_pathlib(self, rw_repo): r_from_gitdir = Repo(pathlib.Path(rw_repo.git_dir)) self.assertEqual(r_from_gitdir.git_dir, rw_repo.git_dir) @@ -113,33 +111,35 @@ class TestRepo(TestBase): # END for each head self.assertIsInstance(self.rorepo.heads.master, Head) - self.assertIsInstance(self.rorepo.heads['master'], Head) + self.assertIsInstance(self.rorepo.heads["master"], Head) def test_tree_from_revision(self): - tree = self.rorepo.tree('0.1.6') + tree = self.rorepo.tree("0.1.6") self.assertEqual(len(tree.hexsha), 40) self.assertEqual(tree.type, "tree") self.assertEqual(self.rorepo.tree(tree), tree) # try from invalid revision that does not exist - self.assertRaises(BadName, self.rorepo.tree, 'hello world') + self.assertRaises(BadName, self.rorepo.tree, "hello world") def test_pickleable(self): pickle.loads(pickle.dumps(self.rorepo)) def test_commit_from_revision(self): - commit = self.rorepo.commit('0.1.4') - self.assertEqual(commit.type, 'commit') + commit = self.rorepo.commit("0.1.4") + self.assertEqual(commit.type, "commit") self.assertEqual(self.rorepo.commit(commit), commit) def test_commits(self): mc = 10 - commits = list(self.rorepo.iter_commits('0.1.6', max_count=mc)) + commits = list(self.rorepo.iter_commits("0.1.6", max_count=mc)) self.assertEqual(len(commits), mc) c = commits[0] - self.assertEqual('9a4b1d4d11eee3c5362a4152216376e634bd14cf', c.hexsha) - self.assertEqual(["c76852d0bff115720af3f27acdb084c59361e5f6"], [p.hexsha for p in c.parents]) + self.assertEqual("9a4b1d4d11eee3c5362a4152216376e634bd14cf", c.hexsha) + self.assertEqual( + ["c76852d0bff115720af3f27acdb084c59361e5f6"], [p.hexsha for p in c.parents] + ) self.assertEqual("ce41fc29549042f1aa09cc03174896cf23f112e3", c.tree.hexsha) self.assertEqual("Michael Trier", c.author.name) self.assertEqual("mtrier@gmail.com", c.author.email) @@ -157,7 +157,7 @@ class TestRepo(TestBase): def test_trees(self): mc = 30 num_trees = 0 - for tree in self.rorepo.iter_trees('0.1.5', max_count=mc): + for tree in self.rorepo.iter_trees("0.1.5", max_count=mc): num_trees += 1 self.assertIsInstance(tree, Tree) # END for each tree @@ -176,7 +176,7 @@ class TestRepo(TestBase): assert not repo.head.is_valid() # we can change the head to some other ref - head_ref = Head.from_path(repo, Head.to_full_path('some_head')) + head_ref = Head.from_path(repo, Head.to_full_path("some_head")) assert not head_ref.is_valid() repo.head.ref = head_ref @@ -195,7 +195,9 @@ class TestRepo(TestBase): original_repo = Repo.init(osp.join(rw_dir, "repo")) environment = {"entry1": "value", "another_entry": "10"} - cloned = Repo.clone_from(original_repo.git_dir, osp.join(rw_dir, "clone"), env=environment) + cloned = Repo.clone_from( + original_repo.git_dir, osp.join(rw_dir, "clone"), env=environment + ) self.assertEqual(environment, cloned.git.environment()) @@ -215,20 +217,32 @@ class TestRepo(TestBase): def test_clone_from_pathlib_withConfig(self, rw_dir): original_repo = Repo.init(osp.join(rw_dir, "repo")) - cloned = Repo.clone_from(original_repo.git_dir, pathlib.Path(rw_dir) / "clone_pathlib_withConfig", - multi_options=["--recurse-submodules=repo", - "--config core.filemode=false", - "--config submodule.repo.update=checkout", - "--config filter.lfs.clean='git-lfs clean -- %f'"]) - - self.assertEqual(cloned.config_reader().get_value('submodule', 'active'), 'repo') - self.assertEqual(cloned.config_reader().get_value('core', 'filemode'), False) - self.assertEqual(cloned.config_reader().get_value('submodule "repo"', 'update'), 'checkout') - self.assertEqual(cloned.config_reader().get_value('filter "lfs"', 'clean'), 'git-lfs clean -- %f') + cloned = Repo.clone_from( + original_repo.git_dir, + pathlib.Path(rw_dir) / "clone_pathlib_withConfig", + multi_options=[ + "--recurse-submodules=repo", + "--config core.filemode=false", + "--config submodule.repo.update=checkout", + "--config filter.lfs.clean='git-lfs clean -- %f'", + ], + ) + + self.assertEqual( + cloned.config_reader().get_value("submodule", "active"), "repo" + ) + self.assertEqual(cloned.config_reader().get_value("core", "filemode"), False) + self.assertEqual( + cloned.config_reader().get_value('submodule "repo"', "update"), "checkout" + ) + self.assertEqual( + cloned.config_reader().get_value('filter "lfs"', "clean"), + "git-lfs clean -- %f", + ) def test_clone_from_with_path_contains_unicode(self): with tempfile.TemporaryDirectory() as tmpdir: - unicode_dir_name = '\u0394' + unicode_dir_name = "\u0394" path_with_unicode = os.path.join(tmpdir, unicode_dir_name) os.makedirs(path_with_unicode) @@ -238,7 +252,7 @@ class TestRepo(TestBase): to_path=path_with_unicode, ) except UnicodeEncodeError: - self.fail('Raised UnicodeEncodeError') + self.fail("Raised UnicodeEncodeError") @with_rw_directory def test_leaking_password_in_clone_logs(self, rw_dir): @@ -246,16 +260,21 @@ class TestRepo(TestBase): try: Repo.clone_from( url="https://fakeuser:{}@fakerepo.example.com/testrepo".format( - password), - to_path=rw_dir) + password + ), + to_path=rw_dir, + ) except GitCommandError as err: - assert password not in str(err), "The error message '%s' should not contain the password" % err + assert password not in str(err), ( + "The error message '%s' should not contain the password" % err + ) # Working example from a blank private project Repo.clone_from( url="https://gitlab+deploy-token-392045:mLWhVus7bjLsy8xj8q2V@gitlab.com/mercierm/test_git_python", - to_path=rw_dir) + to_path=rw_dir, + ) - @with_rw_repo('HEAD') + @with_rw_repo("HEAD") def test_max_chunk_size(self, repo): class TestOutputStream(TestBase): def __init__(self, max_chunk_size): @@ -265,10 +284,20 @@ class TestRepo(TestBase): self.assertTrue(len(b) <= self.max_chunk_size) for chunk_size in [16, 128, 1024]: - repo.git.status(output_stream=TestOutputStream(chunk_size), max_chunk_size=chunk_size) - - repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), max_chunk_size=None) - repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), max_chunk_size=-10) + repo.git.status( + output_stream=TestOutputStream(chunk_size), max_chunk_size=chunk_size + ) + + repo.git.log( + n=100, + output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), + max_chunk_size=None, + ) + repo.git.log( + n=100, + output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE), + max_chunk_size=-10, + ) repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE)) def test_init(self): @@ -352,7 +381,7 @@ class TestRepo(TestBase): self.rorepo.alternates = cur_alternates def test_repr(self): - assert repr(self.rorepo).startswith('<git.repo.base.Repo ') + assert repr(self.rorepo).startswith("<git.repo.base.Repo ") def test_is_dirty_with_bare_repository(self): orig_value = self.rorepo._bare @@ -365,7 +394,9 @@ class TestRepo(TestBase): for index in (0, 1): for working_tree in (0, 1): for untracked_files in (0, 1): - assert self.rorepo.is_dirty(index, working_tree, untracked_files) in (True, False) + assert self.rorepo.is_dirty( + index, working_tree, untracked_files + ) in (True, False) # END untracked files # END working tree # END index @@ -379,7 +410,9 @@ class TestRepo(TestBase): for index in (0, 1): for working_tree in (0, 1): for untracked_files in (0, 1): - assert self.rorepo.is_dirty(index, working_tree, untracked_files, path=':!foo') in (True, False) + assert self.rorepo.is_dirty( + index, working_tree, untracked_files, path=":!foo" + ) in (True, False) # END untracked files # END working tree # END index @@ -388,7 +421,7 @@ class TestRepo(TestBase): assert self.rorepo.is_dirty() is False self.rorepo._bare = orig_val - @with_rw_repo('HEAD') + @with_rw_repo("HEAD") def test_is_dirty_with_path(self, rwrepo): assert rwrepo.is_dirty(path="git") is False @@ -407,17 +440,19 @@ class TestRepo(TestBase): assert rwrepo.is_dirty(untracked_files=True, path="doc") is True def test_head(self): - self.assertEqual(self.rorepo.head.reference.object, self.rorepo.active_branch.object) + self.assertEqual( + self.rorepo.head.reference.object, self.rorepo.active_branch.object + ) def test_index(self): index = self.rorepo.index self.assertIsInstance(index, IndexFile) def test_tag(self): - assert self.rorepo.tag('refs/tags/0.1.5').commit + assert self.rorepo.tag("refs/tags/0.1.5").commit def test_tag_to_full_tag_path(self): - tags = ['0.1.5', 'tags/0.1.5', 'refs/tags/0.1.5'] + tags = ["0.1.5", "tags/0.1.5", "refs/tags/0.1.5"] value_errors = [] for tag in tags: try: @@ -427,16 +462,16 @@ class TestRepo(TestBase): self.assertEqual(value_errors, []) def test_archive(self): - tmpfile = tempfile.mktemp(suffix='archive-test') - with open(tmpfile, 'wb') as stream: - self.rorepo.archive(stream, '0.1.6', path='doc') + tmpfile = tempfile.mktemp(suffix="archive-test") + with open(tmpfile, "wb") as stream: + self.rorepo.archive(stream, "0.1.6", path="doc") assert stream.tell() os.remove(tmpfile) - @mock.patch.object(Git, '_call_process') + @mock.patch.object(Git, "_call_process") def test_should_display_blame_information(self, git): - git.return_value = fixture('blame') - b = self.rorepo.blame('master', 'lib/git.py') + git.return_value = fixture("blame") + b = self.rorepo.blame("master", "lib/git.py") self.assertEqual(13, len(b)) self.assertEqual(2, len(b[0])) # self.assertEqual(25, reduce(lambda acc, x: acc + len(x[-1]), b)) @@ -444,85 +479,127 @@ class TestRepo(TestBase): c = b[0][0] self.assertTrue(git.called) - self.assertEqual('634396b2f541a9f2d58b00be1a07f0c358b999b3', c.hexsha) - self.assertEqual('Tom Preston-Werner', c.author.name) - self.assertEqual('tom@mojombo.com', c.author.email) + self.assertEqual("634396b2f541a9f2d58b00be1a07f0c358b999b3", c.hexsha) + self.assertEqual("Tom Preston-Werner", c.author.name) + self.assertEqual("tom@mojombo.com", c.author.email) self.assertEqual(1191997100, c.authored_date) - self.assertEqual('Tom Preston-Werner', c.committer.name) - self.assertEqual('tom@mojombo.com', c.committer.email) + self.assertEqual("Tom Preston-Werner", c.committer.name) + self.assertEqual("tom@mojombo.com", c.committer.email) self.assertEqual(1191997100, c.committed_date) - self.assertRaisesRegex(ValueError, "634396b2f541a9f2d58b00be1a07f0c358b999b3 missing", lambda: c.message) + self.assertRaisesRegex( + ValueError, + "634396b2f541a9f2d58b00be1a07f0c358b999b3 missing", + lambda: c.message, + ) # test the 'lines per commit' entries tlist = b[0][1] self.assertTrue(tlist) self.assertTrue(isinstance(tlist[0], str)) - self.assertTrue(len(tlist) < sum(len(t) for t in tlist)) # test for single-char bug + self.assertTrue( + len(tlist) < sum(len(t) for t in tlist) + ) # test for single-char bug # BINARY BLAME - git.return_value = fixture('blame_binary') - blames = self.rorepo.blame('master', 'rps') + git.return_value = fixture("blame_binary") + blames = self.rorepo.blame("master", "rps") self.assertEqual(len(blames), 2) def test_blame_real(self): c = 0 - nml = 0 # amount of multi-lines per blame + nml = 0 # amount of multi-lines per blame for item in self.rorepo.head.commit.tree.traverse( - predicate=lambda i, d: i.type == 'blob' and i.path.endswith('.py')): + predicate=lambda i, d: i.type == "blob" and i.path.endswith(".py") + ): c += 1 for b in self.rorepo.blame(self.rorepo.head, item.path): nml += int(len(b[1]) > 1) # END for each item to traverse assert c, "Should have executed at least one blame command" - assert nml, "There should at least be one blame commit that contains multiple lines" + assert ( + nml + ), "There should at least be one blame commit that contains multiple lines" - @mock.patch.object(Git, '_call_process') + @mock.patch.object(Git, "_call_process") def test_blame_incremental(self, git): # loop over two fixtures, create a test fixture for 2.11.1+ syntax - for git_fixture in ('blame_incremental', 'blame_incremental_2.11.1_plus'): + for git_fixture in ("blame_incremental", "blame_incremental_2.11.1_plus"): git.return_value = fixture(git_fixture) - blame_output = self.rorepo.blame_incremental('9debf6b0aafb6f7781ea9d1383c86939a1aacde3', 'AUTHORS') + blame_output = self.rorepo.blame_incremental( + "9debf6b0aafb6f7781ea9d1383c86939a1aacde3", "AUTHORS" + ) blame_output = list(blame_output) self.assertEqual(len(blame_output), 5) # Check all outputted line numbers ranges = flatten([entry.linenos for entry in blame_output]) - self.assertEqual(ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(3, 14), range(15, 17)])) + self.assertEqual( + ranges, + flatten( + [ + range(2, 3), + range(14, 15), + range(1, 2), + range(3, 14), + range(15, 17), + ] + ), + ) commits = [entry.commit.hexsha[:7] for entry in blame_output] - self.assertEqual(commits, ['82b8902', '82b8902', 'c76852d', 'c76852d', 'c76852d']) + self.assertEqual( + commits, ["82b8902", "82b8902", "c76852d", "c76852d", "c76852d"] + ) # Original filenames - self.assertSequenceEqual([entry.orig_path for entry in blame_output], ['AUTHORS'] * len(blame_output)) + self.assertSequenceEqual( + [entry.orig_path for entry in blame_output], + ["AUTHORS"] * len(blame_output), + ) # Original line numbers orig_ranges = flatten([entry.orig_linenos for entry in blame_output]) - self.assertEqual(orig_ranges, flatten([range(2, 3), range(14, 15), range(1, 2), range(2, 13), range(13, 15)])) # noqa E501 - - @mock.patch.object(Git, '_call_process') + self.assertEqual( + orig_ranges, + flatten( + [ + range(2, 3), + range(14, 15), + range(1, 2), + range(2, 13), + range(13, 15), + ] + ), + ) # noqa E501 + + @mock.patch.object(Git, "_call_process") def test_blame_complex_revision(self, git): - git.return_value = fixture('blame_complex_revision') + git.return_value = fixture("blame_complex_revision") res = self.rorepo.blame("HEAD~10..HEAD", "README.md") self.assertEqual(len(res), 1) self.assertEqual(len(res[0][1]), 83, "Unexpected amount of parsed blame lines") - @skipIf(HIDE_WINDOWS_KNOWN_ERRORS and Git.is_cygwin(), - """FIXME: File "C:\\projects\\gitpython\\git\\cmd.py", line 671, in execute + @skipIf( + HIDE_WINDOWS_KNOWN_ERRORS and Git.is_cygwin(), + """FIXME: File "C:\\projects\\gitpython\\git\\cmd.py", line 671, in execute raise GitCommandError(command, status, stderr_value, stdout_value) GitCommandError: Cmd('git') failed due to: exit code(128) cmdline: git add 1__��ava verb��ten 1_test _myfile 1_test_other_file 1_��ava-----verb��ten stderr: 'fatal: pathspec '"1__çava verböten"' did not match any files' - """) - @with_rw_repo('HEAD', bare=False) + """, + ) + @with_rw_repo("HEAD", bare=False) def test_untracked_files(self, rwrepo): for run, repo_add in enumerate((rwrepo.index.add, rwrepo.git.add)): base = rwrepo.working_tree_dir - files = (join_path_native(base, "%i_test _myfile" % run), - join_path_native(base, "%i_test_other_file" % run), - join_path_native(base, "%i__çava verböten" % run), - join_path_native(base, "%i_çava-----verböten" % run)) + files = ( + join_path_native(base, "%i_test _myfile" % run), + join_path_native(base, "%i_test_other_file" % run), + join_path_native(base, "%i__çava verböten" % run), + join_path_native(base, "%i_çava-----verböten" % run), + ) num_recently_untracked = 0 for fpath in files: @@ -538,13 +615,15 @@ class TestRepo(TestBase): self.assertEqual(len(files), num_test_untracked) repo_add(untracked_files) - self.assertEqual(len(rwrepo.untracked_files), (num_recently_untracked - len(files))) + self.assertEqual( + len(rwrepo.untracked_files), (num_recently_untracked - len(files)) + ) # end for each run def test_config_reader(self): - reader = self.rorepo.config_reader() # all config files + reader = self.rorepo.config_reader() # all config files assert reader.read_only - reader = self.rorepo.config_reader("repository") # single config file + reader = self.rorepo.config_reader("repository") # single config file assert reader.read_only def test_config_writer(self): @@ -586,17 +665,17 @@ class TestRepo(TestBase): @with_rw_directory def test_tilde_and_env_vars_in_repo_path(self, rw_dir): - ph = os.environ.get('HOME') + ph = os.environ.get("HOME") try: - os.environ['HOME'] = rw_dir - Repo.init(osp.join('~', 'test.git'), bare=True) + os.environ["HOME"] = rw_dir + Repo.init(osp.join("~", "test.git"), bare=True) - os.environ['FOO'] = rw_dir - Repo.init(osp.join('$FOO', 'test.git'), bare=True) + os.environ["FOO"] = rw_dir + Repo.init(osp.join("$FOO", "test.git"), bare=True) finally: if ph: - os.environ['HOME'] = ph - del os.environ['FOO'] + os.environ["HOME"] = ph + del os.environ["FOO"] # end assure HOME gets reset to what it was def test_git_cmd(self): @@ -623,7 +702,7 @@ class TestRepo(TestBase): s = mkfull() lines = s.readlines() self.assertEqual(len(lines), 3) - self.assertTrue(lines[-1].endswith(b'\n'), lines[-1]) + self.assertTrue(lines[-1].endswith(b"\n"), lines[-1]) self.assertEqual(s._stream.tell(), len(d)) # must have scrubbed to the end # realines line limit @@ -643,7 +722,7 @@ class TestRepo(TestBase): self.assertEqual(s.readline(), l1) self.assertEqual(s.readline(), l2) self.assertEqual(s.readline(), l3) - self.assertEqual(s.readline(), b'') + self.assertEqual(s.readline(), b"") self.assertEqual(s._stream.tell(), len(d)) # readline limit @@ -654,13 +733,13 @@ class TestRepo(TestBase): # readline on tiny section s = mktiny() self.assertEqual(s.readline(), l1p) - self.assertEqual(s.readline(), b'') + self.assertEqual(s.readline(), b"") self.assertEqual(s._stream.tell(), ts + 1) # read no limit s = mkfull() self.assertEqual(s.read(), d[:-1]) - self.assertEqual(s.read(), b'') + self.assertEqual(s.read(), b"") self.assertEqual(s._stream.tell(), len(d)) # read limit @@ -679,24 +758,24 @@ class TestRepo(TestBase): def _assert_rev_parse_types(self, name, rev_obj): rev_parse = self.rorepo.rev_parse - if rev_obj.type == 'tag': + if rev_obj.type == "tag": rev_obj = rev_obj.object # tree and blob type - obj = rev_parse(name + '^{tree}') + obj = rev_parse(name + "^{tree}") self.assertEqual(obj, rev_obj.tree) - obj = rev_parse(name + ':CHANGES') - self.assertEqual(obj.type, 'blob') - self.assertEqual(obj.path, 'CHANGES') - self.assertEqual(rev_obj.tree['CHANGES'], obj) + obj = rev_parse(name + ":CHANGES") + self.assertEqual(obj.type, "blob") + self.assertEqual(obj.path, "CHANGES") + self.assertEqual(rev_obj.tree["CHANGES"], obj) def _assert_rev_parse(self, name): """tries multiple different rev-parse syntaxes with the given name :return: parsed object""" rev_parse = self.rorepo.rev_parse orig_obj = rev_parse(name) - if orig_obj.type == 'tag': + if orig_obj.type == "tag": obj = orig_obj.object else: obj = orig_obj @@ -737,17 +816,19 @@ class TestRepo(TestBase): return orig_obj - @with_rw_repo('HEAD', bare=False) + @with_rw_repo("HEAD", bare=False) def test_rw_rev_parse(self, rwrepo): # verify it does not confuse branches with hexsha ids - ahead = rwrepo.create_head('aaaaaaaa') - assert(rwrepo.rev_parse(str(ahead)) == ahead.commit) + ahead = rwrepo.create_head("aaaaaaaa") + assert rwrepo.rev_parse(str(ahead)) == ahead.commit def test_rev_parse(self): rev_parse = self.rorepo.rev_parse # try special case: This one failed at some point, make sure its fixed - self.assertEqual(rev_parse("33ebe").hexsha, "33ebe7acec14b25c5f84f35a664803fcab2f7781") + self.assertEqual( + rev_parse("33ebe").hexsha, "33ebe7acec14b25c5f84f35a664803fcab2f7781" + ) # start from reference num_resolved = 0 @@ -755,7 +836,7 @@ class TestRepo(TestBase): for ref_no, ref in enumerate(Reference.iter_items(self.rorepo)): path_tokens = ref.path.split("/") for pt in range(len(path_tokens)): - path_section = '/'.join(path_tokens[-(pt + 1):]) + path_section = "/".join(path_tokens[-(pt + 1) :]) try: obj = self._assert_rev_parse(path_section) self.assertEqual(obj.type, ref.object.type) @@ -771,17 +852,17 @@ class TestRepo(TestBase): assert num_resolved # it works with tags ! - tag = self._assert_rev_parse('0.1.4') - self.assertEqual(tag.type, 'tag') + tag = self._assert_rev_parse("0.1.4") + self.assertEqual(tag.type, "tag") # try full sha directly ( including type conversion ) self.assertEqual(tag.object, rev_parse(tag.object.hexsha)) self._assert_rev_parse_types(tag.object.hexsha, tag.object) # multiple tree types result in the same tree: HEAD^{tree}^{tree}:CHANGES - rev = '0.1.4^{tree}^{tree}' + rev = "0.1.4^{tree}^{tree}" self.assertEqual(rev_parse(rev), tag.object.tree) - self.assertEqual(rev_parse(rev + ':CHANGES'), tag.object.tree['CHANGES']) + self.assertEqual(rev_parse(rev + ":CHANGES"), tag.object.tree["CHANGES"]) # try to get parents from first revision - it should fail as no such revision # exists @@ -802,15 +883,18 @@ class TestRepo(TestBase): # needs a tag which points to a blob # ref^0 returns commit being pointed to, same with ref~0, and ^{} - tag = rev_parse('0.1.4') - for token in (('~0', '^0', '^{}')): - self.assertEqual(tag.object, rev_parse('0.1.4%s' % token)) + tag = rev_parse("0.1.4") + for token in ("~0", "^0", "^{}"): + self.assertEqual(tag.object, rev_parse("0.1.4%s" % token)) # END handle multiple tokens # try partial parsing max_items = 40 for i, binsha in enumerate(self.rorepo.odb.sha_iter()): - self.assertEqual(rev_parse(bin_to_hex(binsha)[:8 - (i % 2)].decode('ascii')).binsha, binsha) + self.assertEqual( + rev_parse(bin_to_hex(binsha)[: 8 - (i % 2)].decode("ascii")).binsha, + binsha, + ) if i > max_items: # this is rather slow currently, as rev_parse returns an object # which requires accessing packs, it has some additional overhead @@ -818,10 +902,10 @@ class TestRepo(TestBase): # END for each binsha in repo # missing closing brace commit^{tree - self.assertRaises(ValueError, rev_parse, '0.1.4^{tree') + self.assertRaises(ValueError, rev_parse, "0.1.4^{tree") # missing starting brace - self.assertRaises(ValueError, rev_parse, '0.1.4^tree}') + self.assertRaises(ValueError, rev_parse, "0.1.4^tree}") # REVLOG ####### @@ -831,23 +915,23 @@ class TestRepo(TestBase): self.assertRaises(BadObject, rev_parse, "%s@{0}" % head.commit.hexsha) # uses HEAD.ref by default - self.assertEqual(rev_parse('@{0}'), head.commit) + self.assertEqual(rev_parse("@{0}"), head.commit) if not head.is_detached: - refspec = '%s@{0}' % head.ref.name + refspec = "%s@{0}" % head.ref.name self.assertEqual(rev_parse(refspec), head.ref.commit) # all additional specs work as well self.assertEqual(rev_parse(refspec + "^{tree}"), head.commit.tree) - self.assertEqual(rev_parse(refspec + ":CHANGES").type, 'blob') + self.assertEqual(rev_parse(refspec + ":CHANGES").type, "blob") # END operate on non-detached head # position doesn't exist - self.assertRaises(IndexError, rev_parse, '@{10000}') + self.assertRaises(IndexError, rev_parse, "@{10000}") # currently, nothing more is supported self.assertRaises(NotImplementedError, rev_parse, "@{1 week ago}") # the last position - assert rev_parse('@{1}') != head.commit + assert rev_parse("@{1}") != head.commit def test_repo_odbtype(self): target_type = GitCmdObjectDB @@ -860,7 +944,7 @@ class TestRepo(TestBase): self.assertIsInstance(self.rorepo.submodule("gitdb"), Submodule) self.assertRaises(ValueError, self.rorepo.submodule, "doesn't exist") - @with_rw_repo('HEAD', bare=False) + @with_rw_repo("HEAD", bare=False) def test_submodule_update(self, rwrepo): # fails in bare mode rwrepo._bare = True @@ -869,27 +953,31 @@ class TestRepo(TestBase): # test create submodule sm = rwrepo.submodules[0] - sm = rwrepo.create_submodule("my_new_sub", "some_path", join_path_native(self.rorepo.working_tree_dir, sm.path)) + sm = rwrepo.create_submodule( + "my_new_sub", + "some_path", + join_path_native(self.rorepo.working_tree_dir, sm.path), + ) self.assertIsInstance(sm, Submodule) # note: the rest of this functionality is tested in test_submodule - @with_rw_repo('HEAD') + @with_rw_repo("HEAD") def test_git_file(self, rwrepo): # Move the .git directory to another location and create the .git file. - real_path_abs = osp.abspath(join_path_native(rwrepo.working_tree_dir, '.real')) + real_path_abs = osp.abspath(join_path_native(rwrepo.working_tree_dir, ".real")) os.rename(rwrepo.git_dir, real_path_abs) - git_file_path = join_path_native(rwrepo.working_tree_dir, '.git') - with open(git_file_path, 'wb') as fp: - fp.write(fixture('git_file')) + git_file_path = join_path_native(rwrepo.working_tree_dir, ".git") + with open(git_file_path, "wb") as fp: + fp.write(fixture("git_file")) # Create a repo and make sure it's pointing to the relocated .git directory. git_file_repo = Repo(rwrepo.working_tree_dir) self.assertEqual(osp.abspath(git_file_repo.git_dir), real_path_abs) # Test using an absolute gitdir path in the .git file. - with open(git_file_path, 'wb') as fp: - fp.write(('gitdir: %s\n' % real_path_abs).encode('ascii')) + with open(git_file_path, "wb") as fp: + fp.write(("gitdir: %s\n" % real_path_abs).encode("ascii")) git_file_repo = Repo(rwrepo.working_tree_dir) self.assertEqual(osp.abspath(git_file_repo.git_dir), real_path_abs) @@ -906,13 +994,13 @@ class TestRepo(TestBase): for _ in range(64): for repo_type in (GitCmdObjectDB, GitDB): repo = Repo(self.rorepo.working_tree_dir, odbt=repo_type) - last_commit(repo, 'master', 'test/test_base.py') + last_commit(repo, "master", "test/test_base.py") # end for each repository type # end for each iteration def test_remote_method(self): - self.assertRaises(ValueError, self.rorepo.remote, 'foo-blue') - self.assertIsInstance(self.rorepo.remote(name='origin'), Remote) + self.assertRaises(ValueError, self.rorepo.remote, "foo-blue") + self.assertIsInstance(self.rorepo.remote(name="origin"), Remote) @with_rw_directory def test_empty_repo(self, rw_dir): @@ -920,13 +1008,13 @@ class TestRepo(TestBase): r = Repo.init(rw_dir, mkdir=False) # It's ok not to be able to iterate a commit, as there is none self.assertRaises(ValueError, r.iter_commits) - self.assertEqual(r.active_branch.name, 'master') + self.assertEqual(r.active_branch.name, "master") assert not r.active_branch.is_valid(), "Branch is yet to be born" # actually, when trying to create a new branch without a commit, git itself fails # We should, however, not fail ungracefully - self.assertRaises(BadName, r.create_head, 'foo') - self.assertRaises(BadName, r.create_head, 'master') + self.assertRaises(BadName, r.create_head, "foo") + self.assertRaises(BadName, r.create_head, "master") # It's expected to not be able to access a tree self.assertRaises(ValueError, r.tree) @@ -936,43 +1024,43 @@ class TestRepo(TestBase): r.index.commit("initial commit\nBAD MESSAGE 1\n") # Now a branch should be creatable - nb = r.create_head('foo') + nb = r.create_head("foo") assert nb.is_valid() - with open(new_file_path, 'w') as f: - f.write('Line 1\n') + with open(new_file_path, "w") as f: + f.write("Line 1\n") r.index.add([new_file_path]) r.index.commit("add line 1\nBAD MESSAGE 2\n") - with open('%s/.git/logs/refs/heads/master' % (rw_dir,), 'r') as f: + with open("%s/.git/logs/refs/heads/master" % (rw_dir,), "r") as f: contents = f.read() - assert 'BAD MESSAGE' not in contents, 'log is corrupt' + assert "BAD MESSAGE" not in contents, "log is corrupt" def test_merge_base(self): repo = self.rorepo - c1 = 'f6aa8d1' - c2 = repo.commit('d46e3fe') - c3 = '763ef75' + c1 = "f6aa8d1" + c2 = repo.commit("d46e3fe") + c3 = "763ef75" self.assertRaises(ValueError, repo.merge_base) - self.assertRaises(ValueError, repo.merge_base, 'foo') + self.assertRaises(ValueError, repo.merge_base, "foo") # two commit merge-base res = repo.merge_base(c1, c2) self.assertIsInstance(res, list) self.assertEqual(len(res), 1) self.assertIsInstance(res[0], Commit) - self.assertTrue(res[0].hexsha.startswith('3936084')) + self.assertTrue(res[0].hexsha.startswith("3936084")) - for kw in ('a', 'all'): + for kw in ("a", "all"): res = repo.merge_base(c1, c2, c3, **{kw: True}) self.assertIsInstance(res, list) self.assertEqual(len(res), 1) # end for each keyword signalling all merge-bases to be returned # Test for no merge base - can't do as we have - self.assertRaises(GitCommandError, repo.merge_base, c1, 'ffffff') + self.assertRaises(GitCommandError, repo.merge_base, c1, "ffffff") def test_is_ancestor(self): git = self.rorepo.git @@ -980,23 +1068,23 @@ class TestRepo(TestBase): raise SkipTest("git merge-base --is-ancestor feature unsupported") repo = self.rorepo - c1 = 'f6aa8d1' - c2 = '763ef75' + c1 = "f6aa8d1" + c2 = "763ef75" self.assertTrue(repo.is_ancestor(c1, c1)) self.assertTrue(repo.is_ancestor("master", "master")) self.assertTrue(repo.is_ancestor(c1, c2)) self.assertTrue(repo.is_ancestor(c1, "master")) self.assertFalse(repo.is_ancestor(c2, c1)) self.assertFalse(repo.is_ancestor("master", c1)) - for i, j in itertools.permutations([c1, 'ffffff', ''], r=2): + for i, j in itertools.permutations([c1, "ffffff", ""], r=2): self.assertRaises(GitCommandError, repo.is_ancestor, i, j) def test_is_valid_object(self): repo = self.rorepo - commit_sha = 'f6aa8d1' - blob_sha = '1fbe3e4375' - tree_sha = '960b40fe36' - tag_sha = '42c2f60c43' + commit_sha = "f6aa8d1" + blob_sha = "1fbe3e4375" + tree_sha = "960b40fe36" + tag_sha = "42c2f60c43" # Check for valid objects self.assertTrue(repo.is_valid_object(commit_sha)) @@ -1005,19 +1093,21 @@ class TestRepo(TestBase): self.assertTrue(repo.is_valid_object(tag_sha)) # Check for valid objects of specific type - self.assertTrue(repo.is_valid_object(commit_sha, 'commit')) - self.assertTrue(repo.is_valid_object(blob_sha, 'blob')) - self.assertTrue(repo.is_valid_object(tree_sha, 'tree')) - self.assertTrue(repo.is_valid_object(tag_sha, 'tag')) + self.assertTrue(repo.is_valid_object(commit_sha, "commit")) + self.assertTrue(repo.is_valid_object(blob_sha, "blob")) + self.assertTrue(repo.is_valid_object(tree_sha, "tree")) + self.assertTrue(repo.is_valid_object(tag_sha, "tag")) # Check for invalid objects - self.assertFalse(repo.is_valid_object(b'1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a', 'blob')) + self.assertFalse( + repo.is_valid_object(b"1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a", "blob") + ) # Check for invalid objects of specific type - self.assertFalse(repo.is_valid_object(commit_sha, 'blob')) - self.assertFalse(repo.is_valid_object(blob_sha, 'commit')) - self.assertFalse(repo.is_valid_object(tree_sha, 'commit')) - self.assertFalse(repo.is_valid_object(tag_sha, 'commit')) + self.assertFalse(repo.is_valid_object(commit_sha, "blob")) + self.assertFalse(repo.is_valid_object(blob_sha, "commit")) + self.assertFalse(repo.is_valid_object(tree_sha, "commit")) + self.assertFalse(repo.is_valid_object(tag_sha, "commit")) @with_rw_directory def test_git_work_tree_dotgit(self, rw_dir): @@ -1027,12 +1117,12 @@ class TestRepo(TestBase): if git.version_info[:3] < (2, 5, 1): raise SkipTest("worktree feature unsupported") - rw_master = self.rorepo.clone(join_path_native(rw_dir, 'master_repo')) - branch = rw_master.create_head('aaaaaaaa') - worktree_path = join_path_native(rw_dir, 'worktree_repo') + rw_master = self.rorepo.clone(join_path_native(rw_dir, "master_repo")) + branch = rw_master.create_head("aaaaaaaa") + worktree_path = join_path_native(rw_dir, "worktree_repo") if Git.is_cygwin(): worktree_path = cygpath(worktree_path) - rw_master.git.worktree('add', worktree_path, branch.name) + rw_master.git.worktree("add", worktree_path, branch.name) # this ensures that we can read the repo's gitdir correctly repo = Repo(worktree_path) @@ -1048,7 +1138,7 @@ class TestRepo(TestBase): origin = repo.remotes.origin self.assertIsInstance(origin, Remote) - self.assertIsInstance(repo.heads['aaaaaaaa'], Head) + self.assertIsInstance(repo.heads["aaaaaaaa"], Head) @with_rw_directory def test_git_work_tree_env(self, rw_dir): @@ -1057,18 +1147,18 @@ class TestRepo(TestBase): # move .git directory to a subdirectory # set GIT_DIR and GIT_WORK_TREE appropriately # check that repo.working_tree_dir == rw_dir - self.rorepo.clone(join_path_native(rw_dir, 'master_repo')) + self.rorepo.clone(join_path_native(rw_dir, "master_repo")) - repo_dir = join_path_native(rw_dir, 'master_repo') - old_git_dir = join_path_native(repo_dir, '.git') - new_subdir = join_path_native(repo_dir, 'gitdir') - new_git_dir = join_path_native(new_subdir, 'git') + repo_dir = join_path_native(rw_dir, "master_repo") + old_git_dir = join_path_native(repo_dir, ".git") + new_subdir = join_path_native(repo_dir, "gitdir") + new_git_dir = join_path_native(new_subdir, "git") os.mkdir(new_subdir) os.rename(old_git_dir, new_git_dir) oldenv = os.environ.copy() - os.environ['GIT_DIR'] = new_git_dir - os.environ['GIT_WORK_TREE'] = repo_dir + os.environ["GIT_DIR"] = new_git_dir + os.environ["GIT_WORK_TREE"] = repo_dir try: r = Repo() @@ -1080,15 +1170,18 @@ class TestRepo(TestBase): @with_rw_directory def test_rebasing(self, rw_dir): r = Repo.init(rw_dir) - fp = osp.join(rw_dir, 'hello.txt') - r.git.commit("--allow-empty", message="init",) - with open(fp, 'w') as fs: + fp = osp.join(rw_dir, "hello.txt") + r.git.commit( + "--allow-empty", + message="init", + ) + with open(fp, "w") as fs: fs.write("hello world") r.git.add(Git.polish_url(fp)) r.git.commit(message="English") self.assertEqual(r.currently_rebasing_on(), None) r.git.checkout("HEAD^1") - with open(fp, 'w') as fs: + with open(fp, "w") as fs: fs.write("Hola Mundo") r.git.add(Git.polish_url(fp)) r.git.commit(message="Spanish") @@ -1102,9 +1195,11 @@ class TestRepo(TestBase): @with_rw_directory def test_do_not_strip_newline_in_stdout(self, rw_dir): r = Repo.init(rw_dir) - fp = osp.join(rw_dir, 'hello.txt') - with open(fp, 'w') as fs: + fp = osp.join(rw_dir, "hello.txt") + with open(fp, "w") as fs: fs.write("hello\n") r.git.add(Git.polish_url(fp)) r.git.commit(message="init") - self.assertEqual(r.git.show("HEAD:hello.txt", strip_newline_in_stdout=False), 'hello\n') + self.assertEqual( + r.git.show("HEAD:hello.txt", strip_newline_in_stdout=False), "hello\n" + ) |