Skip to content

Commit c96572c

Browse files
committed
fix tests to handle default branch main or master
1 parent 31903ea commit c96572c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

testing/util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ def get_resource_path(path):
1414
def git_commit(*args, **kwargs):
1515
cmd = ('git', 'commit', '--no-gpg-sign', '--no-verify', '--no-edit', *args)
1616
subprocess.check_call(cmd, **kwargs)
17+
18+
19+
def get_default_branch():
20+
cmd = ('git', 'config', '--get', 'init.defaultBranch')
21+
return subprocess.getoutput(cmd).strip() or 'master'

tests/check_merge_conflict_test.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

88
from pre_commit_hooks.check_merge_conflict import main
99
from pre_commit_hooks.util import cmd_output
10+
from testing.util import get_default_branch
1011
from testing.util import get_resource_path
1112
from testing.util import git_commit
1213

1314

15+
default_branch = get_default_branch()
16+
17+
1418
@pytest.fixture
1519
def f1_is_a_conflict_file(tmpdir):
1620
# Make a merge conflict
@@ -150,7 +154,12 @@ def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir, capsys):
150154
cmd_output('git', 'worktree', 'add', str(worktree))
151155
with worktree.as_cwd():
152156
cmd_output(
153-
'git', 'pull', '--no-rebase', 'origin', 'master', retcode=None,
157+
'git',
158+
'pull',
159+
'--no-rebase',
160+
'origin',
161+
default_branch,
162+
retcode=None,
154163
)
155164
msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG')
156165
assert msg.exists()

tests/no_commit_to_branch_test.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@
55
from pre_commit_hooks.no_commit_to_branch import is_on_branch
66
from pre_commit_hooks.no_commit_to_branch import main
77
from pre_commit_hooks.util import cmd_output
8+
from testing.util import get_default_branch
89
from testing.util import git_commit
910

1011

12+
default_branch = get_default_branch()
13+
14+
1115
def test_other_branch(temp_git_dir):
1216
with temp_git_dir.as_cwd():
1317
cmd_output('git', 'checkout', '-b', 'anotherbranch')
14-
assert is_on_branch({'master'}) is False
18+
assert is_on_branch({default_branch}) is False
1519

1620

1721
def test_multi_branch(temp_git_dir):
1822
with temp_git_dir.as_cwd():
1923
cmd_output('git', 'checkout', '-b', 'another/branch')
20-
assert is_on_branch({'master'}) is False
24+
assert is_on_branch({default_branch}) is False
2125

2226

2327
def test_multi_branch_fail(temp_git_dir):
@@ -28,7 +32,7 @@ def test_multi_branch_fail(temp_git_dir):
2832

2933
def test_master_branch(temp_git_dir):
3034
with temp_git_dir.as_cwd():
31-
assert is_on_branch({'master'}) is True
35+
assert is_on_branch({default_branch}) is True
3236

3337

3438
def test_main_branch_call(temp_git_dir):
@@ -50,11 +54,11 @@ def test_branch_pattern_fail(temp_git_dir):
5054
assert is_on_branch(set(), {'another/.*'}) is True
5155

5256

53-
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
57+
@pytest.mark.parametrize('branch_name', (default_branch, 'another/branch'))
5458
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
5559
with temp_git_dir.as_cwd():
5660
cmd_output('git', 'checkout', '-b', branch_name)
57-
assert main(('--branch', 'master', '--pattern', 'another/.*'))
61+
assert main(('--branch', default_branch, '--pattern', 'another/.*'))
5862

5963

6064
def test_main_default_call(temp_git_dir):

0 commit comments

Comments
 (0)