Skip to content

Commit

Permalink
Avoid another tempdir content assumption in test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EliahKagan committed Dec 8, 2023
1 parent ad570de commit 9277ff5
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
UnsafeProtocolError,
)
from git.repo.fun import touch
from git.util import bin_to_hex, cygpath, join_path_native, rmfile, rmtree
from git.util import bin_to_hex, cwd, cygpath, join_path_native, rmfile, rmtree
from test.lib import TestBase, fixture, with_rw_directory, with_rw_repo


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

def test_init(self):
prev_cwd = os.getcwd()
os.chdir(tempfile.gettempdir())
git_dir_rela = "repos/foo/bar.git"
del_dir_abs = osp.abspath("repos")
git_dir_abs = osp.abspath(git_dir_rela)
try:
# with specific path
with tempfile.TemporaryDirectory() as tdir, cwd(tdir):
git_dir_rela = "repos/foo/bar.git"
git_dir_abs = osp.abspath(git_dir_rela)

# With specific path
for path in (git_dir_rela, git_dir_abs):
r = Repo.init(path=path, bare=True)
self.assertIsInstance(r, Repo)
Expand All @@ -527,7 +525,7 @@ def test_init(self):

self._assert_empty_repo(r)

# test clone
# Test clone
clone_path = path + "_clone"
rc = r.clone(clone_path)
self._assert_empty_repo(rc)
Expand Down Expand Up @@ -562,13 +560,6 @@ def test_init(self):
assert not r.has_separate_working_tree()

self._assert_empty_repo(r)
finally:
try:
rmtree(del_dir_abs)
except OSError:
pass
os.chdir(prev_cwd)
# END restore previous state

def test_bare_property(self):
self.rorepo.bare
Expand Down

0 comments on commit 9277ff5

Please sign in to comment.