From b3fc30eeb476e6d76a292b5a58788efa576fb9a1 Mon Sep 17 00:00:00 2001 From: Eliah Kagan Date: Wed, 24 Jan 2024 19:40:31 -0500 Subject: [PATCH] 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. --- test/test_git.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/test_git.py b/test/test_git.py index 9b6059098..080486a55 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -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 @@ -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) @@ -328,7 +328,7 @@ 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) @@ -336,7 +336,7 @@ def test_refresh_bad_relative_git_path(self): 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) @@ -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)