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

Commitda3460c

Browse files
committed
Extract shared test logic to a helper
This also helps mock Popen over a smaller scope, which may bebeneficial (especially if it is mocked in the subprocess module,rather than the git.cmd module, in the future).
1 parent0f19fb0 commitda3460c

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

‎test/test_git.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,33 @@ def test_it_transforms_kwargs_into_git_command_arguments(self):
8686
(True,True,True),
8787
)
8888

89+
def_do_shell_combo(self,value_in_call,value_from_class):
90+
withmock.patch.object(Git,"USE_SHELL",value_from_class):
91+
# git.cmd gets Popen via a "from" import, so patch it there.
92+
withmock.patch.object(cmd,"Popen",wraps=cmd.Popen)asmock_popen:
93+
# Use a command with no arguments (besides the program name), so it runs
94+
# with or without a shell, on all OSes, with the same effect. Since git
95+
# errors out when run with no arguments, we swallow that error.
96+
withcontextlib.suppress(GitCommandError):
97+
self.git.execute(["git"],shell=value_in_call)
98+
99+
returnmock_popen
100+
89101
@ddt.idata(_shell_cases)
90-
@mock.patch.object(cmd,"Popen",wraps=cmd.Popen)# Since it is gotten via a "from" import.
91-
deftest_it_uses_shell_or_not_as_specified(self,case,mock_popen):
102+
deftest_it_uses_shell_or_not_as_specified(self,case):
92103
"""A bool passed as ``shell=`` takes precedence over `Git.USE_SHELL`."""
93104
value_in_call,value_from_class,expected_popen_arg=case
94-
95-
withmock.patch.object(Git,"USE_SHELL",value_from_class):
96-
withcontextlib.suppress(GitCommandError):
97-
self.git.execute(
98-
["git"],# No args, so it runs with or without a shell, on all OSes.
99-
shell=value_in_call,
100-
)
101-
105+
mock_popen=self._do_shell_combo(value_in_call,value_from_class)
102106
mock_popen.assert_called_once()
103107
self.assertIs(mock_popen.call_args.kwargs["shell"],expected_popen_arg)
104108

105109
@ddt.idata(full_case[:2]forfull_casein_shell_cases)
106-
@mock.patch.object(cmd,"Popen",wraps=cmd.Popen)# Since it is gotten via a "from" import.
107-
deftest_it_logs_if_it_uses_a_shell(self,case,mock_popen):
110+
deftest_it_logs_if_it_uses_a_shell(self,case):
108111
"""``shell=`` in the log message agrees with what is passed to `Popen`."""
109112
value_in_call,value_from_class=case
110113

111114
withself.assertLogs(cmd.log,level=logging.DEBUG)aslog_watcher:
112-
withmock.patch.object(Git,"USE_SHELL",value_from_class):
113-
withcontextlib.suppress(GitCommandError):
114-
self.git.execute(
115-
["git"],# No args, so it runs with or without a shell, on all OSes.
116-
shell=value_in_call,
117-
)
115+
mock_popen=self._do_shell_combo(value_in_call,value_from_class)
118116

119117
popen_shell_arg=mock_popen.call_args.kwargs["shell"]
120118
expected_message=re.compile(rf"DEBUG:git.cmd:Popen\(.*\bshell={popen_shell_arg}\b.*\)")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp