Skip to content

Commit 147e80b

Browse files
committed
Test current initial-refresh behavior
This adds tests of the initial refresh that is attempted automatically on import. All the refresh tests prior to this point test subsequent refreshes. Those tests are kept, and new ones are added that simulate the condition of not having yet done the initial refresh by setting Git.GIT_PYTHON_GIT_EXECUTABLE to None. Some current behavior these tests assert may change for gitpython-developers#1808.
1 parent 8dc8eb9 commit 147e80b

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

Diff for: test/test_git.py

+76-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import contextlib
77
import gc
88
import inspect
9+
import io
910
import logging
1011
import os
1112
import os.path as osp
@@ -329,6 +330,80 @@ def test_cmd_override(self):
329330
):
330331
self.assertRaises(GitCommandNotFound, self.git.version)
331332

333+
def test_git_exc_name_is_git(self):
334+
self.assertEqual(self.git.git_exec_name, "git")
335+
336+
@ddt.data(("0",), ("q",), ("quiet",), ("s",), ("silence",), ("n",), ("none",))
337+
def test_initial_refresh_from_bad_git_path_env_quiet(self, case):
338+
"""In "q" mode, bad initial path sets "git" and is quiet."""
339+
(mode,) = case
340+
set_vars = {
341+
"GIT_PYTHON_GIT_EXECUTABLE": str(Path("yada").absolute()), # Any bad path.
342+
"GIT_PYTHON_REFRESH": mode,
343+
}
344+
with _rollback_refresh():
345+
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
346+
347+
with mock.patch.dict(os.environ, set_vars):
348+
refresh()
349+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, "git")
350+
351+
@ddt.data(("1",), ("w",), ("warn",), ("warning",))
352+
def test_initial_refresh_from_bad_git_path_env_warn(self, case):
353+
"""In "w" mode, bad initial path sets "git" and warns."""
354+
(mode,) = case
355+
env_vars = {
356+
"GIT_PYTHON_GIT_EXECUTABLE": str(Path("yada").absolute()), # Any bad path.
357+
"GIT_PYTHON_REFRESH": mode,
358+
}
359+
with _rollback_refresh():
360+
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
361+
362+
with mock.patch.dict(os.environ, env_vars):
363+
with contextlib.redirect_stdout(io.StringIO()) as out:
364+
refresh()
365+
self.assertRegex(out.getvalue(), r"\AWARNING: Bad git executable.\n")
366+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, "git")
367+
368+
@ddt.data(("2",), ("r",), ("raise",), ("e",), ("error",))
369+
def test_initial_refresh_from_bad_git_path_env_error(self, case):
370+
"""In "e" mode, bad initial path raises an exception."""
371+
(mode,) = case
372+
env_vars = {
373+
"GIT_PYTHON_GIT_EXECUTABLE": str(Path("yada").absolute()), # Any bad path.
374+
"GIT_PYTHON_REFRESH": mode,
375+
}
376+
with _rollback_refresh():
377+
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
378+
379+
with mock.patch.dict(os.environ, env_vars):
380+
with self.assertRaisesRegex(ImportError, r"\ABad git executable.\n"):
381+
refresh()
382+
383+
def test_initial_refresh_from_good_absolute_git_path_env(self):
384+
"""Good initial absolute path from environment is set."""
385+
absolute_path = shutil.which("git")
386+
387+
with _rollback_refresh():
388+
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
389+
390+
with mock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE": absolute_path}):
391+
refresh()
392+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, absolute_path)
393+
394+
def test_initial_refresh_from_good_relative_git_path_env(self):
395+
"""Good initial relative path from environment is kept relative and set."""
396+
with _rollback_refresh():
397+
# Set the fallback to a string that wouldn't work and isn't "git", so we are
398+
# more likely to detect if "git" is not set from the environment variable.
399+
with mock.patch.object(type(self.git), "git_exec_name", ""):
400+
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.
401+
402+
# Now observe if setting the environment variable to "git" takes effect.
403+
with mock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE": "git"}):
404+
refresh()
405+
self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE, "git")
406+
332407
def test_refresh_from_bad_absolute_git_path_env(self):
333408
"""Bad absolute path from environment is reported and not set."""
334409
absolute_path = str(Path("yada").absolute())
@@ -365,7 +440,7 @@ def test_refresh_from_good_absolute_git_path_env(self):
365440
def test_refresh_from_good_relative_git_path_env(self):
366441
"""Good relative path from environment is kept relative and set."""
367442
with _rollback_refresh():
368-
# Set a string that wouldn't work and isn't "git".
443+
# Set as the executable name a string that wouldn't work and isn't "git".
369444
type(self.git).GIT_PYTHON_GIT_EXECUTABLE = ""
370445

371446
# Now observe if setting the environment variable to "git" takes effect.

0 commit comments

Comments
 (0)