Skip to content

Commit

Permalink
Test InvalidGitRepositoryError in repo subdir
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliahKagan committed Dec 8, 2023
1 parent 9277ff5 commit c09ac1a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit c09ac1a

Please sign in to comment.