2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-06-23 08:10:13 +08:00
|
|
|
import os
|
2018-10-13 10:37:46 +08:00
|
|
|
import subprocess
|
2021-06-23 08:10:13 +08:00
|
|
|
from unittest import mock
|
2018-10-13 10:37:46 +08:00
|
|
|
|
2016-12-01 01:56:42 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pre_commit_hooks.forbid_new_submodules import main
|
2021-06-23 08:10:13 +08:00
|
|
|
from testing.util import git_commit
|
2016-12-01 01:56:42 +08:00
|
|
|
|
|
|
|
|
2018-01-22 07:31:23 +08:00
|
|
|
@pytest.fixture
|
2016-12-01 01:56:42 +08:00
|
|
|
def git_dir_with_git_dir(tmpdir):
|
|
|
|
with tmpdir.as_cwd():
|
2018-10-13 10:37:46 +08:00
|
|
|
subprocess.check_call(('git', 'init', '.'))
|
2021-06-23 08:10:13 +08:00
|
|
|
git_commit('--allow-empty', '-m', 'init')
|
2018-10-13 10:37:46 +08:00
|
|
|
subprocess.check_call(('git', 'init', 'foo'))
|
2021-06-23 08:10:13 +08:00
|
|
|
git_commit('--allow-empty', '-m', 'init', cwd=str(tmpdir.join('foo')))
|
2016-12-01 01:56:42 +08:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'cmd',
|
|
|
|
(
|
|
|
|
# Actually add the submodule
|
|
|
|
('git', 'submodule', 'add', './foo'),
|
|
|
|
# Sneaky submodule add (that doesn't show up in .gitmodules)
|
|
|
|
('git', 'add', 'foo'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd):
|
2018-10-13 10:37:46 +08:00
|
|
|
subprocess.check_call(cmd)
|
2021-06-23 08:10:13 +08:00
|
|
|
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
|
2016-12-01 01:56:42 +08:00
|
|
|
out, _ = capsys.readouterr()
|
|
|
|
assert out.startswith('foo: new submodule introduced\n')
|
|
|
|
|
|
|
|
|
|
|
|
def test_main_no_new_submodule(git_dir_with_git_dir):
|
|
|
|
open('test.py', 'a+').close()
|
2018-10-13 10:37:46 +08:00
|
|
|
subprocess.check_call(('git', 'add', 'test.py'))
|
2021-06-23 08:10:13 +08:00
|
|
|
assert main(('test.py',)) == 0
|