forked from pre-commit/pre-commit-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_commit_to_branch_test.py
More file actions
123 lines (88 loc) · 3.94 KB
/
no_commit_to_branch_test.py
File metadata and controls
123 lines (88 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from __future__ import annotations
from unittest.mock import patch
import pytest
from pre_commit_hooks.no_commit_to_branch import _default_branch
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 git_commit
def test_other_branch(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'anotherbranch')
assert is_on_branch({'placeholder'}) 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({'placeholder'}) is False
def test_multi_branch_fail(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'another/branch')
assert is_on_branch({'another/branch'}) is True
def test_exact_branch(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'branchname')
assert is_on_branch({'branchname'}) is True
def test_main_branch_call(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'other')
assert main(('--branch', 'other')) == 1
@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'))
def test_branch_pattern_fail(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'another/branch')
assert is_on_branch(set(), {'another/.*'}) is True
@pytest.mark.parametrize('branch_name', ('somebranch', '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', 'somebranch', '--pattern', 'another/.*'))
def test_main_default_call(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'anotherbranch')
assert main(()) == 0
def test_not_on_a_branch(temp_git_dir):
with temp_git_dir.as_cwd():
git_commit('--allow-empty', '-m1')
head = cmd_output('git', 'rev-parse', 'HEAD').strip()
cmd_output('git', 'checkout', head)
# we're not on a branch!
assert main(()) == 0
@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
def test_default_branch_falls_back_when_empty_branch(tmpdir):
with patch(
'pre_commit_hooks.no_commit_to_branch.cmd_output',
return_value='origin/',
):
assert _default_branch() == frozenset({'master', 'main'})
def test_default_branch_detects_from_origin(tmpdir):
remote = tmpdir.join('remote')
cmd_output('git', 'init', '--', str(remote))
with remote.as_cwd():
cmd_output('git', 'checkout', '-b', 'develop')
git_commit('--allow-empty', '-m', 'init')
local = tmpdir.join('local')
cmd_output('git', 'clone', str(remote), str(local))
with local.as_cwd():
assert _default_branch() == frozenset({'develop'})
def test_main_blocks_detected_default_branch(tmpdir):
remote = tmpdir.join('remote')
cmd_output('git', 'init', '--', str(remote))
with remote.as_cwd():
cmd_output('git', 'checkout', '-b', 'develop')
git_commit('--allow-empty', '-m', 'init')
local = tmpdir.join('local')
cmd_output('git', 'clone', str(remote), str(local))
with local.as_cwd():
# On detected default branch — should be blocked
assert main(()) == 1
# On a feature branch — should pass
cmd_output('git', 'checkout', '-b', 'feature')
assert main(()) == 0