Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Alexander Dupuy | a6023ac | 2015-05-10 10:00:54 +0200 | [diff] [blame] | 3 | import pytest |
| 4 | |
Anthony Sottile | 38e02ff | 2018-02-28 08:43:07 -0800 | [diff] [blame] | 5 | from pre_commit_hooks.trailing_whitespace_fixer import main |
Anthony Sottile | 8270d81 | 2014-04-13 22:21:42 -0700 | [diff] [blame] | 6 | |
| 7 | |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 8 | @pytest.mark.parametrize( |
| 9 | ('input_s', 'expected'), |
| 10 | ( |
| 11 | ('foo \nbar \n', 'foo\nbar\n'), |
| 12 | ('bar\t\nbaz\t\n', 'bar\nbaz\n'), |
| 13 | ), |
| 14 | ) |
| 15 | def test_fixes_trailing_whitespace(input_s, expected, tmpdir): |
Anthony Sottile | 99453a5 | 2018-10-12 18:10:02 -0700 | [diff] [blame] | 16 | path = tmpdir.join('file.md') |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 17 | path.write(input_s) |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 18 | assert main((str(path),)) == 1 |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 19 | assert path.read() == expected |
Anthony Sottile | 8270d81 | 2014-04-13 22:21:42 -0700 | [diff] [blame] | 20 | |
| 21 | |
Anthony Sottile | 38e02ff | 2018-02-28 08:43:07 -0800 | [diff] [blame] | 22 | def test_ok_no_newline_end_of_file(tmpdir): |
| 23 | filename = tmpdir.join('f') |
| 24 | filename.write_binary(b'foo\nbar') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 25 | ret = main((str(filename),)) |
Anthony Sottile | 38e02ff | 2018-02-28 08:43:07 -0800 | [diff] [blame] | 26 | assert filename.read_binary() == b'foo\nbar' |
| 27 | assert ret == 0 |
| 28 | |
| 29 | |
Anthony Sottile | 8114733 | 2017-02-07 09:36:39 -0800 | [diff] [blame] | 30 | def test_ok_with_dos_line_endings(tmpdir): |
| 31 | filename = tmpdir.join('f') |
| 32 | filename.write_binary(b'foo\r\nbar\r\nbaz\r\n') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 33 | ret = main((str(filename),)) |
Anthony Sottile | 8114733 | 2017-02-07 09:36:39 -0800 | [diff] [blame] | 34 | assert filename.read_binary() == b'foo\r\nbar\r\nbaz\r\n' |
| 35 | assert ret == 0 |
| 36 | |
| 37 | |
Anthony Sottile | 99453a5 | 2018-10-12 18:10:02 -0700 | [diff] [blame] | 38 | @pytest.mark.parametrize('ext', ('md', 'Md', '.md', '*')) |
| 39 | def test_fixes_markdown_files(tmpdir, ext): |
| 40 | path = tmpdir.join('test.md') |
| 41 | path.write( |
| 42 | 'foo \n' # leaves alone |
| 43 | 'bar \n' # less than two so it is removed |
| 44 | 'baz \n' # more than two so it becomes two spaces |
| 45 | '\t\n' # trailing tabs are stripped anyway |
| 46 | '\n ', # whitespace at the end of the file is removed |
| 47 | ) |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 48 | ret = main((str(path), f'--markdown-linebreak-ext={ext}')) |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 49 | assert ret == 1 |
Anthony Sottile | 99453a5 | 2018-10-12 18:10:02 -0700 | [diff] [blame] | 50 | assert path.read() == ( |
| 51 | 'foo \n' |
| 52 | 'bar\n' |
| 53 | 'baz \n' |
| 54 | '\n' |
| 55 | '\n' |
| 56 | ) |
Alexander Dupuy | a6023ac | 2015-05-10 10:00:54 +0200 | [diff] [blame] | 57 | |
| 58 | |
Anthony Sottile | 99453a5 | 2018-10-12 18:10:02 -0700 | [diff] [blame] | 59 | @pytest.mark.parametrize('arg', ('--', 'a.b', 'a/b', '')) |
Alexander Dupuy | a6023ac | 2015-05-10 10:00:54 +0200 | [diff] [blame] | 60 | def test_markdown_linebreak_ext_badopt(arg): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 61 | with pytest.raises(SystemExit) as excinfo: |
Anthony Sottile | 38e02ff | 2018-02-28 08:43:07 -0800 | [diff] [blame] | 62 | main(['--markdown-linebreak-ext', arg]) |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 63 | assert excinfo.value.code == 2 |
Alexander Dupuy | a6023ac | 2015-05-10 10:00:54 +0200 | [diff] [blame] | 64 | |
| 65 | |
Anthony Sottile | 99453a5 | 2018-10-12 18:10:02 -0700 | [diff] [blame] | 66 | def test_prints_warning_with_no_markdown_ext(capsys, tmpdir): |
| 67 | f = tmpdir.join('f').ensure() |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 68 | assert main((str(f), '--no-markdown-linebreak-ext')) == 0 |
Anthony Sottile | 99453a5 | 2018-10-12 18:10:02 -0700 | [diff] [blame] | 69 | out, _ = capsys.readouterr() |
| 70 | assert out == '--no-markdown-linebreak-ext now does nothing!\n' |
Alexander Dupuy | a6023ac | 2015-05-10 10:00:54 +0200 | [diff] [blame] | 71 | |
| 72 | |
Lucas Cimon | bc5e7f2 | 2016-08-18 16:39:06 +0200 | [diff] [blame] | 73 | def test_preserve_non_utf8_file(tmpdir): |
Lucas Cimon | eaad923 | 2016-08-19 22:40:15 +0100 | [diff] [blame] | 74 | non_utf8_bytes_content = b'<a>\xe9 \n</a>\n' |
Lucas Cimon | bc5e7f2 | 2016-08-18 16:39:06 +0200 | [diff] [blame] | 75 | path = tmpdir.join('file.txt') |
Lucas Cimon | eaad923 | 2016-08-19 22:40:15 +0100 | [diff] [blame] | 76 | path.write_binary(non_utf8_bytes_content) |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 77 | ret = main([str(path)]) |
Lucas Cimon | eaad923 | 2016-08-19 22:40:15 +0100 | [diff] [blame] | 78 | assert ret == 1 |
| 79 | assert path.size() == (len(non_utf8_bytes_content) - 1) |
iconmaster5326 | a2f836a | 2019-10-25 11:34:26 -0400 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def test_custom_charset_change(tmpdir): |
| 83 | # strip spaces only, no tabs |
| 84 | path = tmpdir.join('file.txt') |
| 85 | path.write('\ta \t \n') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 86 | ret = main([str(path), '--chars', ' ']) |
iconmaster5326 | a2f836a | 2019-10-25 11:34:26 -0400 | [diff] [blame] | 87 | assert ret == 1 |
| 88 | assert path.read() == '\ta \t\n' |
| 89 | |
| 90 | |
| 91 | def test_custom_charset_no_change(tmpdir): |
| 92 | path = tmpdir.join('file.txt') |
| 93 | path.write('\ta \t\n') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 94 | ret = main([str(path), '--chars', ' ']) |
iconmaster5326 | a2f836a | 2019-10-25 11:34:26 -0400 | [diff] [blame] | 95 | assert ret == 0 |
iconmaster5326 | 0114962 | 2019-10-25 12:28:50 -0400 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def test_markdown_with_custom_charset(tmpdir): |
| 99 | path = tmpdir.join('file.md') |
| 100 | path.write('\ta \t \n') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 101 | ret = main([str(path), '--chars', ' ', '--markdown-linebreak-ext', '*']) |
iconmaster5326 | 0114962 | 2019-10-25 12:28:50 -0400 | [diff] [blame] | 102 | assert ret == 1 |
| 103 | assert path.read() == '\ta \t \n' |