Skip to content

Commit ad570de

Browse files
committed
Avoid unsafe assumptions about tempdir content in tests
test_new_should_raise_on_invalid_repo_location had previously used tempfile.gettempdir() directly to get a path assumed not to be a valid repository, assuming more than necessary about the directory in which temporary files and directories are created. A newly created temporary directory is now used for that check, instead. test_new_should_raise_on_non_existent_path had assumed no repos/foobar existed relative to the current directory. Typically there would be no such directory, but is unnecessary for the test to rely on this. A newly created temporary directory known to be empty is now used in place of the "repos" component, for that test.
1 parent d7bf231 commit ad570de

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

test/test_repo.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,19 @@ def tearDown(self):
7171
for lfp in glob.glob(_tc_lock_fpaths):
7272
if osp.isfile(lfp):
7373
raise AssertionError("Previous TC left hanging git-lock file: {}".format(lfp))
74+
7475
import gc
7576

7677
gc.collect()
7778

7879
def test_new_should_raise_on_invalid_repo_location(self):
79-
self.assertRaises(InvalidGitRepositoryError, Repo, tempfile.gettempdir())
80+
with tempfile.TemporaryDirectory() as tdir:
81+
self.assertRaises(InvalidGitRepositoryError, Repo, tdir)
8082

8183
def test_new_should_raise_on_non_existent_path(self):
82-
self.assertRaises(NoSuchPathError, Repo, "repos/foobar")
84+
with tempfile.TemporaryDirectory() as tdir:
85+
nonexistent = osp.join(tdir, "foobar")
86+
self.assertRaises(NoSuchPathError, Repo, nonexistent)
8387

8488
@with_rw_repo("0.3.2.1")
8589
def test_repo_creation_from_different_paths(self, rw_repo):

0 commit comments

Comments
 (0)