Skip to content

Commit

Permalink
Merge pull request #1816 from EliahKagan/refresh-format
Browse files Browse the repository at this point in the history
Omit warning prefix in "Bad git executable" message
  • Loading branch information
Byron authored Feb 6, 2024
2 parents db6fb90 + 4b86993 commit 3749037
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
)

if mode in warn:
_logger.critical("WARNING: %s", err)
_logger.critical(err)
else:
raise ImportError(err)
else:
Expand Down
32 changes: 16 additions & 16 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,17 @@ def test_version(self):
self.assertIsInstance(n, int)
# END verify number types

def test_cmd_override(self):
with mock.patch.object(
type(self.git),
"GIT_PYTHON_GIT_EXECUTABLE",
osp.join("some", "path", "which", "doesn't", "exist", "gitbinary"),
):
self.assertRaises(GitCommandNotFound, self.git.version)

def test_git_exc_name_is_git(self):
self.assertEqual(self.git.git_exec_name, "git")

def test_cmd_override(self):
"""Directly set bad GIT_PYTHON_GIT_EXECUTABLE causes git operations to raise."""
bad_path = osp.join("some", "path", "which", "doesn't", "exist", "gitbinary")
with mock.patch.object(Git, "GIT_PYTHON_GIT_EXECUTABLE", bad_path):
with self.assertRaises(GitCommandNotFound) as ctx:
self.git.version()
self.assertEqual(ctx.exception.command, [bad_path, "version"])

@ddt.data(("0",), ("q",), ("quiet",), ("s",), ("silence",), ("silent",), ("n",), ("none",))
def test_initial_refresh_from_bad_git_path_env_quiet(self, case):
"""In "q" mode, bad initial path sets "git" and is quiet."""
Expand All @@ -341,7 +341,7 @@ def test_initial_refresh_from_bad_git_path_env_quiet(self, case):
"GIT_PYTHON_REFRESH": mode,
}
with _rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
Git.GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.

with mock.patch.dict(os.environ, set_vars):
refresh()
Expand All @@ -356,14 +356,14 @@ def test_initial_refresh_from_bad_git_path_env_warn(self, case):
"GIT_PYTHON_REFRESH": mode,
}
with _rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
Git.GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.

with mock.patch.dict(os.environ, env_vars):
with self.assertLogs(cmd.__name__, logging.CRITICAL) as ctx:
refresh()
self.assertEqual(len(ctx.records), 1)
message = ctx.records[0].getMessage()
self.assertRegex(message, r"\AWARNING: Bad git executable.\n")
self.assertRegex(message, r"\ABad git executable.\n")
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, "git")

@ddt.data(("2",), ("r",), ("raise",), ("e",), ("error",))
Expand All @@ -375,7 +375,7 @@ def test_initial_refresh_from_bad_git_path_env_error(self, case):
"GIT_PYTHON_REFRESH": mode,
}
with _rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
Git.GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.

with mock.patch.dict(os.environ, env_vars):
with self.assertRaisesRegex(ImportError, r"\ABad git executable.\n"):
Expand All @@ -386,7 +386,7 @@ def test_initial_refresh_from_good_absolute_git_path_env(self):
absolute_path = shutil.which("git")

with _rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
Git.GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.

with mock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE": absolute_path}):
refresh()
Expand All @@ -397,8 +397,8 @@ def test_initial_refresh_from_good_relative_git_path_env(self):
with _rollback_refresh():
# Set the fallback to a string that wouldn't work and isn't "git", so we are
# more likely to detect if "git" is not set from the environment variable.
with mock.patch.object(type(self.git), "git_exec_name", ""):
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
with mock.patch.object(Git, "git_exec_name", ""):
Git.GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.

# Now observe if setting the environment variable to "git" takes effect.
with mock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE": "git"}):
Expand Down Expand Up @@ -442,7 +442,7 @@ def test_refresh_from_good_relative_git_path_env(self):
"""Good relative path from environment is kept relative and set."""
with _rollback_refresh():
# Set as the executable name a string that wouldn't work and isn't "git".
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = ""
Git.GIT_PYTHON_GIT_EXECUTABLE = ""

# Now observe if setting the environment variable to "git" takes effect.
with mock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE": "git"}):
Expand Down
2 changes: 1 addition & 1 deletion test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _decode(stdout):
except UnicodeDecodeError:
pass
except LookupError as error:
_logger.warning("%s", str(error)) # Message already says "Unknown encoding:".
_logger.warning(str(error)) # Message already says "Unknown encoding:".

# Assume UTF-8. If invalid, substitute Unicode replacement characters.
return stdout.decode("utf-8", errors="replace")
Expand Down

0 comments on commit 3749037

Please sign in to comment.