Skip to content

Commit 98877c5

Browse files
committed
Refactor "finally" cleanup in tests, fix minor bug
This refactors code in the test suite that uses try-finally, to simplify and clarify what it is doing. An exception to this being a refactoring is that a possible bug is fixed where a throwaway environment variable "FOO" was patched for a test and never unpatched. There are two patterns refactored here: - try-(try-except)-finally to try-except-finally. When the entire suite of the try-block in try-finally is itself a try-except, that has the same effect as try-except-finally. (Python always attempts to run the finally suite in either case, and does so after any applicable except suite is run.) - Replacing try-finally with an appropriate context manager. (These changes do not fix, nor introduce, any flake8 errors, but they were found by manual search for possible problems related to recent flake8 findings but outside of what flake8 can catch. To limit scope, this only refactors try-finally in the test suite.)
1 parent 48441a9 commit 98877c5

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

Diff for: test/lib/helper.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,16 @@ def wrapper(self):
9494
os.mkdir(path)
9595
keep = False
9696
try:
97-
try:
98-
return func(self, path)
99-
except Exception:
100-
log.info(
101-
"Test %s.%s failed, output is at %r\n",
102-
type(self).__name__,
103-
func.__name__,
104-
path,
105-
)
106-
keep = True
107-
raise
97+
return func(self, path)
98+
except Exception:
99+
log.info(
100+
"Test %s.%s failed, output is at %r\n",
101+
type(self).__name__,
102+
func.__name__,
103+
path,
104+
)
105+
keep = True
106+
raise
108107
finally:
109108
# Need to collect here to be sure all handles have been closed. It appears
110109
# a windows-only issue. In fact things should be deleted, as well as
@@ -147,12 +146,11 @@ def repo_creator(self):
147146
prev_cwd = os.getcwd()
148147
os.chdir(rw_repo.working_dir)
149148
try:
150-
try:
151-
return func(self, rw_repo)
152-
except: # noqa E722
153-
log.info("Keeping repo after failure: %s", repo_dir)
154-
repo_dir = None
155-
raise
149+
return func(self, rw_repo)
150+
except: # noqa E722
151+
log.info("Keeping repo after failure: %s", repo_dir)
152+
repo_dir = None
153+
raise
156154
finally:
157155
os.chdir(prev_cwd)
158156
rw_repo.git.clear_cache()

Diff for: test/test_git.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,12 @@ def test_version(self):
195195
# END verify number types
196196

197197
def test_cmd_override(self):
198-
prev_cmd = self.git.GIT_PYTHON_GIT_EXECUTABLE
199-
exc = GitCommandNotFound
200-
try:
201-
# set it to something that doesn't exist, assure it raises
202-
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = osp.join(
203-
"some", "path", "which", "doesn't", "exist", "gitbinary"
204-
)
205-
self.assertRaises(exc, self.git.version)
206-
finally:
207-
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = prev_cmd
208-
# END undo adjustment
198+
with mock.patch.object(
199+
type(self.git),
200+
"GIT_PYTHON_GIT_EXECUTABLE",
201+
osp.join("some", "path", "which", "doesn't", "exist", "gitbinary"),
202+
):
203+
self.assertRaises(GitCommandNotFound, self.git.version)
209204

210205
def test_refresh(self):
211206
# test a bad git path refresh

Diff for: test/test_repo.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -847,18 +847,13 @@ def test_comparison_and_hash(self):
847847

848848
@with_rw_directory
849849
def test_tilde_and_env_vars_in_repo_path(self, rw_dir):
850-
ph = os.environ.get("HOME")
851-
try:
850+
with mock.patch.dict(os.environ, {"HOME": rw_dir}):
852851
os.environ["HOME"] = rw_dir
853852
Repo.init(osp.join("~", "test.git"), bare=True)
854853

854+
with mock.patch.dict(os.environ, {"FOO": rw_dir}):
855855
os.environ["FOO"] = rw_dir
856856
Repo.init(osp.join("$FOO", "test.git"), bare=True)
857-
finally:
858-
if ph:
859-
os.environ["HOME"] = ph
860-
del os.environ["FOO"]
861-
# end assure HOME gets reset to what it was
862857

863858
def test_git_cmd(self):
864859
# test CatFileContentStream, just to be very sure we have no fencepost errors

0 commit comments

Comments
 (0)