|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pre_commit.commands.hazmat import _cmd_filenames |
| 8 | +from pre_commit.commands.hazmat import main |
| 9 | +from testing.util import cwd |
| 10 | + |
| 11 | + |
| 12 | +def test_cmd_filenames_no_dash_dash(): |
| 13 | + with pytest.raises(SystemExit) as excinfo: |
| 14 | + _cmd_filenames(('no', 'dashdash', 'here')) |
| 15 | + msg, = excinfo.value.args |
| 16 | + assert msg == 'hazmat entry must end with `--`' |
| 17 | + |
| 18 | + |
| 19 | +def test_cmd_filenames_no_filenames(): |
| 20 | + cmd, filenames = _cmd_filenames(('hello', 'world', '--')) |
| 21 | + assert cmd == ('hello', 'world') |
| 22 | + assert filenames == () |
| 23 | + |
| 24 | + |
| 25 | +def test_cmd_filenames_some_filenames(): |
| 26 | + cmd, filenames = _cmd_filenames(('hello', 'world', '--', 'f1', 'f2')) |
| 27 | + assert cmd == ('hello', 'world') |
| 28 | + assert filenames == ('f1', 'f2') |
| 29 | + |
| 30 | + |
| 31 | +def test_cmd_filenames_multiple_dashdash(): |
| 32 | + cmd, filenames = _cmd_filenames(('hello', '--', 'arg', '--', 'f1', 'f2')) |
| 33 | + assert cmd == ('hello', '--', 'arg') |
| 34 | + assert filenames == ('f1', 'f2') |
| 35 | + |
| 36 | + |
| 37 | +def test_cd_unexpected_filename(): |
| 38 | + with pytest.raises(SystemExit) as excinfo: |
| 39 | + main(('cd', 'subdir', 'cmd', '--', 'subdir/1', 'not-subdir/2')) |
| 40 | + msg, = excinfo.value.args |
| 41 | + assert msg == "unexpected file without prefix='subdir/': not-subdir/2" |
| 42 | + |
| 43 | + |
| 44 | +def _norm(out): |
| 45 | + return out.replace('\r\n', '\n') |
| 46 | + |
| 47 | + |
| 48 | +def test_cd(tmp_path, capfd): |
| 49 | + subdir = tmp_path.joinpath('subdir') |
| 50 | + subdir.mkdir() |
| 51 | + subdir.joinpath('a').write_text('a') |
| 52 | + subdir.joinpath('b').write_text('b') |
| 53 | + |
| 54 | + with cwd(tmp_path): |
| 55 | + ret = main(( |
| 56 | + 'cd', 'subdir', |
| 57 | + sys.executable, '-c', |
| 58 | + 'import os; print(os.getcwd());' |
| 59 | + 'import sys; [print(open(f).read()) for f in sys.argv[1:]]', |
| 60 | + '--', |
| 61 | + 'subdir/a', 'subdir/b', |
| 62 | + )) |
| 63 | + |
| 64 | + assert ret == 0 |
| 65 | + out, err = capfd.readouterr() |
| 66 | + assert _norm(out) == f'{subdir}\na\nb\n' |
| 67 | + assert err == '' |
| 68 | + |
| 69 | + |
| 70 | +def test_ignore_exit_code(capfd): |
| 71 | + ret = main(( |
| 72 | + 'ignore-exit-code', sys.executable, '-c', 'raise SystemExit("bye")', |
| 73 | + )) |
| 74 | + assert ret == 0 |
| 75 | + out, err = capfd.readouterr() |
| 76 | + assert out == '' |
| 77 | + assert _norm(err) == 'bye\n' |
| 78 | + |
| 79 | + |
| 80 | +def test_n1(capfd): |
| 81 | + ret = main(( |
| 82 | + 'n1', sys.executable, '-c', 'import sys; print(sys.argv[1:])', |
| 83 | + '--', |
| 84 | + 'foo', 'bar', 'baz', |
| 85 | + )) |
| 86 | + assert ret == 0 |
| 87 | + out, err = capfd.readouterr() |
| 88 | + assert _norm(out) == "['foo']\n['bar']\n['baz']\n" |
| 89 | + assert err == '' |
| 90 | + |
| 91 | + |
| 92 | +def test_n1_some_error_code(): |
| 93 | + ret = main(( |
| 94 | + 'n1', sys.executable, '-c', |
| 95 | + 'import sys; raise SystemExit(sys.argv[1] == "error")', |
| 96 | + '--', |
| 97 | + 'ok', 'error', 'ok', |
| 98 | + )) |
| 99 | + assert ret == 1 |
0 commit comments