Skip to content

Report actual attempted Git command when Git.refresh fails #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 26, 2024
Prev Previous commit
Next Next commit
Re-refresh to restore state after refresh tests
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
commit b3fc30eeb476e6d76a292b5a58788efa576fb9a1
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