Skip to content

Commit 9fa1cee

Browse files
committed
Extract a _assert_logged_for_popen method
This extracts the logic of searching log messages, and asserting that (at least) one matches a pattern for the report of a Popen call with a given argument, from test_it_logs_if_it_uses_a_shell into a new nonpublic test helper method _assert_logged_for_popen. The extracted version is modified to make it slightly more general, and slightly more robust. This is still not extremely robust: the notation used to log Popen calls is informal, so it wouldn't make sense to really parse it as code. But this no longer assumes that the representation of a value ends at a word boundary, nor that the value is free of regular expression metacharacters.
1 parent a8a43fe commit 9fa1cee

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Diff for: test/test_git.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ def tearDown(self):
4040

4141
gc.collect()
4242

43+
def _assert_logged_for_popen(self, log_watcher, name, value):
44+
re_name = re.escape(name)
45+
re_value = re.escape(str(value))
46+
re_line = re.compile(fr"DEBUG:git.cmd:Popen\(.*\b{re_name}={re_value}[,)]")
47+
match_attempts = [re_line.match(message) for message in log_watcher.output]
48+
self.assertTrue(any(match_attempts), repr(log_watcher.output))
49+
4350
@mock.patch.object(Git, "execute")
4451
def test_call_process_calls_execute(self, git):
4552
git.return_value = ""
@@ -113,14 +120,9 @@ def test_it_uses_shell_or_not_as_specified(self, case):
113120
def test_it_logs_if_it_uses_a_shell(self, case):
114121
"""``shell=`` in the log message agrees with what is passed to `Popen`."""
115122
value_in_call, value_from_class = case
116-
117123
with self.assertLogs(cmd.log, level=logging.DEBUG) as log_watcher:
118124
mock_popen = self._do_shell_combo(value_in_call, value_from_class)
119-
120-
popen_shell_arg = mock_popen.call_args.kwargs["shell"]
121-
expected_message = re.compile(rf"DEBUG:git.cmd:Popen\(.*\bshell={popen_shell_arg}\b.*\)")
122-
match_attempts = [expected_message.fullmatch(message) for message in log_watcher.output]
123-
self.assertTrue(any(match_attempts), repr(log_watcher.output))
125+
self._assert_logged_for_popen(log_watcher, "shell", mock_popen.call_args.kwargs["shell"])
124126

125127
def test_it_executes_git_and_returns_result(self):
126128
self.assertRegex(self.git.execute(["git", "version"]), r"^git version [\d\.]{2}.*$")

0 commit comments

Comments
 (0)