Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98877c5

Browse files
committed
Refactor "finally" cleanup in tests, fix minor bug
This refactors code in the test suite that uses try-finally, tosimplify and clarify what it is doing. An exception to this beinga refactoring is that a possible bug is fixed where a throwawayenvironment variable "FOO" was patched for a test and neverunpatched.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, butthey were found by manual search for possible problems related torecent flake8 findings but outside of what flake8 can catch. Tolimit scope, this only refactors try-finally in the test suite.)
1 parent48441a9 commit98877c5

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

‎test/lib/helper.py

Lines changed: 15 additions & 17 deletions
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-
returnfunc(self,path)
99-
exceptException:
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+
returnfunc(self,path)
98+
exceptException:
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-
returnfunc(self,rw_repo)
152-
except:# noqa E722
153-
log.info("Keeping repo after failure: %s",repo_dir)
154-
repo_dir=None
155-
raise
149+
returnfunc(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()

‎test/test_git.py

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

197197
deftest_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+
withmock.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
deftest_refresh(self):
211206
# test a bad git path refresh

‎test/test_repo.py

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

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

854+
withmock.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-
ifph:
859-
os.environ["HOME"]=ph
860-
delos.environ["FOO"]
861-
# end assure HOME gets reset to what it was
862857

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp