mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 04:34:16 +00:00
forbid-new-submodules: fix triggering failure when only a submodule is committed (without any other file); support --from-ref and --to-ref; fixes #609
This commit is contained in:
parent
8c1183c0c8
commit
10c5e4e166
9 changed files with 65 additions and 25 deletions
|
|
@ -1,22 +1,20 @@
|
|||
import os
|
||||
import subprocess
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.forbid_new_submodules import main
|
||||
from testing.util import git_commit
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def git_dir_with_git_dir(tmpdir):
|
||||
with tmpdir.as_cwd():
|
||||
subprocess.check_call(('git', 'init', '.'))
|
||||
subprocess.check_call((
|
||||
'git', 'commit', '-m', 'init', '--allow-empty', '--no-gpg-sign',
|
||||
))
|
||||
git_commit('--allow-empty', '-m', 'init')
|
||||
subprocess.check_call(('git', 'init', 'foo'))
|
||||
subprocess.check_call(
|
||||
('git', 'commit', '-m', 'init', '--allow-empty', '--no-gpg-sign'),
|
||||
cwd=str(tmpdir.join('foo')),
|
||||
)
|
||||
git_commit('--allow-empty', '-m', 'init', cwd=str(tmpdir.join('foo')))
|
||||
yield
|
||||
|
||||
|
||||
|
|
@ -31,7 +29,24 @@ def git_dir_with_git_dir(tmpdir):
|
|||
)
|
||||
def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd):
|
||||
subprocess.check_call(cmd)
|
||||
assert main() == 1
|
||||
assert main(('random_non-related_file',)) == 0
|
||||
assert main(('foo',)) == 1
|
||||
out, _ = capsys.readouterr()
|
||||
assert out.startswith('foo: new submodule introduced\n')
|
||||
|
||||
|
||||
def test_main_new_submodule_committed(git_dir_with_git_dir, capsys):
|
||||
rev_parse_cmd = ('git', 'rev-parse', 'HEAD')
|
||||
from_ref = subprocess.check_output(rev_parse_cmd).decode().strip()
|
||||
subprocess.check_call(('git', 'submodule', 'add', './foo'))
|
||||
git_commit('-m', 'new submodule')
|
||||
to_ref = subprocess.check_output(rev_parse_cmd).decode().strip()
|
||||
with mock.patch.dict(
|
||||
os.environ,
|
||||
{'PRE_COMMIT_FROM_REF': from_ref, 'PRE_COMMIT_TO_REF': to_ref},
|
||||
):
|
||||
assert main(('random_non-related_file',)) == 0
|
||||
assert main(('foo',)) == 1
|
||||
out, _ = capsys.readouterr()
|
||||
assert out.startswith('foo: new submodule introduced\n')
|
||||
|
||||
|
|
@ -39,4 +54,4 @@ def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd):
|
|||
def test_main_no_new_submodule(git_dir_with_git_dir):
|
||||
open('test.py', 'a+').close()
|
||||
subprocess.check_call(('git', 'add', 'test.py'))
|
||||
assert main() == 0
|
||||
assert main(('test.py',)) == 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue