Skip to content

Commit 4b518be

Browse files
committed
Start work on making refresh tests restore state
+ Test a successful refresh with a relative path, which will be safer to do once the refresh tests restore changed state. This is incomplete, because while it is probably not necessary while running the test suite to preserve an old False value of git.GIT_OK (most tests can't work if that happens anyway), the value of Git.GIT_PYTHON_GIT_EXECUTABLE is not the only other global state that effects the behavior of subsequently run tests and that may be changed as a result of the refresh tests. 1. After the git.refresh function calls git.cmd.Git.refresh, it calls git.remote.FetchInfo.refresh, which rebinds the git.remote.FetchInfo._flag_map attribute. 2. Future changes to git.cmd.Git.refresh may mutate other state in the Git class, and ideally the coupling would be loose enough that the refresh tests wouldn't have to be updated for that if the behavior being tested does not change. 3. Future changes to git.refresh may perform other refreshing actions, and ideally it would be easy (and obvious) what has to be done to patch it back. In particular, it will likely call another Git method that mutates class-wide state due to gitpython-developers#1791, and for such state that is also of the Git class, ideally no further changes would have to be made to the code that restores state after the refresh tests. If we assume git.refresh is working at least in the case that it is called with no arguments, then the cleanup can just be a call to git.refresh(). Otherwise, sufficiently general cleanup may be more complicated.
1 parent 3a34dee commit 4b518be

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

test/test_git.py

+33-8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ def _patch_out_env(name):
4444
os.environ[name] = old_value
4545

4646

47+
@contextlib.contextmanager
48+
def _restore_git_executable():
49+
old_git_executable = Git.GIT_PYTHON_GIT_EXECUTABLE
50+
try:
51+
yield old_git_executable # Let test code run that may rebind the attribute.
52+
finally:
53+
Git.GIT_PYTHON_GIT_EXECUTABLE = old_git_executable
54+
55+
4756
@ddt.ddt
4857
class TestGit(TestBase):
4958
@classmethod
@@ -309,20 +318,36 @@ def test_cmd_override(self):
309318
def test_refresh_bad_absolute_git_path(self):
310319
absolute_path = str(Path("yada").absolute())
311320
expected_pattern = rf"\n[ \t]*cmdline: {re.escape(absolute_path)}\Z"
312-
with self.assertRaisesRegex(GitCommandNotFound, expected_pattern):
313-
refresh(absolute_path)
321+
322+
with _restore_git_executable() as old_git_executable:
323+
with self.assertRaisesRegex(GitCommandNotFound, expected_pattern):
324+
refresh(absolute_path)
325+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, old_git_executable)
314326

315327
def test_refresh_bad_relative_git_path(self):
316-
relative_path = "yada"
317-
absolute_path = str(Path(relative_path).absolute())
328+
absolute_path = str(Path("yada").absolute())
318329
expected_pattern = rf"\n[ \t]*cmdline: {re.escape(absolute_path)}\Z"
319-
with self.assertRaisesRegex(GitCommandNotFound, expected_pattern):
320-
refresh(relative_path)
330+
331+
with _restore_git_executable() as old_git_executable:
332+
with self.assertRaisesRegex(GitCommandNotFound, expected_pattern):
333+
refresh("yada")
334+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, old_git_executable)
321335

322336
def test_refresh_good_absolute_git_path(self):
323337
absolute_path = shutil.which("git")
324-
refresh(absolute_path)
325-
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, absolute_path)
338+
339+
with _restore_git_executable():
340+
refresh(absolute_path)
341+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, absolute_path)
342+
343+
def test_refresh_good_relative_git_path(self):
344+
absolute_path = shutil.which("git")
345+
dirname, basename = osp.split(absolute_path)
346+
347+
with cwd(dirname):
348+
with _restore_git_executable():
349+
refresh(basename)
350+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, absolute_path)
326351

327352
def test_options_are_passed_to_git(self):
328353
# This works because any command after git --version is ignored.

0 commit comments

Comments
 (0)