2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-06-10 02:16:14 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-03-21 01:36:51 +08:00
|
|
|
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
|
2021-06-23 08:10:13 +08:00
|
|
|
from testing.util import git_commit
|
2017-03-21 01:36:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_other_branch(temp_git_dir):
|
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
2019-02-01 11:19:10 +08:00
|
|
|
assert is_on_branch({'master'}) is False
|
2017-03-21 01:36:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_branch(temp_git_dir):
|
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', 'another/branch')
|
2019-02-01 11:19:10 +08:00
|
|
|
assert is_on_branch({'master'}) is False
|
2017-03-21 01:36:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_branch_fail(temp_git_dir):
|
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', 'another/branch')
|
2019-02-01 11:19:10 +08:00
|
|
|
assert is_on_branch({'another/branch'}) is True
|
2017-03-21 01:36:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_master_branch(temp_git_dir):
|
|
|
|
with temp_git_dir.as_cwd():
|
2019-02-01 11:19:10 +08:00
|
|
|
assert is_on_branch({'master'}) is True
|
2017-03-21 01:36:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_main_branch_call(temp_git_dir):
|
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', 'other')
|
2018-02-20 04:56:14 +08:00
|
|
|
assert main(('--branch', 'other')) == 1
|
2017-03-21 01:36:51 +08:00
|
|
|
|
|
|
|
|
2018-06-10 02:16:14 +08:00
|
|
|
@pytest.mark.parametrize('branch_name', ('b1', 'b2'))
|
|
|
|
def test_forbid_multiple_branches(temp_git_dir, branch_name):
|
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', branch_name)
|
|
|
|
assert main(('--branch', 'b1', '--branch', 'b2'))
|
|
|
|
|
|
|
|
|
2019-04-20 20:46:49 +08:00
|
|
|
def test_branch_pattern_fail(temp_git_dir):
|
2019-04-10 06:53:39 +08:00
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', 'another/branch')
|
2019-04-20 20:46:49 +08:00
|
|
|
assert is_on_branch(set(), {'another/.*'}) is True
|
2019-04-10 06:53:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
|
2019-04-20 20:46:49 +08:00
|
|
|
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
|
2019-04-10 06:53:39 +08:00
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', branch_name)
|
2019-04-20 20:46:49 +08:00
|
|
|
assert main(('--branch', 'master', '--pattern', 'another/.*'))
|
2019-04-10 06:53:39 +08:00
|
|
|
|
|
|
|
|
2017-03-21 01:36:51 +08:00
|
|
|
def test_main_default_call(temp_git_dir):
|
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
2018-02-20 04:56:14 +08:00
|
|
|
assert main(()) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_on_a_branch(temp_git_dir):
|
|
|
|
with temp_git_dir.as_cwd():
|
2021-06-23 08:10:13 +08:00
|
|
|
git_commit('--allow-empty', '-m1')
|
2018-02-20 04:56:14 +08:00
|
|
|
head = cmd_output('git', 'rev-parse', 'HEAD').strip()
|
|
|
|
cmd_output('git', 'checkout', head)
|
|
|
|
# we're not on a branch!
|
|
|
|
assert main(()) == 0
|
2021-03-04 21:41:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('branch_name', ('master', 'main'))
|
|
|
|
def test_default_branch_names(temp_git_dir, branch_name):
|
|
|
|
with temp_git_dir.as_cwd():
|
|
|
|
cmd_output('git', 'checkout', '-b', branch_name)
|
|
|
|
assert main(()) == 1
|