diff --git a/tests.py b/tests.py index fcd9221..6f05d3f 100644 --- a/tests.py +++ b/tests.py @@ -2,6 +2,7 @@ import locale import os import posixpath +import re import shutil import subprocess import sys @@ -928,12 +929,25 @@ class VCSHelper: # override in subclasses command = None # type: Optional[str] + extra_env = {} + + @property + def version(self): + if not hasattr(self, '_version'): + if not self.is_installed(): + self._version = None + return self._version + + @property + def version_tuple(self): + return tuple(map(int, re.findall(r'\d+', self.version))) def is_installed(self): try: p = subprocess.Popen([self.command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = p.communicate() + self._version = stdout.decode('ascii', 'backslashreplace').strip() rc = p.wait() return (rc == 0) except OSError: @@ -946,14 +960,12 @@ def _run(self, *command): command = [s.encode(locale.getpreferredencoding()) for s in command] print('$', ' '.join(command)) p = subprocess.Popen(command, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) + stderr=subprocess.STDOUT, + env={**os.environ, **self.extra_env}) stdout, stderr = p.communicate() rc = p.wait() if stdout: - print( - stdout if isinstance(stdout, str) else - stdout.decode('ascii', 'backslashreplace') - ) + print(stdout.decode('ascii', 'backslashreplace')) if rc: raise subprocess.CalledProcessError(rc, command[0], output=stdout) @@ -1051,9 +1063,15 @@ def test_get_vcs_files_empty(self): class GitHelper(VCSHelper): command = 'git' + extra_env = dict( + GIT_ALLOW_PROTOCOL='file', + ) def _init_vcs(self): - self._run('git', 'init') + if self.version_tuple >= (2, 28): + self._run('git', 'init', '-b', 'main') + else: + self._run('git', 'init') self._run('git', 'config', 'user.name', 'Unit Test') self._run('git', 'config', 'user.email', 'test@example.com')