Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subprocess.getoutput should really never be used -- it invokes a shell

return ret or 'master'
11 changes: 10 additions & 1 deletion tests/check_merge_conflict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 9 additions & 5 deletions tests/no_commit_to_branch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down