|
4 | 4 | #
|
5 | 5 | # This module is part of GitPython and is released under
|
6 | 6 | # the BSD License: https://opensource.org/license/bsd-3-clause/
|
| 7 | +importcontextlib |
| 8 | +importlogging |
7 | 9 | importos
|
| 10 | +importos.pathasosp |
| 11 | +importre |
8 | 12 | importshutil
|
9 | 13 | importsubprocess
|
10 | 14 | importsys
|
11 | 15 | fromtempfileimportTemporaryDirectory,TemporaryFile
|
12 |
| -fromunittestimportmock,skipUnless |
| 16 | +fromunittestimportskipUnless |
13 | 17 |
|
14 |
| -fromgitimportGit,refresh,GitCommandError,GitCommandNotFound,Repo,cmd |
15 |
| -fromtest.libimportTestBase,fixture_path |
16 |
| -fromtest.libimportwith_rw_directory |
17 |
| -fromgit.utilimportcwd,finalize_process |
| 18 | +ifsys.version_info>= (3,8): |
| 19 | +fromunittestimportmock |
| 20 | +else: |
| 21 | +importmock# To be able to examine call_args.kwargs on a mock. |
18 | 22 |
|
19 |
| -importos.pathasosp |
| 23 | +importddt |
20 | 24 |
|
| 25 | +fromgitimportGit,refresh,GitCommandError,GitCommandNotFound,Repo,cmd |
21 | 26 | fromgit.compatimportis_win
|
| 27 | +fromgit.utilimportcwd,finalize_process |
| 28 | +fromtest.libimportTestBase,fixture_path,with_rw_directory |
22 | 29 |
|
23 | 30 |
|
| 31 | +@ddt.ddt |
24 | 32 | classTestGit(TestBase):
|
25 | 33 | @classmethod
|
26 | 34 | defsetUpClass(cls):
|
@@ -73,7 +81,50 @@ def test_it_transforms_kwargs_into_git_command_arguments(self):
|
73 | 81 | res=self.git.transform_kwargs(**{"s":True,"t":True})
|
74 | 82 | self.assertEqual({"-s","-t"},set(res))
|
75 | 83 |
|
76 |
| -deftest_it_executes_git_to_shell_and_returns_result(self): |
| 84 | +_shell_cases= ( |
| 85 | +# value_in_call, value_from_class, expected_popen_arg |
| 86 | + (None,False,False), |
| 87 | + (None,True,True), |
| 88 | + (False,True,False), |
| 89 | + (False,False,False), |
| 90 | + (True,False,True), |
| 91 | + (True,True,True), |
| 92 | + ) |
| 93 | + |
| 94 | +def_do_shell_combo(self,value_in_call,value_from_class): |
| 95 | +withmock.patch.object(Git,"USE_SHELL",value_from_class): |
| 96 | +# git.cmd gets Popen via a "from" import, so patch it there. |
| 97 | +withmock.patch.object(cmd,"Popen",wraps=cmd.Popen)asmock_popen: |
| 98 | +# Use a command with no arguments (besides the program name), so it runs |
| 99 | +# with or without a shell, on all OSes, with the same effect. Since git |
| 100 | +# errors out when run with no arguments, we swallow that error. |
| 101 | +withcontextlib.suppress(GitCommandError): |
| 102 | +self.git.execute(["git"],shell=value_in_call) |
| 103 | + |
| 104 | +returnmock_popen |
| 105 | + |
| 106 | +@ddt.idata(_shell_cases) |
| 107 | +deftest_it_uses_shell_or_not_as_specified(self,case): |
| 108 | +"""A bool passed as ``shell=`` takes precedence over `Git.USE_SHELL`.""" |
| 109 | +value_in_call,value_from_class,expected_popen_arg=case |
| 110 | +mock_popen=self._do_shell_combo(value_in_call,value_from_class) |
| 111 | +mock_popen.assert_called_once() |
| 112 | +self.assertIs(mock_popen.call_args.kwargs["shell"],expected_popen_arg) |
| 113 | + |
| 114 | +@ddt.idata(full_case[:2]forfull_casein_shell_cases) |
| 115 | +deftest_it_logs_if_it_uses_a_shell(self,case): |
| 116 | +"""``shell=`` in the log message agrees with what is passed to `Popen`.""" |
| 117 | +value_in_call,value_from_class=case |
| 118 | + |
| 119 | +withself.assertLogs(cmd.log,level=logging.DEBUG)aslog_watcher: |
| 120 | +mock_popen=self._do_shell_combo(value_in_call,value_from_class) |
| 121 | + |
| 122 | +popen_shell_arg=mock_popen.call_args.kwargs["shell"] |
| 123 | +expected_message=re.compile(rf"DEBUG:git.cmd:Popen\(.*\bshell={popen_shell_arg}\b.*\)") |
| 124 | +match_attempts= [expected_message.fullmatch(message)formessageinlog_watcher.output] |
| 125 | +self.assertTrue(any(match_attempts),repr(log_watcher.output)) |
| 126 | + |
| 127 | +deftest_it_executes_git_and_returns_result(self): |
77 | 128 | self.assertRegex(self.git.execute(["git","version"]),r"^git version [\d\.]{2}.*$")
|
78 | 129 |
|
79 | 130 | deftest_it_executes_git_not_from_cwd(self):
|
|