fix tests to handle default branch main or master

This commit is contained in:
Ryan Downing 2025-03-27 22:45:38 -04:00
parent 31903eabdb
commit c96572c378
3 changed files with 24 additions and 6 deletions

View file

@ -14,3 +14,8 @@ def get_resource_path(path):
def git_commit(*args, **kwargs):
cmd = ('git', 'commit', '--no-gpg-sign', '--no-verify', '--no-edit', *args)
subprocess.check_call(cmd, **kwargs)
def get_default_branch():
cmd = ('git', 'config', '--get', 'init.defaultBranch')
return subprocess.getoutput(cmd).strip() or 'master'

View file

@ -7,10 +7,14 @@ import pytest
from pre_commit_hooks.check_merge_conflict import main
from pre_commit_hooks.util import cmd_output
from testing.util import get_default_branch
from testing.util import get_resource_path
from testing.util import git_commit
default_branch = get_default_branch()
@pytest.fixture
def f1_is_a_conflict_file(tmpdir):
# Make a merge conflict
@ -150,7 +154,12 @@ def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir, capsys):
cmd_output('git', 'worktree', 'add', str(worktree))
with worktree.as_cwd():
cmd_output(
'git', 'pull', '--no-rebase', 'origin', 'master', retcode=None,
'git',
'pull',
'--no-rebase',
'origin',
default_branch,
retcode=None,
)
msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG')
assert msg.exists()

View file

@ -5,19 +5,23 @@ import pytest
from pre_commit_hooks.no_commit_to_branch import is_on_branch
from pre_commit_hooks.no_commit_to_branch import main
from pre_commit_hooks.util import cmd_output
from testing.util import get_default_branch
from testing.util import git_commit
default_branch = get_default_branch()
def test_other_branch(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'anotherbranch')
assert is_on_branch({'master'}) is False
assert is_on_branch({default_branch}) is False
def test_multi_branch(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'another/branch')
assert is_on_branch({'master'}) is False
assert is_on_branch({default_branch}) is False
def test_multi_branch_fail(temp_git_dir):
@ -28,7 +32,7 @@ def test_multi_branch_fail(temp_git_dir):
def test_master_branch(temp_git_dir):
with temp_git_dir.as_cwd():
assert is_on_branch({'master'}) is True
assert is_on_branch({default_branch}) is True
def test_main_branch_call(temp_git_dir):
@ -50,11 +54,11 @@ def test_branch_pattern_fail(temp_git_dir):
assert is_on_branch(set(), {'another/.*'}) is True
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
@pytest.mark.parametrize('branch_name', (default_branch, 'another/branch'))
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', branch_name)
assert main(('--branch', 'master', '--pattern', 'another/.*'))
assert main(('--branch', default_branch, '--pattern', 'another/.*'))
def test_main_default_call(temp_git_dir):