pre-commit-hooks/tests/mixed_line_ending_test.py

119 lines
3.6 KiB
Python
Raw Normal View History

from __future__ import annotations
2017-06-14 03:38:14 +08:00
import pytest
2017-09-06 11:20:43 +08:00
from pre_commit_hooks.mixed_line_ending import main
2017-06-14 03:38:14 +08:00
@pytest.mark.parametrize(
2017-09-06 11:20:43 +08:00
('input_s', 'output'),
(
# mixed with majority of 'LF'
(b'foo\r\nbar\nbaz\n', b'foo\nbar\nbaz\n'),
# mixed with majority of 'CRLF'
(b'foo\r\nbar\nbaz\r\n', b'foo\r\nbar\r\nbaz\r\n'),
# mixed with majority of 'CR'
(b'foo\rbar\nbaz\r', b'foo\rbar\rbaz\r'),
# mixed with as much 'LF' as 'CRLF'
(b'foo\r\nbar\n', b'foo\nbar\n'),
# mixed with as much 'LF' as 'CR'
(b'foo\rbar\n', b'foo\nbar\n'),
# mixed with as much 'CRLF' as 'CR'
(b'foo\r\nbar\r', b'foo\r\nbar\r\n'),
# mixed with as much 'CRLF' as 'LF' as 'CR'
(b'foo\r\nbar\nbaz\r', b'foo\nbar\nbaz\n'),
),
2017-06-14 03:38:14 +08:00
)
2017-09-06 11:20:43 +08:00
def test_mixed_line_ending_fixes_auto(input_s, output, tmpdir):
2017-06-14 03:38:14 +08:00
path = tmpdir.join('file.txt')
2017-09-06 10:27:34 +08:00
path.write_binary(input_s)
2020-05-21 00:07:45 +08:00
ret = main((str(path),))
2017-06-14 03:38:14 +08:00
2017-09-06 11:20:43 +08:00
assert ret == 1
2017-06-14 03:38:14 +08:00
assert path.read_binary() == output
2017-09-06 11:20:43 +08:00
def test_non_mixed_no_newline_end_of_file(tmpdir):
path = tmpdir.join('f.txt')
path.write_binary(b'foo\nbar\nbaz')
2020-05-21 00:07:45 +08:00
assert not main((str(path),))
2017-09-06 11:20:43 +08:00
# the hook *could* fix the end of the file, but leaves it alone
# this is mostly to document the current behaviour
assert path.read_binary() == b'foo\nbar\nbaz'
def test_mixed_no_newline_end_of_file(tmpdir):
path = tmpdir.join('f.txt')
path.write_binary(b'foo\r\nbar\nbaz')
2020-05-21 00:07:45 +08:00
assert main((str(path),))
2017-09-06 11:20:43 +08:00
# the hook rewrites the end of the file, this is slightly inconsistent
# with the non-mixed case but I think this is the better behaviour
# this is mostly to document the current behaviour
assert path.read_binary() == b'foo\nbar\nbaz\n'
2017-06-14 03:38:14 +08:00
@pytest.mark.parametrize(
2017-09-06 11:20:43 +08:00
('fix_option', 'input_s'),
(
# All --fix=auto with uniform line endings should be ok
('--fix=auto', b'foo\r\nbar\r\nbaz\r\n'),
('--fix=auto', b'foo\rbar\rbaz\r'),
('--fix=auto', b'foo\nbar\nbaz\n'),
# --fix=crlf with crlf endings
('--fix=crlf', b'foo\r\nbar\r\nbaz\r\n'),
# --fix=lf with lf endings
('--fix=lf', b'foo\nbar\nbaz\n'),
),
2017-06-14 03:38:14 +08:00
)
2018-12-04 00:34:05 +08:00
def test_line_endings_ok(fix_option, input_s, tmpdir, capsys):
2017-09-06 11:20:43 +08:00
path = tmpdir.join('input.txt')
2017-09-06 10:27:34 +08:00
path.write_binary(input_s)
2020-05-21 00:07:45 +08:00
ret = main((fix_option, str(path)))
2017-06-14 03:38:14 +08:00
2017-09-06 11:20:43 +08:00
assert ret == 0
assert path.read_binary() == input_s
2018-12-04 00:34:05 +08:00
out, _ = capsys.readouterr()
assert out == ''
2017-06-14 03:38:14 +08:00
2018-12-04 00:34:05 +08:00
def test_no_fix_does_not_modify(tmpdir, capsys):
2017-09-06 11:20:43 +08:00
path = tmpdir.join('input.txt')
contents = b'foo\r\nbar\rbaz\nwomp\n'
path.write_binary(contents)
2020-05-21 00:07:45 +08:00
ret = main(('--fix=no', str(path)))
2017-06-14 03:38:14 +08:00
2017-09-06 11:20:43 +08:00
assert ret == 1
assert path.read_binary() == contents
2018-12-04 00:34:05 +08:00
out, _ = capsys.readouterr()
2020-02-06 03:10:42 +08:00
assert out == f'{path}: mixed line endings\n'
2017-06-14 03:38:14 +08:00
2018-12-04 00:34:05 +08:00
def test_fix_lf(tmpdir, capsys):
2017-09-06 11:20:43 +08:00
path = tmpdir.join('input.txt')
path.write_binary(b'foo\r\nbar\rbaz\n')
2020-05-21 00:07:45 +08:00
ret = main(('--fix=lf', str(path)))
2017-06-14 03:38:14 +08:00
2017-09-06 11:20:43 +08:00
assert ret == 1
assert path.read_binary() == b'foo\nbar\nbaz\n'
2018-12-04 00:34:05 +08:00
out, _ = capsys.readouterr()
2020-02-06 03:10:42 +08:00
assert out == f'{path}: fixed mixed line endings\n'
2017-06-14 03:38:14 +08:00
2017-09-06 11:20:43 +08:00
def test_fix_crlf(tmpdir):
path = tmpdir.join('input.txt')
path.write_binary(b'foo\r\nbar\rbaz\n')
2020-05-21 00:07:45 +08:00
ret = main(('--fix=crlf', str(path)))
2017-06-14 03:38:14 +08:00
2017-09-06 11:20:43 +08:00
assert ret == 1
assert path.read_binary() == b'foo\r\nbar\r\nbaz\r\n'
def test_fix_lf_all_crlf(tmpdir):
"""Regression test for #239"""
path = tmpdir.join('input.txt')
path.write_binary(b'foo\r\nbar\r\n')
2020-05-21 00:07:45 +08:00
ret = main(('--fix=lf', str(path)))
assert ret == 1
assert path.read_binary() == b'foo\nbar\n'