Skip to content

Commit ac50d8e

Browse files
committed
Change warning refresh-mode tests to expect logging
This is instead of the current behavior writing the message to stdout. This commit does not change the behavior of the code under test, but it changes tests to assert the following: - "Bad git executable" messages are logged, at level CRITICAL. - "log" (and "l") is recognized as another synonym of "warn". - "silent" is recognized as a synonym of "quiet" (as "silence" is). Although it is ambiguous whether this should be logged at level ERROR or CRITICAL, because git.refresh is still expected to be usable and can be called manually, not having a working git is a condition in which GitPython, and any program that really relies on its functionality, should be expected not work. That is the general rationale for using CRIICAL here. There are also two specific reasons: - Existing messages GitPython logs as ERROR typically represent errors in individual operations on repositories, which could fail without indicating that GitPython's overall functionality is in an unusable state. Using the higher CRITICAL level for this situation makes sense for contrast. - Prior to #1813, logging messsges emitted from GitPython modules, no matter the level, were suppressed when logging was not configured, but because this message was printed instead of being logged, it was still shown. Now that it is to be logged, there may be a benefit to have an easy way for someone to bring back a close approximation of the old behavior. Having this message be at a higher logging level makes that easier to do. (This is a less important reason, as that should rarely be done.) test_initial_refresh_from_bad_git_path_env_warn is the main changed test. All tests should pass again once code is changed for #1808.
1 parent 147e80b commit ac50d8e

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Diff for: test/test_git.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import contextlib
77
import gc
88
import inspect
9-
import io
109
import logging
1110
import os
1211
import os.path as osp
@@ -333,7 +332,7 @@ def test_cmd_override(self):
333332
def test_git_exc_name_is_git(self):
334333
self.assertEqual(self.git.git_exec_name, "git")
335334

336-
@ddt.data(("0",), ("q",), ("quiet",), ("s",), ("silence",), ("n",), ("none",))
335+
@ddt.data(("0",), ("q",), ("quiet",), ("s",), ("silence",), ("silent",), ("n",), ("none",))
337336
def test_initial_refresh_from_bad_git_path_env_quiet(self, case):
338337
"""In "q" mode, bad initial path sets "git" and is quiet."""
339338
(mode,) = case
@@ -348,9 +347,9 @@ def test_initial_refresh_from_bad_git_path_env_quiet(self, case):
348347
refresh()
349348
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, "git")
350349

351-
@ddt.data(("1",), ("w",), ("warn",), ("warning",))
350+
@ddt.data(("1",), ("w",), ("warn",), ("warning",), ("l",), ("log",))
352351
def test_initial_refresh_from_bad_git_path_env_warn(self, case):
353-
"""In "w" mode, bad initial path sets "git" and warns."""
352+
"""In "w" mode, bad initial path sets "git" and warns, by logging."""
354353
(mode,) = case
355354
env_vars = {
356355
"GIT_PYTHON_GIT_EXECUTABLE": str(Path("yada").absolute()), # Any bad path.
@@ -360,9 +359,11 @@ def test_initial_refresh_from_bad_git_path_env_warn(self, case):
360359
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
361360

362361
with mock.patch.dict(os.environ, env_vars):
363-
with contextlib.redirect_stdout(io.StringIO()) as out:
362+
with self.assertLogs(cmd.__name__, logging.CRITICAL) as ctx:
364363
refresh()
365-
self.assertRegex(out.getvalue(), r"\AWARNING: Bad git executable.\n")
364+
self.assertEqual(len(ctx.records), 1)
365+
message = ctx.records[0].getMessage()
366+
self.assertRegex(message, r"\AWARNING: Bad git executable.\n")
366367
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, "git")
367368

368369
@ddt.data(("2",), ("r",), ("raise",), ("e",), ("error",))

0 commit comments

Comments
 (0)