mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 00:04:42 +04:00
add pre-commit hazmat
This commit is contained in:
parent
9c7ea88ab9
commit
bdf68790b7
7 changed files with 243 additions and 2 deletions
99
tests/commands/hazmat_test.py
Normal file
99
tests/commands/hazmat_test.py
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit.commands.hazmat import _cmd_filenames
|
||||
from pre_commit.commands.hazmat import main
|
||||
from testing.util import cwd
|
||||
|
||||
|
||||
def test_cmd_filenames_no_dash_dash():
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
_cmd_filenames(('no', 'dashdash', 'here'))
|
||||
msg, = excinfo.value.args
|
||||
assert msg == 'hazmat entry must end with `--`'
|
||||
|
||||
|
||||
def test_cmd_filenames_no_filenames():
|
||||
cmd, filenames = _cmd_filenames(('hello', 'world', '--'))
|
||||
assert cmd == ('hello', 'world')
|
||||
assert filenames == ()
|
||||
|
||||
|
||||
def test_cmd_filenames_some_filenames():
|
||||
cmd, filenames = _cmd_filenames(('hello', 'world', '--', 'f1', 'f2'))
|
||||
assert cmd == ('hello', 'world')
|
||||
assert filenames == ('f1', 'f2')
|
||||
|
||||
|
||||
def test_cmd_filenames_multiple_dashdash():
|
||||
cmd, filenames = _cmd_filenames(('hello', '--', 'arg', '--', 'f1', 'f2'))
|
||||
assert cmd == ('hello', '--', 'arg')
|
||||
assert filenames == ('f1', 'f2')
|
||||
|
||||
|
||||
def test_cd_unexpected_filename():
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
main(('cd', 'subdir', 'cmd', '--', 'subdir/1', 'not-subdir/2'))
|
||||
msg, = excinfo.value.args
|
||||
assert msg == "unexpected file without prefix='subdir/': not-subdir/2"
|
||||
|
||||
|
||||
def _norm(out):
|
||||
return out.replace('\r\n', '\n')
|
||||
|
||||
|
||||
def test_cd(tmp_path, capfd):
|
||||
subdir = tmp_path.joinpath('subdir')
|
||||
subdir.mkdir()
|
||||
subdir.joinpath('a').write_text('a')
|
||||
subdir.joinpath('b').write_text('b')
|
||||
|
||||
with cwd(tmp_path):
|
||||
ret = main((
|
||||
'cd', 'subdir',
|
||||
sys.executable, '-c',
|
||||
'import os; print(os.getcwd());'
|
||||
'import sys; [print(open(f).read()) for f in sys.argv[1:]]',
|
||||
'--',
|
||||
'subdir/a', 'subdir/b',
|
||||
))
|
||||
|
||||
assert ret == 0
|
||||
out, err = capfd.readouterr()
|
||||
assert _norm(out) == f'{subdir}\na\nb\n'
|
||||
assert err == ''
|
||||
|
||||
|
||||
def test_ignore_exit_code(capfd):
|
||||
ret = main((
|
||||
'ignore-exit-code', sys.executable, '-c', 'raise SystemExit("bye")',
|
||||
))
|
||||
assert ret == 0
|
||||
out, err = capfd.readouterr()
|
||||
assert out == ''
|
||||
assert _norm(err) == 'bye\n'
|
||||
|
||||
|
||||
def test_n1(capfd):
|
||||
ret = main((
|
||||
'n1', sys.executable, '-c', 'import sys; print(sys.argv[1:])',
|
||||
'--',
|
||||
'foo', 'bar', 'baz',
|
||||
))
|
||||
assert ret == 0
|
||||
out, err = capfd.readouterr()
|
||||
assert _norm(out) == "['foo']\n['bar']\n['baz']\n"
|
||||
assert err == ''
|
||||
|
||||
|
||||
def test_n1_some_error_code():
|
||||
ret = main((
|
||||
'n1', sys.executable, '-c',
|
||||
'import sys; raise SystemExit(sys.argv[1] == "error")',
|
||||
'--',
|
||||
'ok', 'error', 'ok',
|
||||
))
|
||||
assert ret == 1
|
||||
|
|
@ -164,3 +164,15 @@ def test_basic_run_hook(tmp_path):
|
|||
assert ret == 0
|
||||
out = out.replace(b'\r\n', b'\n')
|
||||
assert out == b'hi hello file file file\n'
|
||||
|
||||
|
||||
def test_hook_cmd():
|
||||
assert lang_base.hook_cmd('echo hi', ()) == ('echo', 'hi')
|
||||
|
||||
|
||||
def test_hook_cmd_hazmat():
|
||||
ret = lang_base.hook_cmd('pre-commit hazmat cd a echo -- b', ())
|
||||
assert ret == (
|
||||
sys.executable, '-m', 'pre_commit.commands.hazmat',
|
||||
'cd', 'a', 'echo', '--', 'b',
|
||||
)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import pytest
|
|||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import main
|
||||
from pre_commit.commands import hazmat
|
||||
from pre_commit.errors import FatalError
|
||||
from pre_commit.util import cmd_output
|
||||
from testing.auto_namedtuple import auto_namedtuple
|
||||
|
|
@ -158,6 +159,17 @@ def test_all_cmds(command, mock_commands, mock_store_dir):
|
|||
assert_only_one_mock_called(mock_commands)
|
||||
|
||||
|
||||
def test_hazmat(mock_store_dir):
|
||||
with mock.patch.object(hazmat, 'impl') as mck:
|
||||
main.main(('hazmat', 'cd', 'subdir', '--', 'cmd', '--', 'f1', 'f2'))
|
||||
assert mck.call_count == 1
|
||||
(arg,), dct = mck.call_args
|
||||
assert dct == {}
|
||||
assert arg.tool == 'cd'
|
||||
assert arg.subdir == 'subdir'
|
||||
assert arg.cmd == ['cmd', '--', 'f1', 'f2']
|
||||
|
||||
|
||||
def test_try_repo(mock_store_dir):
|
||||
with mock.patch.object(main, 'try_repo') as patch:
|
||||
main.main(('try-repo', '.'))
|
||||
|
|
|
|||
|
|
@ -506,3 +506,14 @@ def test_args_with_spaces_and_quotes(tmp_path):
|
|||
|
||||
expected = b"['i have spaces', 'and\"\\'quotes', '$and !this']\n"
|
||||
assert ret == (0, expected)
|
||||
|
||||
|
||||
def test_hazmat(tmp_path):
|
||||
ret = run_language(
|
||||
tmp_path, unsupported,
|
||||
f'pre-commit hazmat ignore-exit-code {shlex.quote(sys.executable)} '
|
||||
f"-c 'import sys; raise SystemExit(sys.argv[1:])'",
|
||||
('f1', 'f2'),
|
||||
)
|
||||
expected = b"['f1', 'f2']\n"
|
||||
assert ret == (0, expected)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue