From c09ac1ab2c86a87080dcfb2fc0af64cbcc2bc9eb Mon Sep 17 00:00:00 2001 From: Travis Runner Date: Fri, 8 Dec 2023 01:18:54 -0500 Subject: [PATCH] Test InvalidGitRepositoryError in repo subdir A Repo object can of course be constructed from a path to a directory that is the root of an existing repository, and raises InvalidGitRepositoryError on a directory that is outside of any repository. Tests intended to show both conditions already exist. This adds a test to verify that InvalidGitRepositoryError is also raised when an attempt is made to construct a Repo object from a path to a directory that is not the root of a repository but that is known to be located inside an existing git repository. --- test/test_repo.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/test_repo.py b/test/test_repo.py index 845fabf15..836862b0c 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -77,9 +77,20 @@ def tearDown(self): gc.collect() def test_new_should_raise_on_invalid_repo_location(self): + # Ideally this tests a directory that is outside of any repository. In the rare + # case tempfile.gettempdir() is inside a repo, this still passes, but tests the + # same scenario as test_new_should_raise_on_invalid_repo_location_within_repo. with tempfile.TemporaryDirectory() as tdir: self.assertRaises(InvalidGitRepositoryError, Repo, tdir) + @with_rw_directory + def test_new_should_raise_on_invalid_repo_location_within_repo(self, rw_dir): + repo_dir = osp.join(rw_dir, "repo") + Repo.init(repo_dir) + subdir = osp.join(repo_dir, "subdir") + os.mkdir(subdir) + self.assertRaises(InvalidGitRepositoryError, Repo, subdir) + def test_new_should_raise_on_non_existent_path(self): with tempfile.TemporaryDirectory() as tdir: nonexistent = osp.join(tdir, "foobar") @@ -122,7 +133,7 @@ def test_tree_from_revision(self): self.assertEqual(tree.type, "tree") self.assertEqual(self.rorepo.tree(tree), tree) - # try from invalid revision that does not exist + # Try from an invalid revision that does not exist. self.assertRaises(BadName, self.rorepo.tree, "hello world") def test_pickleable(self):