diff --git a/testing/util.py b/testing/util.py index 2bbbe644..75a81b40 100644 --- a/testing/util.py +++ b/testing/util.py @@ -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(): + ret = subprocess.getoutput('git config init.defaultBranch').strip() + return ret or 'master' diff --git a/tests/check_merge_conflict_test.py b/tests/check_merge_conflict_test.py index 76c4283c..dabe3db4 100644 --- a/tests/check_merge_conflict_test.py +++ b/tests/check_merge_conflict_test.py @@ -7,10 +7,14 @@ 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() diff --git a/tests/no_commit_to_branch_test.py b/tests/no_commit_to_branch_test.py index eaae5e62..4c8207ec 100644 --- a/tests/no_commit_to_branch_test.py +++ b/tests/no_commit_to_branch_test.py @@ -5,19 +5,23 @@ 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):