|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import unicode_literals |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pre_commit_hooks.mixed_line_ending import main |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + ('input_s', 'output'), |
| 11 | + ( |
| 12 | + # mixed with majority of 'LF' |
| 13 | + (b'foo\r\nbar\nbaz\n', b'foo\nbar\nbaz\n'), |
| 14 | + # mixed with majority of 'CRLF' |
| 15 | + (b'foo\r\nbar\nbaz\r\n', b'foo\r\nbar\r\nbaz\r\n'), |
| 16 | + # mixed with majority of 'CR' |
| 17 | + (b'foo\rbar\nbaz\r', b'foo\rbar\rbaz\r'), |
| 18 | + # mixed with as much 'LF' as 'CRLF' |
| 19 | + (b'foo\r\nbar\n', b'foo\nbar\n'), |
| 20 | + # mixed with as much 'LF' as 'CR' |
| 21 | + (b'foo\rbar\n', b'foo\nbar\n'), |
| 22 | + # mixed with as much 'CRLF' as 'CR' |
| 23 | + (b'foo\r\nbar\r', b'foo\r\nbar\r\n'), |
| 24 | + # mixed with as much 'CRLF' as 'LF' as 'CR' |
| 25 | + (b'foo\r\nbar\nbaz\r', b'foo\nbar\nbaz\n'), |
| 26 | + ), |
| 27 | +) |
| 28 | +def test_mixed_line_ending_fixes_auto(input_s, output, tmpdir): |
| 29 | + path = tmpdir.join('file.txt') |
| 30 | + path.write_binary(input_s) |
| 31 | + ret = main((path.strpath,)) |
| 32 | + |
| 33 | + assert ret == 1 |
| 34 | + assert path.read_binary() == output |
| 35 | + |
| 36 | + |
| 37 | +def test_non_mixed_no_newline_end_of_file(tmpdir): |
| 38 | + path = tmpdir.join('f.txt') |
| 39 | + path.write_binary(b'foo\nbar\nbaz') |
| 40 | + assert not main((path.strpath,)) |
| 41 | + # the hook *could* fix the end of the file, but leaves it alone |
| 42 | + # this is mostly to document the current behaviour |
| 43 | + assert path.read_binary() == b'foo\nbar\nbaz' |
| 44 | + |
| 45 | + |
| 46 | +def test_mixed_no_newline_end_of_file(tmpdir): |
| 47 | + path = tmpdir.join('f.txt') |
| 48 | + path.write_binary(b'foo\r\nbar\nbaz') |
| 49 | + assert main((path.strpath,)) |
| 50 | + # the hook rewrites the end of the file, this is slightly inconsistent |
| 51 | + # with the non-mixed case but I think this is the better behaviour |
| 52 | + # this is mostly to document the current behaviour |
| 53 | + assert path.read_binary() == b'foo\nbar\nbaz\n' |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize( |
| 57 | + ('fix_option', 'input_s'), |
| 58 | + ( |
| 59 | + # All --fix=auto with uniform line endings should be ok |
| 60 | + ('--fix=auto', b'foo\r\nbar\r\nbaz\r\n'), |
| 61 | + ('--fix=auto', b'foo\rbar\rbaz\r'), |
| 62 | + ('--fix=auto', b'foo\nbar\nbaz\n'), |
| 63 | + # --fix=crlf with crlf endings |
| 64 | + ('--fix=crlf', b'foo\r\nbar\r\nbaz\r\n'), |
| 65 | + # --fix=lf with lf endings |
| 66 | + ('--fix=lf', b'foo\nbar\nbaz\n'), |
| 67 | + ), |
| 68 | +) |
| 69 | +def test_line_endings_ok(fix_option, input_s, tmpdir): |
| 70 | + path = tmpdir.join('input.txt') |
| 71 | + path.write_binary(input_s) |
| 72 | + ret = main((fix_option, path.strpath)) |
| 73 | + |
| 74 | + assert ret == 0 |
| 75 | + assert path.read_binary() == input_s |
| 76 | + |
| 77 | + |
| 78 | +def test_no_fix_does_not_modify(tmpdir): |
| 79 | + path = tmpdir.join('input.txt') |
| 80 | + contents = b'foo\r\nbar\rbaz\nwomp\n' |
| 81 | + path.write_binary(contents) |
| 82 | + ret = main(('--fix=no', path.strpath)) |
| 83 | + |
| 84 | + assert ret == 1 |
| 85 | + assert path.read_binary() == contents |
| 86 | + |
| 87 | + |
| 88 | +def test_fix_lf(tmpdir): |
| 89 | + path = tmpdir.join('input.txt') |
| 90 | + path.write_binary(b'foo\r\nbar\rbaz\n') |
| 91 | + ret = main(('--fix=lf', path.strpath)) |
| 92 | + |
| 93 | + assert ret == 1 |
| 94 | + assert path.read_binary() == b'foo\nbar\nbaz\n' |
| 95 | + |
| 96 | + |
| 97 | +def test_fix_crlf(tmpdir): |
| 98 | + path = tmpdir.join('input.txt') |
| 99 | + path.write_binary(b'foo\r\nbar\rbaz\n') |
| 100 | + ret = main(('--fix=crlf', path.strpath)) |
| 101 | + |
| 102 | + assert ret == 1 |
| 103 | + assert path.read_binary() == b'foo\r\nbar\r\nbaz\r\n' |
0 commit comments