Skip to content

Commit 9277ff5

Browse files
committed
Avoid another tempdir content assumption in test
test_init was using tempfile.gettempdir() directly to get the location where the hard-coded path repos/foo/bar.git would be used to test repository creation with relative and absolute paths. That reused the same location each time, and also assumed the directory would be usable, which could fail due to previous runs or due to the path being used separately from GitPython's tests. This commit fixes that by using that path inside a temporary directory, known at the start of the test to be empty. Reorganizing the acquision and cleanup logic also has had the effect of causing the test no longer to be skipped due to the logic in git.util.rmtree due to the final cleanup attempt (after all assertions). The directory is now successfully removed on Windows, and the test passes on all platforms.
1 parent ad570de commit 9277ff5

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

test/test_repo.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
UnsafeProtocolError,
4242
)
4343
from git.repo.fun import touch
44-
from git.util import bin_to_hex, cygpath, join_path_native, rmfile, rmtree
44+
from git.util import bin_to_hex, cwd, cygpath, join_path_native, rmfile, rmtree
4545
from test.lib import TestBase, fixture, with_rw_directory, with_rw_repo
4646

4747

@@ -511,13 +511,11 @@ def write(self, b):
511511
repo.git.log(n=100, output_stream=TestOutputStream(io.DEFAULT_BUFFER_SIZE))
512512

513513
def test_init(self):
514-
prev_cwd = os.getcwd()
515-
os.chdir(tempfile.gettempdir())
516-
git_dir_rela = "repos/foo/bar.git"
517-
del_dir_abs = osp.abspath("repos")
518-
git_dir_abs = osp.abspath(git_dir_rela)
519-
try:
520-
# with specific path
514+
with tempfile.TemporaryDirectory() as tdir, cwd(tdir):
515+
git_dir_rela = "repos/foo/bar.git"
516+
git_dir_abs = osp.abspath(git_dir_rela)
517+
518+
# With specific path
521519
for path in (git_dir_rela, git_dir_abs):
522520
r = Repo.init(path=path, bare=True)
523521
self.assertIsInstance(r, Repo)
@@ -527,7 +525,7 @@ def test_init(self):
527525

528526
self._assert_empty_repo(r)
529527

530-
# test clone
528+
# Test clone
531529
clone_path = path + "_clone"
532530
rc = r.clone(clone_path)
533531
self._assert_empty_repo(rc)
@@ -562,13 +560,6 @@ def test_init(self):
562560
assert not r.has_separate_working_tree()
563561

564562
self._assert_empty_repo(r)
565-
finally:
566-
try:
567-
rmtree(del_dir_abs)
568-
except OSError:
569-
pass
570-
os.chdir(prev_cwd)
571-
# END restore previous state
572563

573564
def test_bare_property(self):
574565
self.rorepo.bare

0 commit comments

Comments
 (0)