Skip to content

Avoid brittle assumptions about preexisting temporary files in tests #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
EliahKagan committed Dec 8, 2023
commit c09ac1ab2c86a87080dcfb2fc0af64cbcc2bc9eb
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