@@ -44,6 +44,14 @@ def _patch_out_env(name):
44
44
os .environ [name ] = old_value
45
45
46
46
47
+ @contextlib .contextmanager
48
+ def _rollback_refresh ():
49
+ try :
50
+ yield Git .GIT_PYTHON_GIT_EXECUTABLE # Provide the old value for convenience.
51
+ finally :
52
+ refresh ()
53
+
54
+
47
55
@ddt .ddt
48
56
class TestGit (TestBase ):
49
57
@classmethod
@@ -306,14 +314,43 @@ def test_cmd_override(self):
306
314
):
307
315
self .assertRaises (GitCommandNotFound , self .git .version )
308
316
309
- def test_refresh (self ):
310
- # Test a bad git path refresh.
311
- self .assertRaises (GitCommandNotFound , refresh , "yada" )
312
-
313
- # Test a good path refresh.
314
- which_cmd = "where" if os .name == "nt" else "command -v"
315
- path = os .popen ("{0} git" .format (which_cmd )).read ().strip ().split ("\n " )[0 ]
316
- refresh (path )
317
+ def test_refresh_bad_absolute_git_path (self ):
318
+ """Bad absolute path arg is reported and not set."""
319
+ absolute_path = str (Path ("yada" ).absolute ())
320
+ expected_pattern = rf"\n[ \t]*cmdline: { re .escape (absolute_path )} \Z"
321
+
322
+ with _rollback_refresh () as old_git_executable :
323
+ with self .assertRaisesRegex (GitCommandNotFound , expected_pattern ):
324
+ refresh (absolute_path )
325
+ self .assertEqual (self .git .GIT_PYTHON_GIT_EXECUTABLE , old_git_executable )
326
+
327
+ def test_refresh_bad_relative_git_path (self ):
328
+ """Bad relative path arg is resolved to absolute path and reported, not set."""
329
+ absolute_path = str (Path ("yada" ).absolute ())
330
+ expected_pattern = rf"\n[ \t]*cmdline: { re .escape (absolute_path )} \Z"
331
+
332
+ with _rollback_refresh () as old_git_executable :
333
+ with self .assertRaisesRegex (GitCommandNotFound , expected_pattern ):
334
+ refresh ("yada" )
335
+ self .assertEqual (self .git .GIT_PYTHON_GIT_EXECUTABLE , old_git_executable )
336
+
337
+ def test_refresh_good_absolute_git_path (self ):
338
+ """Good absolute path arg is set."""
339
+ absolute_path = shutil .which ("git" )
340
+
341
+ with _rollback_refresh ():
342
+ refresh (absolute_path )
343
+ self .assertEqual (self .git .GIT_PYTHON_GIT_EXECUTABLE , absolute_path )
344
+
345
+ def test_refresh_good_relative_git_path (self ):
346
+ """Good relative path arg is resolved to absolute path and set."""
347
+ absolute_path = shutil .which ("git" )
348
+ dirname , basename = osp .split (absolute_path )
349
+
350
+ with cwd (dirname ):
351
+ with _rollback_refresh ():
352
+ refresh (basename )
353
+ self .assertEqual (self .git .GIT_PYTHON_GIT_EXECUTABLE , absolute_path )
317
354
318
355
def test_options_are_passed_to_git (self ):
319
356
# This works because any command after git --version is ignored.
0 commit comments