Skip to content

Commit

Permalink
Re-refresh to restore state after refresh tests
Browse files Browse the repository at this point in the history
For #1811. This is the simple way to do it. This relies on
git.refresh working when called when no arguments, so if that ever
breaks, then it may be difficult or inefficient to investigate. But
this is simpler than any alternatives that are equally or more
robust.

This is already better than the previous incomplete way that only
restored Git.GIT_PYTHON_GIT_EXECUTABLE (and nothing in FetchInfo).
Furthermore, because the tests already depend to a large extent on
git.refreh working when called with no arguments, as otherwise the
initial refresh may not have set Git.GIT_PYTHON_GIT_EXECUTABLE
usably, it might not be worthwhile to explore other approaches.
  • Loading branch information
EliahKagan committed Jan 25, 2024
1 parent 5ad7cb2 commit b3fc30e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def _patch_out_env(name):


@contextlib.contextmanager
def _restore_git_executable():
def _rollback_refresh():
old_git_executable = Git.GIT_PYTHON_GIT_EXECUTABLE
try:
yield old_git_executable # Let test code run that may rebind the attribute.
yield old_git_executable # Let test code run that may mutate class state.
finally:
Git.GIT_PYTHON_GIT_EXECUTABLE = old_git_executable
refresh()


@ddt.ddt
Expand Down Expand Up @@ -319,7 +319,7 @@ def test_refresh_bad_absolute_git_path(self):
absolute_path = str(Path("yada").absolute())
expected_pattern = rf"\n[ \t]*cmdline: {re.escape(absolute_path)}\Z"

with _restore_git_executable() as old_git_executable:
with _rollback_refresh() as old_git_executable:
with self.assertRaisesRegex(GitCommandNotFound, expected_pattern):
refresh(absolute_path)
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, old_git_executable)
Expand All @@ -328,15 +328,15 @@ def test_refresh_bad_relative_git_path(self):
absolute_path = str(Path("yada").absolute())
expected_pattern = rf"\n[ \t]*cmdline: {re.escape(absolute_path)}\Z"

with _restore_git_executable() as old_git_executable:
with _rollback_refresh() as old_git_executable:
with self.assertRaisesRegex(GitCommandNotFound, expected_pattern):
refresh("yada")
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, old_git_executable)

def test_refresh_good_absolute_git_path(self):
absolute_path = shutil.which("git")

with _restore_git_executable():
with _rollback_refresh():
refresh(absolute_path)
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, absolute_path)

Expand All @@ -345,7 +345,7 @@ def test_refresh_good_relative_git_path(self):
dirname, basename = osp.split(absolute_path)

with cwd(dirname):
with _restore_git_executable():
with _rollback_refresh():
refresh(basename)
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, absolute_path)

Expand Down

0 comments on commit b3fc30e

Please sign in to comment.