|
6 | 6 | importcontextlib
|
7 | 7 | importgc
|
8 | 8 | importinspect
|
| 9 | +importio |
9 | 10 | importlogging
|
10 | 11 | importos
|
11 | 12 | importos.pathasosp
|
@@ -329,6 +330,80 @@ def test_cmd_override(self):
|
329 | 330 | ):
|
330 | 331 | self.assertRaises(GitCommandNotFound,self.git.version)
|
331 | 332 |
|
| 333 | +deftest_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 | +deftest_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 | +withmock.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 | +deftest_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 | +withmock.patch.dict(os.environ,env_vars): |
| 363 | +withcontextlib.redirect_stdout(io.StringIO())asout: |
| 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 | +deftest_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 | +withmock.patch.dict(os.environ,env_vars): |
| 380 | +withself.assertRaisesRegex(ImportError,r"\ABad git executable.\n"): |
| 381 | +refresh() |
| 382 | + |
| 383 | +deftest_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 | +withmock.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 | +deftest_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 | +withmock.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 | +withmock.patch.dict(os.environ, {"GIT_PYTHON_GIT_EXECUTABLE":"git"}): |
| 404 | +refresh() |
| 405 | +self.assertEqual(self.git.GIT_PYTHON_GIT_EXECUTABLE,"git") |
| 406 | + |
332 | 407 | deftest_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 | deftest_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 | +# Setas the executable namea 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.
|
|