From 6342d5691478873708ee9363bd7dc8e275a75098 Mon Sep 17 00:00:00 2001 From: Fredrik de Groot Date: Tue, 1 Dec 2020 15:58:53 +0100 Subject: [PATCH] Fix tests after "use new main branch" Tests worked fine if init.defaultBranch main was used, but failed due to git branch reasons if master was still used. Since we can only use init.defaultBranch if git version >= 2.28, I also went with a template dir HEAD main tweak if lower so tests now pass regardless of client git default branch and version. Test: Ran tests with ~/.gitconfig:init.defaultBranch=master Test: Ran tests with ~/.gitconfig:init.defaultBranch=main Test: Ran tests for both code branches of git require Change-Id: I49fa1e4ae45b8aec16a093132ee9fa466cbc11ec Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/290404 Tested-by: Fredrik de Groot Reviewed-by: Mike Frysinger --- tests/test_project.py | 15 ++++++++++++++- tests/test_wrapper.py | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/test_project.py b/tests/test_project.py index 924a2459..02285e2f 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -26,6 +26,7 @@ import tempfile import unittest import error +import git_command import git_config import platform_utils import project @@ -38,7 +39,19 @@ def TempGitTree(): # Python 2 support entirely. try: tempdir = tempfile.mkdtemp(prefix='repo-tests') - subprocess.check_call(['git', 'init'], cwd=tempdir) + + # Tests need to assume, that main is default branch at init, + # which is not supported in config until 2.28. + cmd = ['git', 'init'] + if git_command.git_require((2, 28, 0)): + cmd += ['--initial-branch=main'] + else: + # Use template dir for init. + templatedir = tempfile.mkdtemp(prefix='.test-template') + with open(os.path.join(templatedir, 'HEAD'), 'w') as fp: + fp.write('ref: refs/heads/main\n') + cmd += ['--template=', templatedir] + subprocess.check_call(cmd, cwd=tempdir) yield tempdir finally: platform_utils.rmtree(tempdir) diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py index d22dc4ee..069a5c3b 100644 --- a/tests/test_wrapper.py +++ b/tests/test_wrapper.py @@ -25,6 +25,7 @@ import shutil import tempfile import unittest +import git_command import platform_utils from pyversion import is_python3 import wrapper @@ -357,7 +358,19 @@ class GitCheckoutTestCase(RepoWrapperTestCase): remote = os.path.join(cls.GIT_DIR, 'remote') os.mkdir(remote) - run_git('init', cwd=remote) + + # Tests need to assume, that main is default branch at init, + # which is not supported in config until 2.28. + if git_command.git_require((2, 28, 0)): + initstr = '--initial-branch=main' + else: + # Use template dir for init. + templatedir = tempfile.mkdtemp(prefix='.test-template') + with open(os.path.join(templatedir, 'HEAD'), 'w') as fp: + fp.write('ref: refs/heads/main\n') + initstr = '--template=' + templatedir + + run_git('init', initstr, cwd=remote) run_git('commit', '--allow-empty', '-minit', cwd=remote) run_git('branch', 'stable', cwd=remote) run_git('tag', 'v1.0', cwd=remote)