2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-06-23 15:33:13 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pre_commit_hooks.file_contents_sorter import FAIL
|
|
|
|
from pre_commit_hooks.file_contents_sorter import main
|
|
|
|
from pre_commit_hooks.file_contents_sorter import PASS
|
|
|
|
|
|
|
|
|
2017-06-26 00:48:16 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-08-27 08:28:26 +08:00
|
|
|
('input_s', 'argv', 'expected_retval', 'output'),
|
2017-06-26 00:48:16 +08:00
|
|
|
(
|
2023-08-06 04:56:46 +08:00
|
|
|
(b'', [], PASS, b''),
|
|
|
|
(b'\n', [], FAIL, b''),
|
|
|
|
(b'\n\n', [], FAIL, b''),
|
2020-08-27 08:28:26 +08:00
|
|
|
(b'lonesome\n', [], PASS, b'lonesome\n'),
|
|
|
|
(b'missing_newline', [], FAIL, b'missing_newline\n'),
|
|
|
|
(b'newline\nmissing', [], FAIL, b'missing\nnewline\n'),
|
|
|
|
(b'missing\nnewline', [], FAIL, b'missing\nnewline\n'),
|
|
|
|
(b'alpha\nbeta\n', [], PASS, b'alpha\nbeta\n'),
|
|
|
|
(b'beta\nalpha\n', [], FAIL, b'alpha\nbeta\n'),
|
|
|
|
(b'C\nc\n', [], PASS, b'C\nc\n'),
|
|
|
|
(b'c\nC\n', [], FAIL, b'C\nc\n'),
|
|
|
|
(b'mag ical \n tre vor\n', [], FAIL, b' tre vor\nmag ical \n'),
|
|
|
|
(b'@\n-\n_\n#\n', [], FAIL, b'#\n-\n@\n_\n'),
|
|
|
|
(b'extra\n\n\nwhitespace\n', [], FAIL, b'extra\nwhitespace\n'),
|
|
|
|
(b'whitespace\n\n\nextra\n', [], FAIL, b'extra\nwhitespace\n'),
|
|
|
|
(
|
|
|
|
b'fee\nFie\nFoe\nfum\n',
|
|
|
|
[],
|
|
|
|
FAIL,
|
|
|
|
b'Fie\nFoe\nfee\nfum\n',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
b'Fie\nFoe\nfee\nfum\n',
|
|
|
|
[],
|
|
|
|
PASS,
|
|
|
|
b'Fie\nFoe\nfee\nfum\n',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
b'fee\nFie\nFoe\nfum\n',
|
|
|
|
['--ignore-case'],
|
|
|
|
PASS,
|
|
|
|
b'fee\nFie\nFoe\nfum\n',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
b'Fie\nFoe\nfee\nfum\n',
|
|
|
|
['--ignore-case'],
|
|
|
|
FAIL,
|
|
|
|
b'fee\nFie\nFoe\nfum\n',
|
|
|
|
),
|
2020-10-25 12:05:40 +08:00
|
|
|
(
|
|
|
|
b'Fie\nFoe\nfee\nfee\nfum\n',
|
|
|
|
['--ignore-case'],
|
|
|
|
FAIL,
|
|
|
|
b'fee\nfee\nFie\nFoe\nfum\n',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
b'Fie\nFoe\nfee\nfum\n',
|
|
|
|
['--unique'],
|
|
|
|
PASS,
|
|
|
|
b'Fie\nFoe\nfee\nfum\n',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
b'Fie\nFie\nFoe\nfee\nfum\n',
|
|
|
|
['--unique'],
|
|
|
|
FAIL,
|
|
|
|
b'Fie\nFoe\nfee\nfum\n',
|
|
|
|
),
|
2024-10-06 12:57:18 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_integration(input_s, argv, expected_retval, output, tmpdir):
|
|
|
|
path = tmpdir.join('file.txt')
|
|
|
|
path.write_binary(input_s)
|
|
|
|
|
|
|
|
output_retval = main([str(path)] + argv)
|
|
|
|
|
|
|
|
assert path.read_binary() == output
|
|
|
|
assert output_retval == expected_retval
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('input_s', 'argv'),
|
|
|
|
(
|
2020-10-25 12:05:40 +08:00
|
|
|
(
|
|
|
|
b'fee\nFie\nFoe\nfum\n',
|
|
|
|
['--unique', '--ignore-case'],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
b'fee\nfee\nFie\nFoe\nfum\n',
|
|
|
|
['--unique', '--ignore-case'],
|
|
|
|
),
|
2017-07-13 09:35:24 +08:00
|
|
|
),
|
2017-06-23 15:33:13 +08:00
|
|
|
)
|
2024-10-06 12:57:18 +08:00
|
|
|
def test_integration_invalid_args(input_s, argv, tmpdir):
|
2017-06-23 15:33:13 +08:00
|
|
|
path = tmpdir.join('file.txt')
|
|
|
|
path.write_binary(input_s)
|
|
|
|
|
2024-10-06 12:57:18 +08:00
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
main([str(path)] + argv)
|