Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Anthony Sottile | baec308 | 2018-06-09 11:16:14 -0700 | [diff] [blame] | 3 | import pytest |
| 4 | |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 5 | from pre_commit_hooks.no_commit_to_branch import is_on_branch |
| 6 | from pre_commit_hooks.no_commit_to_branch import main |
| 7 | from pre_commit_hooks.util import cmd_output |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 8 | from testing.util import git_commit |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def test_other_branch(temp_git_dir): |
| 12 | with temp_git_dir.as_cwd(): |
| 13 | cmd_output('git', 'checkout', '-b', 'anotherbranch') |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 14 | assert is_on_branch({'master'}) is False |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | def test_multi_branch(temp_git_dir): |
| 18 | with temp_git_dir.as_cwd(): |
| 19 | cmd_output('git', 'checkout', '-b', 'another/branch') |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 20 | assert is_on_branch({'master'}) is False |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def test_multi_branch_fail(temp_git_dir): |
| 24 | with temp_git_dir.as_cwd(): |
| 25 | cmd_output('git', 'checkout', '-b', 'another/branch') |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 26 | assert is_on_branch({'another/branch'}) is True |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def test_master_branch(temp_git_dir): |
| 30 | with temp_git_dir.as_cwd(): |
Anthony Sottile | 030bfac | 2019-01-31 19:19:10 -0800 | [diff] [blame] | 31 | assert is_on_branch({'master'}) is True |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 32 | |
| 33 | |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 34 | def test_main_branch_call(temp_git_dir): |
| 35 | with temp_git_dir.as_cwd(): |
| 36 | cmd_output('git', 'checkout', '-b', 'other') |
Anthony Sottile | 93f319c | 2018-02-19 12:56:14 -0800 | [diff] [blame] | 37 | assert main(('--branch', 'other')) == 1 |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 38 | |
| 39 | |
Anthony Sottile | baec308 | 2018-06-09 11:16:14 -0700 | [diff] [blame] | 40 | @pytest.mark.parametrize('branch_name', ('b1', 'b2')) |
| 41 | def 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 Jay | 8d2785b | 2019-04-20 13:46:49 +0100 | [diff] [blame] | 47 | def test_branch_pattern_fail(temp_git_dir): |
Marc Jay | d6847c4 | 2019-04-09 23:53:39 +0100 | [diff] [blame] | 48 | with temp_git_dir.as_cwd(): |
| 49 | cmd_output('git', 'checkout', '-b', 'another/branch') |
Marc Jay | 8d2785b | 2019-04-20 13:46:49 +0100 | [diff] [blame] | 50 | assert is_on_branch(set(), {'another/.*'}) is True |
Marc Jay | d6847c4 | 2019-04-09 23:53:39 +0100 | [diff] [blame] | 51 | |
| 52 | |
| 53 | @pytest.mark.parametrize('branch_name', ('master', 'another/branch')) |
Marc Jay | 8d2785b | 2019-04-20 13:46:49 +0100 | [diff] [blame] | 54 | def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name): |
Marc Jay | d6847c4 | 2019-04-09 23:53:39 +0100 | [diff] [blame] | 55 | with temp_git_dir.as_cwd(): |
| 56 | cmd_output('git', 'checkout', '-b', branch_name) |
Marc Jay | 8d2785b | 2019-04-20 13:46:49 +0100 | [diff] [blame] | 57 | assert main(('--branch', 'master', '--pattern', 'another/.*')) |
Marc Jay | d6847c4 | 2019-04-09 23:53:39 +0100 | [diff] [blame] | 58 | |
| 59 | |
Evan Felix | a859266 | 2017-03-20 10:36:51 -0700 | [diff] [blame] | 60 | def test_main_default_call(temp_git_dir): |
| 61 | with temp_git_dir.as_cwd(): |
| 62 | cmd_output('git', 'checkout', '-b', 'anotherbranch') |
Anthony Sottile | 93f319c | 2018-02-19 12:56:14 -0800 | [diff] [blame] | 63 | assert main(()) == 0 |
| 64 | |
| 65 | |
| 66 | def test_not_on_a_branch(temp_git_dir): |
| 67 | with temp_git_dir.as_cwd(): |
Mikhail Khvoinitsky | 10c5e4e | 2021-06-23 03:10:13 +0300 | [diff] [blame] | 68 | git_commit('--allow-empty', '-m1') |
Anthony Sottile | 93f319c | 2018-02-19 12:56:14 -0800 | [diff] [blame] | 69 | 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 Devenish | 3abbd47 | 2021-03-04 13:41:04 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | @pytest.mark.parametrize('branch_name', ('master', 'main')) |
| 76 | def 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 |