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

Have initial refresh use a logger to warn#1815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Byron merged 4 commits intogitpython-developers:mainfromEliahKagan:refresh-env
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletionsgit/cmd.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -409,15 +409,15 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
# expect GIT_PYTHON_REFRESH to either be unset or be one of the
# following values:
#
# 0|q|quiet|s|silence|n|none
# 1|w|warn|warning
# 2|r|raise|e|error
# 0|q|quiet|s|silence|silent|n|none
# 1|w|warn|warning|l|log
# 2|r|raise|e|error|exception

mode=os.environ.get(cls._refresh_env_var,"raise").lower()

quiet= ["quiet","q","silence","s","none","n","0"]
warn= ["warn","w","warning","1"]
error= ["error","e","raise","r","2"]
quiet= ["quiet","q","silence","s","silent","none","n","0"]
warn= ["warn","w","warning","log","l","1"]
error= ["error","e","exception","raise","r","2"]

ifmodeinquiet:
pass
Expand All@@ -428,10 +428,10 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
%s
All git commands will error until this is rectified.
This initialwarning can be silenced or aggravated in the future by setting the
This initialmessage can be silenced or aggravated in the future by setting the
$%s environment variable. Use one of the following values:
- %s: for nowarning or exception
- %s: for aprintedwarning
- %s: for nomessage or exception
- %s: for a warning message (logged at level CRITICAL, displayed by default)
- %s: for a raised exception
Example:
Expand All@@ -450,7 +450,7 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
)

ifmodeinwarn:
print("WARNING: %s"%err)
_logger.critical("WARNING: %s",err)
else:
raiseImportError(err)
else:
Expand All@@ -460,8 +460,8 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
%s environment variable has been set but it has been set with an invalid value.
Use only the following values:
- %s: for nowarning or exception
- %s: for aprintedwarning
- %s: for nomessage or exception
- %s: for a warning message (logged at level CRITICAL, displayed by default)
- %s: for a raised exception
"""
)
Expand Down
145 changes: 140 additions & 5 deletionstest/test_git.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,9 +46,24 @@ def _patch_out_env(name):

@contextlib.contextmanager
def_rollback_refresh():
old_git_executable=Git.GIT_PYTHON_GIT_EXECUTABLE

ifold_git_executableisNone:
raiseRuntimeError("no executable string (need initial refresh before test)")

try:
yieldGit.GIT_PYTHON_GIT_EXECUTABLE# Provide the old value for convenience.
yieldold_git_executable# Provide the old value for convenience.
finally:
# The cleanup refresh should always raise an exception if it fails, since if it
# fails then previously discovered test results could be misleading and, more
# importantly, subsequent tests may be unable to run or give misleading results.
# So pre-set a non-None value, so that the cleanup will be a "second" refresh.
# This covers cases where a test has set it to None to test a "first" refresh.
Git.GIT_PYTHON_GIT_EXECUTABLE=Git.git_exec_name

# Do the cleanup refresh. This sets Git.GIT_PYTHON_GIT_EXECUTABLE to old_value
# in most cases. The reason to call it is to achieve other associated state
# changes as well, which include updating attributes of the FetchInfo class.
refresh()


Expand DownExpand Up@@ -314,7 +329,127 @@ def test_cmd_override(self):
):
self.assertRaises(GitCommandNotFound,self.git.version)

deftest_refresh_bad_absolute_git_path(self):
deftest_git_exc_name_is_git(self):
self.assertEqual(self.git.git_exec_name,"git")

@ddt.data(("0",), ("q",), ("quiet",), ("s",), ("silence",), ("silent",), ("n",), ("none",))
deftest_initial_refresh_from_bad_git_path_env_quiet(self,case):
"""In "q" mode, bad initial path sets "git" and is quiet."""
(mode,)=case
set_vars= {
"GIT_PYTHON_GIT_EXECUTABLE":str(Path("yada").absolute()),# Any bad path.
"GIT_PYTHON_REFRESH":mode,
}
with_rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE=None# Simulate startup.

withmock.patch.dict(os.environ,set_vars):
refresh()
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,"git")

@ddt.data(("1",), ("w",), ("warn",), ("warning",), ("l",), ("log",))
deftest_initial_refresh_from_bad_git_path_env_warn(self,case):
"""In "w" mode, bad initial path sets "git" and warns, by logging."""
(mode,)=case
env_vars= {
"GIT_PYTHON_GIT_EXECUTABLE":str(Path("yada").absolute()),# Any bad path.
"GIT_PYTHON_REFRESH":mode,
}
with_rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE=None# Simulate startup.

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

@ddt.data(("2",), ("r",), ("raise",), ("e",), ("error",))
deftest_initial_refresh_from_bad_git_path_env_error(self,case):
"""In "e" mode, bad initial path raises an exception."""
(mode,)=case
env_vars= {
"GIT_PYTHON_GIT_EXECUTABLE":str(Path("yada").absolute()),# Any bad path.
"GIT_PYTHON_REFRESH":mode,
}
with_rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE=None# Simulate startup.

withmock.patch.dict(os.environ,env_vars):
withself.assertRaisesRegex(ImportError,r"\ABad git executable.\n"):
refresh()

deftest_initial_refresh_from_good_absolute_git_path_env(self):
"""Good initial absolute path from environment is set."""
absolute_path=shutil.which("git")

with_rollback_refresh():
type(self.git).GIT_PYTHON_GIT_EXECUTABLE=None# Simulate startup.

withmock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE":absolute_path}):
refresh()
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,absolute_path)

deftest_initial_refresh_from_good_relative_git_path_env(self):
"""Good initial relative path from environment is kept relative and set."""
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.
withmock.patch.object(type(self.git),"git_exec_name",""):
type(self.git).GIT_PYTHON_GIT_EXECUTABLE=None# Simulate startup.

# Now observe if setting the environment variable to "git" takes effect.
withmock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE":"git"}):
refresh()
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,"git")

deftest_refresh_from_bad_absolute_git_path_env(self):
"""Bad absolute path from environment is reported and not set."""
absolute_path=str(Path("yada").absolute())
expected_pattern=rf"\n[ \t]*cmdline:{re.escape(absolute_path)}\Z"

with_rollback_refresh()asold_git_executable:
withmock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE":absolute_path}):
withself.assertRaisesRegex(GitCommandNotFound,expected_pattern):
refresh()
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,old_git_executable)

deftest_refresh_from_bad_relative_git_path_env(self):
"""Bad relative path from environment is kept relative and reported, not set."""
# Relative paths are not resolved when refresh() is called with no arguments, so
# use a string that's very unlikely to be a command name found in a path lookup.
relative_path="yada-e47e70c6-acbf-40f8-ad65-13af93c2195b"
expected_pattern=rf"\n[ \t]*cmdline:{re.escape(relative_path)}\Z"

with_rollback_refresh()asold_git_executable:
withmock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE":relative_path}):
withself.assertRaisesRegex(GitCommandNotFound,expected_pattern):
refresh()
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,old_git_executable)

deftest_refresh_from_good_absolute_git_path_env(self):
"""Good absolute path from environment is set."""
absolute_path=shutil.which("git")

with_rollback_refresh():
withmock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE":absolute_path}):
refresh()
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,absolute_path)

deftest_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=""

# Now observe if setting the environment variable to "git" takes effect.
withmock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE":"git"}):
refresh()
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,"git")

deftest_refresh_with_bad_absolute_git_path_arg(self):
"""Bad absolute path arg is reported and not set."""
absolute_path=str(Path("yada").absolute())
expected_pattern=rf"\n[ \t]*cmdline:{re.escape(absolute_path)}\Z"
Expand All@@ -324,7 +459,7 @@ def test_refresh_bad_absolute_git_path(self):
refresh(absolute_path)
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,old_git_executable)

deftest_refresh_bad_relative_git_path(self):
deftest_refresh_with_bad_relative_git_path_arg(self):
"""Bad relative path arg is resolved to absolute path and reported, not set."""
absolute_path=str(Path("yada").absolute())
expected_pattern=rf"\n[ \t]*cmdline:{re.escape(absolute_path)}\Z"
Expand All@@ -334,15 +469,15 @@ def test_refresh_bad_relative_git_path(self):
refresh("yada")
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,old_git_executable)

deftest_refresh_good_absolute_git_path(self):
deftest_refresh_with_good_absolute_git_path_arg(self):
"""Good absolute path arg is set."""
absolute_path=shutil.which("git")

with_rollback_refresh():
refresh(absolute_path)
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,absolute_path)

deftest_refresh_good_relative_git_path(self):
deftest_refresh_with_good_relative_git_path_arg(self):
"""Good relative path arg is resolved to absolute path and set."""
absolute_path=shutil.which("git")
dirname,basename=osp.split(absolute_path)
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp