blob: c07497a2215886873b6b68dfd0e84062960d637c [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Alexander Dupuya6023ac2015-05-10 10:00:54 +02003import pytest
4
Anthony Sottile38e02ff2018-02-28 08:43:07 -08005from pre_commit_hooks.trailing_whitespace_fixer import main
Anthony Sottile8270d812014-04-13 22:21:42 -07006
7
Anthony Sottilea99475a2016-05-27 14:09:50 -07008@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)
15def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
Anthony Sottile99453a52018-10-12 18:10:02 -070016 path = tmpdir.join('file.md')
Anthony Sottilea99475a2016-05-27 14:09:50 -070017 path.write(input_s)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040018 assert main((str(path),)) == 1
Anthony Sottilea99475a2016-05-27 14:09:50 -070019 assert path.read() == expected
Anthony Sottile8270d812014-04-13 22:21:42 -070020
21
Anthony Sottile38e02ff2018-02-28 08:43:07 -080022def test_ok_no_newline_end_of_file(tmpdir):
23 filename = tmpdir.join('f')
24 filename.write_binary(b'foo\nbar')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040025 ret = main((str(filename),))
Anthony Sottile38e02ff2018-02-28 08:43:07 -080026 assert filename.read_binary() == b'foo\nbar'
27 assert ret == 0
28
29
Anthony Sottile81147332017-02-07 09:36:39 -080030def 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 Rozentsveygf35bfed2020-05-20 12:07:45 -040033 ret = main((str(filename),))
Anthony Sottile81147332017-02-07 09:36:39 -080034 assert filename.read_binary() == b'foo\r\nbar\r\nbaz\r\n'
35 assert ret == 0
36
37
Anthony Sottile99453a52018-10-12 18:10:02 -070038@pytest.mark.parametrize('ext', ('md', 'Md', '.md', '*'))
39def 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 Rozentsveygf35bfed2020-05-20 12:07:45 -040048 ret = main((str(path), f'--markdown-linebreak-ext={ext}'))
Anthony Sottilea99475a2016-05-27 14:09:50 -070049 assert ret == 1
Anthony Sottile99453a52018-10-12 18:10:02 -070050 assert path.read() == (
51 'foo \n'
52 'bar\n'
53 'baz \n'
54 '\n'
55 '\n'
56 )
Alexander Dupuya6023ac2015-05-10 10:00:54 +020057
58
Anthony Sottile99453a52018-10-12 18:10:02 -070059@pytest.mark.parametrize('arg', ('--', 'a.b', 'a/b', ''))
Alexander Dupuya6023ac2015-05-10 10:00:54 +020060def test_markdown_linebreak_ext_badopt(arg):
Anthony Sottilea99475a2016-05-27 14:09:50 -070061 with pytest.raises(SystemExit) as excinfo:
Anthony Sottile38e02ff2018-02-28 08:43:07 -080062 main(['--markdown-linebreak-ext', arg])
Anthony Sottilea99475a2016-05-27 14:09:50 -070063 assert excinfo.value.code == 2
Alexander Dupuya6023ac2015-05-10 10:00:54 +020064
65
Anthony Sottile99453a52018-10-12 18:10:02 -070066def test_prints_warning_with_no_markdown_ext(capsys, tmpdir):
67 f = tmpdir.join('f').ensure()
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040068 assert main((str(f), '--no-markdown-linebreak-ext')) == 0
Anthony Sottile99453a52018-10-12 18:10:02 -070069 out, _ = capsys.readouterr()
70 assert out == '--no-markdown-linebreak-ext now does nothing!\n'
Alexander Dupuya6023ac2015-05-10 10:00:54 +020071
72
Lucas Cimonbc5e7f22016-08-18 16:39:06 +020073def test_preserve_non_utf8_file(tmpdir):
Lucas Cimoneaad9232016-08-19 22:40:15 +010074 non_utf8_bytes_content = b'<a>\xe9 \n</a>\n'
Lucas Cimonbc5e7f22016-08-18 16:39:06 +020075 path = tmpdir.join('file.txt')
Lucas Cimoneaad9232016-08-19 22:40:15 +010076 path.write_binary(non_utf8_bytes_content)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040077 ret = main([str(path)])
Lucas Cimoneaad9232016-08-19 22:40:15 +010078 assert ret == 1
79 assert path.size() == (len(non_utf8_bytes_content) - 1)
iconmaster5326a2f836a2019-10-25 11:34:26 -040080
81
82def test_custom_charset_change(tmpdir):
83 # strip spaces only, no tabs
84 path = tmpdir.join('file.txt')
85 path.write('\ta \t \n')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040086 ret = main([str(path), '--chars', ' '])
iconmaster5326a2f836a2019-10-25 11:34:26 -040087 assert ret == 1
88 assert path.read() == '\ta \t\n'
89
90
91def test_custom_charset_no_change(tmpdir):
92 path = tmpdir.join('file.txt')
93 path.write('\ta \t\n')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040094 ret = main([str(path), '--chars', ' '])
iconmaster5326a2f836a2019-10-25 11:34:26 -040095 assert ret == 0
iconmaster532601149622019-10-25 12:28:50 -040096
97
98def test_markdown_with_custom_charset(tmpdir):
99 path = tmpdir.join('file.md')
100 path.write('\ta \t \n')
Max Rozentsveygf35bfed2020-05-20 12:07:45 -0400101 ret = main([str(path), '--chars', ' ', '--markdown-linebreak-ext', '*'])
iconmaster532601149622019-10-25 12:28:50 -0400102 assert ret == 1
103 assert path.read() == '\ta \t \n'