|
3 | 3 | # This module is part of GitPython and is released under the |
4 | 4 | # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ |
5 | 5 |
|
| 6 | +import contextlib |
6 | 7 | import gc |
7 | 8 | import inspect |
8 | 9 | import logging |
|
12 | 13 | import shutil |
13 | 14 | import subprocess |
14 | 15 | import sys |
15 | | -from tempfile import TemporaryDirectory, TemporaryFile |
| 16 | +from tempfile import TemporaryFile |
16 | 17 | from unittest import skipUnless |
17 | 18 |
|
18 | 19 | if sys.version_info >= (3, 8): |
@@ -135,27 +136,35 @@ def test_it_executes_git_and_returns_result(self): |
135 | 136 | self.assertRegex(self.git.execute(["git", "version"]), r"^git version [\d\.]{2}.*$") |
136 | 137 |
|
137 | 138 | @ddt.data( |
138 | | - (["git", "version"], False), |
139 | | - ("git version", True), |
| 139 | + (False, False, ["git", "version"]), |
| 140 | + (False, True, "git version"), |
| 141 | + (True, False, ["git", "version"]), |
| 142 | + (True, True, "git version"), |
140 | 143 | ) |
141 | | - def test_it_executes_git_not_from_cwd(self, case): |
142 | | - command, shell = case |
143 | | - |
144 | | - with TemporaryDirectory() as tmpdir: |
145 | | - if os.name == "nt": |
146 | | - # Copy an actual binary executable that is not git. |
147 | | - other_exe_path = os.path.join(os.getenv("WINDIR"), "system32", "hostname.exe") |
148 | | - impostor_path = os.path.join(tmpdir, "git.exe") |
149 | | - shutil.copy(other_exe_path, impostor_path) |
150 | | - else: |
151 | | - # Create a shell script that doesn't do anything. |
152 | | - impostor_path = os.path.join(tmpdir, "git") |
153 | | - with open(impostor_path, mode="w", encoding="utf-8") as file: |
154 | | - print("#!/bin/sh", file=file) |
155 | | - os.chmod(impostor_path, 0o755) |
156 | | - |
157 | | - with cwd(tmpdir): |
158 | | - output = self.git.execute(command, shell=shell) |
| 144 | + @with_rw_directory |
| 145 | + def test_it_executes_git_not_from_cwd(self, rw_dir, case): |
| 146 | + chdir_to_repo, shell, command = case |
| 147 | + |
| 148 | + repo = Repo.init(rw_dir) |
| 149 | + |
| 150 | + if os.name == "nt": |
| 151 | + # Copy an actual binary executable that is not git. (On Windows, running |
| 152 | + # "hostname" only displays the hostname, it never tries to change it.) |
| 153 | + other_exe_path = os.path.join(os.getenv("WINDIR"), "system32", "hostname.exe") |
| 154 | + impostor_path = os.path.join(rw_dir, "git.exe") |
| 155 | + shutil.copy(other_exe_path, impostor_path) |
| 156 | + else: |
| 157 | + # Create a shell script that doesn't do anything. |
| 158 | + impostor_path = os.path.join(rw_dir, "git") |
| 159 | + with open(impostor_path, mode="w", encoding="utf-8") as file: |
| 160 | + print("#!/bin/sh", file=file) |
| 161 | + os.chmod(impostor_path, 0o755) |
| 162 | + |
| 163 | + with cwd(rw_dir) if chdir_to_repo else contextlib.nullcontext(): |
| 164 | + # Run the command without raising an exception on failure, as the exception |
| 165 | + # message is currently misleading when the command is a string rather than a |
| 166 | + # sequence of strings (it really runs "git", but then wrongly reports "g"). |
| 167 | + output = repo.git.execute(command, with_exceptions=False, shell=shell) |
159 | 168 |
|
160 | 169 | self.assertRegex(output, r"^git version\b") |
161 | 170 |
|
|
0 commit comments