blob: a7e797194ec3140b686c93149436215ff33e5a6c [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Morgan Courbetfc8a5b22017-06-13 21:38:14 +02003import pytest
4
Anthony Sottilefbcd0962017-09-05 20:20:43 -07005from pre_commit_hooks.mixed_line_ending import main
Morgan Courbetfc8a5b22017-06-13 21:38:14 +02006
7
8@pytest.mark.parametrize(
Anthony Sottilefbcd0962017-09-05 20:20:43 -07009 ('input_s', 'output'),
10 (
11 # mixed with majority of 'LF'
12 (b'foo\r\nbar\nbaz\n', b'foo\nbar\nbaz\n'),
13 # mixed with majority of 'CRLF'
14 (b'foo\r\nbar\nbaz\r\n', b'foo\r\nbar\r\nbaz\r\n'),
15 # mixed with majority of 'CR'
16 (b'foo\rbar\nbaz\r', b'foo\rbar\rbaz\r'),
17 # mixed with as much 'LF' as 'CRLF'
18 (b'foo\r\nbar\n', b'foo\nbar\n'),
19 # mixed with as much 'LF' as 'CR'
20 (b'foo\rbar\n', b'foo\nbar\n'),
21 # mixed with as much 'CRLF' as 'CR'
22 (b'foo\r\nbar\r', b'foo\r\nbar\r\n'),
23 # mixed with as much 'CRLF' as 'LF' as 'CR'
24 (b'foo\r\nbar\nbaz\r', b'foo\nbar\nbaz\n'),
25 ),
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020026)
Anthony Sottilefbcd0962017-09-05 20:20:43 -070027def test_mixed_line_ending_fixes_auto(input_s, output, tmpdir):
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020028 path = tmpdir.join('file.txt')
Anthony Sottile47c4d9e2017-09-05 19:27:34 -070029 path.write_binary(input_s)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040030 ret = main((str(path),))
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020031
Anthony Sottilefbcd0962017-09-05 20:20:43 -070032 assert ret == 1
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020033 assert path.read_binary() == output
34
35
Anthony Sottilefbcd0962017-09-05 20:20:43 -070036def test_non_mixed_no_newline_end_of_file(tmpdir):
37 path = tmpdir.join('f.txt')
38 path.write_binary(b'foo\nbar\nbaz')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040039 assert not main((str(path),))
Anthony Sottilefbcd0962017-09-05 20:20:43 -070040 # the hook *could* fix the end of the file, but leaves it alone
41 # this is mostly to document the current behaviour
42 assert path.read_binary() == b'foo\nbar\nbaz'
43
44
45def test_mixed_no_newline_end_of_file(tmpdir):
46 path = tmpdir.join('f.txt')
47 path.write_binary(b'foo\r\nbar\nbaz')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040048 assert main((str(path),))
Anthony Sottilefbcd0962017-09-05 20:20:43 -070049 # the hook rewrites the end of the file, this is slightly inconsistent
50 # with the non-mixed case but I think this is the better behaviour
51 # this is mostly to document the current behaviour
52 assert path.read_binary() == b'foo\nbar\nbaz\n'
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020053
54
55@pytest.mark.parametrize(
Anthony Sottilefbcd0962017-09-05 20:20:43 -070056 ('fix_option', 'input_s'),
57 (
58 # All --fix=auto with uniform line endings should be ok
59 ('--fix=auto', b'foo\r\nbar\r\nbaz\r\n'),
60 ('--fix=auto', b'foo\rbar\rbaz\r'),
61 ('--fix=auto', b'foo\nbar\nbaz\n'),
62 # --fix=crlf with crlf endings
63 ('--fix=crlf', b'foo\r\nbar\r\nbaz\r\n'),
64 # --fix=lf with lf endings
65 ('--fix=lf', b'foo\nbar\nbaz\n'),
66 ),
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020067)
Anthony Sottile1566cf92018-12-03 08:34:05 -080068def test_line_endings_ok(fix_option, input_s, tmpdir, capsys):
Anthony Sottilefbcd0962017-09-05 20:20:43 -070069 path = tmpdir.join('input.txt')
Anthony Sottile47c4d9e2017-09-05 19:27:34 -070070 path.write_binary(input_s)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040071 ret = main((fix_option, str(path)))
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020072
Anthony Sottilefbcd0962017-09-05 20:20:43 -070073 assert ret == 0
74 assert path.read_binary() == input_s
Anthony Sottile1566cf92018-12-03 08:34:05 -080075 out, _ = capsys.readouterr()
76 assert out == ''
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020077
78
Anthony Sottile1566cf92018-12-03 08:34:05 -080079def test_no_fix_does_not_modify(tmpdir, capsys):
Anthony Sottilefbcd0962017-09-05 20:20:43 -070080 path = tmpdir.join('input.txt')
81 contents = b'foo\r\nbar\rbaz\nwomp\n'
82 path.write_binary(contents)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040083 ret = main(('--fix=no', str(path)))
Anthony Sottilefbcd0962017-09-05 20:20:43 -070084
85 assert ret == 1
86 assert path.read_binary() == contents
Anthony Sottile1566cf92018-12-03 08:34:05 -080087 out, _ = capsys.readouterr()
Anthony Sottilef5c42a02020-02-05 11:10:42 -080088 assert out == f'{path}: mixed line endings\n'
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020089
90
Anthony Sottile1566cf92018-12-03 08:34:05 -080091def test_fix_lf(tmpdir, capsys):
Anthony Sottilefbcd0962017-09-05 20:20:43 -070092 path = tmpdir.join('input.txt')
93 path.write_binary(b'foo\r\nbar\rbaz\n')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040094 ret = main(('--fix=lf', str(path)))
Morgan Courbetfc8a5b22017-06-13 21:38:14 +020095
Anthony Sottilefbcd0962017-09-05 20:20:43 -070096 assert ret == 1
97 assert path.read_binary() == b'foo\nbar\nbaz\n'
Anthony Sottile1566cf92018-12-03 08:34:05 -080098 out, _ = capsys.readouterr()
Anthony Sottilef5c42a02020-02-05 11:10:42 -080099 assert out == f'{path}: fixed mixed line endings\n'
Morgan Courbetfc8a5b22017-06-13 21:38:14 +0200100
101
Anthony Sottilefbcd0962017-09-05 20:20:43 -0700102def test_fix_crlf(tmpdir):
103 path = tmpdir.join('input.txt')
104 path.write_binary(b'foo\r\nbar\rbaz\n')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -0400105 ret = main(('--fix=crlf', str(path)))
Morgan Courbetfc8a5b22017-06-13 21:38:14 +0200106
Anthony Sottilefbcd0962017-09-05 20:20:43 -0700107 assert ret == 1
108 assert path.read_binary() == b'foo\r\nbar\r\nbaz\r\n'
Anthony Sottile76047f62017-09-27 07:47:24 -0700109
110
111def test_fix_lf_all_crlf(tmpdir):
112 """Regression test for #239"""
113 path = tmpdir.join('input.txt')
114 path.write_binary(b'foo\r\nbar\r\n')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -0400115 ret = main(('--fix=lf', str(path)))
Anthony Sottile76047f62017-09-27 07:47:24 -0700116
117 assert ret == 1
118 assert path.read_binary() == b'foo\nbar\n'