|
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 | 7 | importcontextlib
|
| 8 | +importlogging |
8 | 9 | importos
|
9 | 10 | importos.pathasosp
|
| 11 | +importre |
10 | 12 | importshutil
|
11 | 13 | importsubprocess
|
12 | 14 | importsys
|
@@ -74,28 +76,51 @@ def test_it_transforms_kwargs_into_git_command_arguments(self):
|
74 | 76 | res=self.git.transform_kwargs(**{"s":True,"t":True})
|
75 | 77 | self.assertEqual({"-s","-t"},set(res))
|
76 | 78 |
|
77 |
| -@ddt.data( |
| 79 | +_shell_cases= ( |
| 80 | +# value_in_call, value_from_class, expected_popen_arg |
78 | 81 | (None,False,False),
|
79 | 82 | (None,True,True),
|
80 | 83 | (False,True,False),
|
81 | 84 | (False,False,False),
|
82 | 85 | (True,False,True),
|
83 | 86 | (True,True,True),
|
84 | 87 | )
|
| 88 | + |
| 89 | +@ddt.idata(_shell_cases) |
85 | 90 | @mock.patch.object(cmd,"Popen",wraps=cmd.Popen)# Since it is gotten via a "from" import.
|
86 | 91 | deftest_it_uses_shell_or_not_as_specified(self,case,mock_popen):
|
87 | 92 | """A bool passed as ``shell=`` takes precedence over `Git.USE_SHELL`."""
|
88 | 93 | value_in_call,value_from_class,expected_popen_arg=case
|
89 |
| -# FIXME: Check what gets logged too! |
| 94 | + |
90 | 95 | withmock.patch.object(Git,"USE_SHELL",value_from_class):
|
91 | 96 | withcontextlib.suppress(GitCommandError):
|
92 | 97 | self.git.execute(
|
93 | 98 | "git",# No args, so it runs with or without a shell, on all OSes.
|
94 | 99 | shell=value_in_call,
|
95 | 100 | )
|
| 101 | + |
96 | 102 | mock_popen.assert_called_once()
|
97 | 103 | self.assertIs(mock_popen.call_args.kwargs["shell"],expected_popen_arg)
|
98 | 104 |
|
| 105 | +@ddt.idata(full_case[:2]forfull_casein_shell_cases) |
| 106 | +@mock.patch.object(cmd,"Popen",wraps=cmd.Popen)# Since it is gotten via a "from" import. |
| 107 | +deftest_it_logs_if_it_uses_a_shell(self,case,mock_popen): |
| 108 | +"""``shell=`` in the log message agrees with what is passed to `Popen`.""" |
| 109 | +value_in_call,value_from_class=case |
| 110 | + |
| 111 | +withself.assertLogs(cmd.log,level=logging.DEBUG)aslog_watcher: |
| 112 | +withmock.patch.object(Git,"USE_SHELL",value_from_class): |
| 113 | +withcontextlib.suppress(GitCommandError): |
| 114 | +self.git.execute( |
| 115 | +"git",# No args, so it runs with or without a shell, on all OSes. |
| 116 | +shell=value_in_call, |
| 117 | + ) |
| 118 | + |
| 119 | +popen_shell_arg=mock_popen.call_args.kwargs["shell"] |
| 120 | +expected_message=re.compile(rf"DEBUG:git.cmd:Popen\(.*\bshell={popen_shell_arg}\b.*\)") |
| 121 | +match_attempts= [expected_message.fullmatch(message)formessageinlog_watcher.output] |
| 122 | +self.assertTrue(any(match_attempts),repr(log_watcher.output)) |
| 123 | + |
99 | 124 | deftest_it_executes_git_and_returns_result(self):
|
100 | 125 | self.assertRegex(self.git.execute(["git","version"]),r"^git version [\d\.]{2}.*$")
|
101 | 126 |
|
|