|
6 | 6 | import contextlib
|
7 | 7 | import gc
|
8 | 8 | import inspect
|
| 9 | +import io |
9 | 10 | import logging
|
10 | 11 | import os
|
11 | 12 | import os.path as osp
|
@@ -329,6 +330,80 @@ def test_cmd_override(self):
|
329 | 330 | ):
|
330 | 331 | self.assertRaises(GitCommandNotFound, self.git.version)
|
331 | 332 |
|
| 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 | + |
332 | 407 | def test_refresh_from_bad_absolute_git_path_env(self):
|
333 | 408 | """Bad absolute path from environment is reported and not set."""
|
334 | 409 | absolute_path = str(Path("yada").absolute())
|
@@ -365,7 +440,7 @@ def test_refresh_from_good_absolute_git_path_env(self):
|
365 | 440 | def test_refresh_from_good_relative_git_path_env(self):
|
366 | 441 | """Good relative path from environment is kept relative and set."""
|
367 | 442 | 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". |
369 | 444 | type(self.git).GIT_PYTHON_GIT_EXECUTABLE = ""
|
370 | 445 |
|
371 | 446 | # Now observe if setting the environment variable to "git" takes effect.
|
|
0 commit comments