blob: eaae5e62d7ae239c654d05755d2ae3ac34eb7adb [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Anthony Sottilebaec3082018-06-09 11:16:14 -07003import pytest
4
Evan Felixa8592662017-03-20 10:36:51 -07005from pre_commit_hooks.no_commit_to_branch import is_on_branch
6from pre_commit_hooks.no_commit_to_branch import main
7from pre_commit_hooks.util import cmd_output
Mikhail Khvoinitsky10c5e4e2021-06-23 03:10:13 +03008from testing.util import git_commit
Evan Felixa8592662017-03-20 10:36:51 -07009
10
11def test_other_branch(temp_git_dir):
12 with temp_git_dir.as_cwd():
13 cmd_output('git', 'checkout', '-b', 'anotherbranch')
Anthony Sottile030bfac2019-01-31 19:19:10 -080014 assert is_on_branch({'master'}) is False
Evan Felixa8592662017-03-20 10:36:51 -070015
16
17def test_multi_branch(temp_git_dir):
18 with temp_git_dir.as_cwd():
19 cmd_output('git', 'checkout', '-b', 'another/branch')
Anthony Sottile030bfac2019-01-31 19:19:10 -080020 assert is_on_branch({'master'}) is False
Evan Felixa8592662017-03-20 10:36:51 -070021
22
23def test_multi_branch_fail(temp_git_dir):
24 with temp_git_dir.as_cwd():
25 cmd_output('git', 'checkout', '-b', 'another/branch')
Anthony Sottile030bfac2019-01-31 19:19:10 -080026 assert is_on_branch({'another/branch'}) is True
Evan Felixa8592662017-03-20 10:36:51 -070027
28
29def test_master_branch(temp_git_dir):
30 with temp_git_dir.as_cwd():
Anthony Sottile030bfac2019-01-31 19:19:10 -080031 assert is_on_branch({'master'}) is True
Evan Felixa8592662017-03-20 10:36:51 -070032
33
Evan Felixa8592662017-03-20 10:36:51 -070034def test_main_branch_call(temp_git_dir):
35 with temp_git_dir.as_cwd():
36 cmd_output('git', 'checkout', '-b', 'other')
Anthony Sottile93f319c2018-02-19 12:56:14 -080037 assert main(('--branch', 'other')) == 1
Evan Felixa8592662017-03-20 10:36:51 -070038
39
Anthony Sottilebaec3082018-06-09 11:16:14 -070040@pytest.mark.parametrize('branch_name', ('b1', 'b2'))
41def test_forbid_multiple_branches(temp_git_dir, branch_name):
42 with temp_git_dir.as_cwd():
43 cmd_output('git', 'checkout', '-b', branch_name)
44 assert main(('--branch', 'b1', '--branch', 'b2'))
45
46
Marc Jay8d2785b2019-04-20 13:46:49 +010047def test_branch_pattern_fail(temp_git_dir):
Marc Jayd6847c42019-04-09 23:53:39 +010048 with temp_git_dir.as_cwd():
49 cmd_output('git', 'checkout', '-b', 'another/branch')
Marc Jay8d2785b2019-04-20 13:46:49 +010050 assert is_on_branch(set(), {'another/.*'}) is True
Marc Jayd6847c42019-04-09 23:53:39 +010051
52
53@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
Marc Jay8d2785b2019-04-20 13:46:49 +010054def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
Marc Jayd6847c42019-04-09 23:53:39 +010055 with temp_git_dir.as_cwd():
56 cmd_output('git', 'checkout', '-b', branch_name)
Marc Jay8d2785b2019-04-20 13:46:49 +010057 assert main(('--branch', 'master', '--pattern', 'another/.*'))
Marc Jayd6847c42019-04-09 23:53:39 +010058
59
Evan Felixa8592662017-03-20 10:36:51 -070060def test_main_default_call(temp_git_dir):
61 with temp_git_dir.as_cwd():
62 cmd_output('git', 'checkout', '-b', 'anotherbranch')
Anthony Sottile93f319c2018-02-19 12:56:14 -080063 assert main(()) == 0
64
65
66def test_not_on_a_branch(temp_git_dir):
67 with temp_git_dir.as_cwd():
Mikhail Khvoinitsky10c5e4e2021-06-23 03:10:13 +030068 git_commit('--allow-empty', '-m1')
Anthony Sottile93f319c2018-02-19 12:56:14 -080069 head = cmd_output('git', 'rev-parse', 'HEAD').strip()
70 cmd_output('git', 'checkout', head)
71 # we're not on a branch!
72 assert main(()) == 0
Nicholas Devenish3abbd472021-03-04 13:41:04 +000073
74
75@pytest.mark.parametrize('branch_name', ('master', 'main'))
76def test_default_branch_names(temp_git_dir, branch_name):
77 with temp_git_dir.as_cwd():
78 cmd_output('git', 'checkout', '-b', branch_name)
79 assert main(()) == 1