|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
| 5 | +from pre_commit_hooks.no_commit_to_branch import _default_branch |
5 | 6 | from pre_commit_hooks.no_commit_to_branch import is_on_branch |
6 | 7 | from pre_commit_hooks.no_commit_to_branch import main |
7 | 8 | from pre_commit_hooks.util import cmd_output |
@@ -78,3 +79,35 @@ def test_default_branch_names(temp_git_dir, branch_name): |
78 | 79 | with temp_git_dir.as_cwd(): |
79 | 80 | cmd_output('git', 'checkout', '-b', branch_name) |
80 | 81 | assert main(()) == 1 |
| 82 | + |
| 83 | + |
| 84 | +def test_default_branch_detects_from_origin(tmpdir): |
| 85 | + remote = tmpdir.join('remote') |
| 86 | + cmd_output('git', 'init', '--', str(remote)) |
| 87 | + with remote.as_cwd(): |
| 88 | + cmd_output('git', 'checkout', '-b', 'develop') |
| 89 | + git_commit('--allow-empty', '-m', 'init') |
| 90 | + |
| 91 | + local = tmpdir.join('local') |
| 92 | + cmd_output('git', 'clone', str(remote), str(local)) |
| 93 | + |
| 94 | + with local.as_cwd(): |
| 95 | + assert _default_branch() == frozenset({'develop'}) |
| 96 | + |
| 97 | + |
| 98 | +def test_main_blocks_detected_default_branch(tmpdir): |
| 99 | + remote = tmpdir.join('remote') |
| 100 | + cmd_output('git', 'init', '--', str(remote)) |
| 101 | + with remote.as_cwd(): |
| 102 | + cmd_output('git', 'checkout', '-b', 'develop') |
| 103 | + git_commit('--allow-empty', '-m', 'init') |
| 104 | + |
| 105 | + local = tmpdir.join('local') |
| 106 | + cmd_output('git', 'clone', str(remote), str(local)) |
| 107 | + |
| 108 | + with local.as_cwd(): |
| 109 | + # On detected default branch — should be blocked |
| 110 | + assert main(()) == 1 |
| 111 | + # On a feature branch — should pass |
| 112 | + cmd_output('git', 'checkout', '-b', 'feature') |
| 113 | + assert main(()) == 0 |
0 commit comments