tests: check if 'git init -b' is available

This will be useful for creating new repositories in the test cases
with newer versions of git, which starting with v2.28 are moving away
from a default initial branch.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2021-07-23 15:43:58 -07:00 committed by Marti Bolivar
parent f97f986f30
commit 39e1ffdcf0
1 changed files with 33 additions and 0 deletions

View File

@ -16,6 +16,18 @@ import pytest
GIT = shutil.which('git')
# Git capabilities are discovered at runtime in
# _check_git_capabilities().
# This will be set to True if 'git init --branch' is available.
#
# This feature was available from git release v2.28 and was added in
# commit 32ba12dab2acf1ad11836a627956d1473f6b851a ("init: allow
# specifying the initial branch name for the new repository") as part
# of the git community's choice to avoid a default initial branch
# name.
GIT_INIT_HAS_BRANCH = False
# If you change this, keep the docstring in repos_tmpdir() updated also.
MANIFEST_TEMPLATE = '''\
manifest:
@ -45,6 +57,27 @@ WINDOWS = (platform.system() == 'Windows')
# Test fixtures
#
@pytest.fixture(scope='session', autouse=True)
def _check_git_capabilities(tmpdir_factory):
# Do checks for git behaviors. Right now this is limited to
# deciding whether or not 'git init --branch' is supported.
#
# We aren't using WestCommand._parse_git_version() here just to
# try to keep the conftest behavior independent of the code being
# tested.
global GIT_INIT_HAS_BRANCH
tmpdir = tmpdir_factory.mktemp("west-check-git-caps-tmpdir")
try:
subprocess.run([GIT, 'init', '--initial-branch', 'foo',
os.fspath(tmpdir)],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
check=True)
GIT_INIT_HAS_BRANCH = True
except subprocess.CalledProcessError:
pass
@pytest.fixture(scope='session')
def _session_repos():
'''Just a helper, do not use directly.'''