2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-05-10 16:00:54 +08:00
|
|
|
import pytest
|
|
|
|
|
2018-03-01 00:43:07 +08:00
|
|
|
from pre_commit_hooks.trailing_whitespace_fixer import main
|
2014-04-14 13:21:42 +08:00
|
|
|
|
|
|
|
|
2016-05-28 05:09:50 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('input_s', 'expected'),
|
|
|
|
(
|
|
|
|
('foo \nbar \n', 'foo\nbar\n'),
|
|
|
|
('bar\t\nbaz\t\n', 'bar\nbaz\n'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
|
2018-10-13 09:10:02 +08:00
|
|
|
path = tmpdir.join('file.md')
|
2016-05-28 05:09:50 +08:00
|
|
|
path.write(input_s)
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(path),)) == 1
|
2016-05-28 05:09:50 +08:00
|
|
|
assert path.read() == expected
|
2014-04-14 13:21:42 +08:00
|
|
|
|
|
|
|
|
2018-03-01 00:43:07 +08:00
|
|
|
def test_ok_no_newline_end_of_file(tmpdir):
|
|
|
|
filename = tmpdir.join('f')
|
|
|
|
filename.write_binary(b'foo\nbar')
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main((str(filename),))
|
2018-03-01 00:43:07 +08:00
|
|
|
assert filename.read_binary() == b'foo\nbar'
|
|
|
|
assert ret == 0
|
|
|
|
|
|
|
|
|
2017-02-08 01:36:39 +08:00
|
|
|
def test_ok_with_dos_line_endings(tmpdir):
|
|
|
|
filename = tmpdir.join('f')
|
|
|
|
filename.write_binary(b'foo\r\nbar\r\nbaz\r\n')
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main((str(filename),))
|
2017-02-08 01:36:39 +08:00
|
|
|
assert filename.read_binary() == b'foo\r\nbar\r\nbaz\r\n'
|
|
|
|
assert ret == 0
|
|
|
|
|
|
|
|
|
2018-10-13 09:10:02 +08:00
|
|
|
@pytest.mark.parametrize('ext', ('md', 'Md', '.md', '*'))
|
|
|
|
def test_fixes_markdown_files(tmpdir, ext):
|
|
|
|
path = tmpdir.join('test.md')
|
|
|
|
path.write(
|
|
|
|
'foo \n' # leaves alone
|
|
|
|
'bar \n' # less than two so it is removed
|
|
|
|
'baz \n' # more than two so it becomes two spaces
|
|
|
|
'\t\n' # trailing tabs are stripped anyway
|
|
|
|
'\n ', # whitespace at the end of the file is removed
|
|
|
|
)
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main((str(path), f'--markdown-linebreak-ext={ext}'))
|
2016-05-28 05:09:50 +08:00
|
|
|
assert ret == 1
|
2018-10-13 09:10:02 +08:00
|
|
|
assert path.read() == (
|
|
|
|
'foo \n'
|
|
|
|
'bar\n'
|
|
|
|
'baz \n'
|
|
|
|
'\n'
|
|
|
|
'\n'
|
|
|
|
)
|
2015-05-10 16:00:54 +08:00
|
|
|
|
|
|
|
|
2018-10-13 09:10:02 +08:00
|
|
|
@pytest.mark.parametrize('arg', ('--', 'a.b', 'a/b', ''))
|
2015-05-10 16:00:54 +08:00
|
|
|
def test_markdown_linebreak_ext_badopt(arg):
|
2016-05-28 05:09:50 +08:00
|
|
|
with pytest.raises(SystemExit) as excinfo:
|
2018-03-01 00:43:07 +08:00
|
|
|
main(['--markdown-linebreak-ext', arg])
|
2016-05-28 05:09:50 +08:00
|
|
|
assert excinfo.value.code == 2
|
2015-05-10 16:00:54 +08:00
|
|
|
|
|
|
|
|
2018-10-13 09:10:02 +08:00
|
|
|
def test_prints_warning_with_no_markdown_ext(capsys, tmpdir):
|
|
|
|
f = tmpdir.join('f').ensure()
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(f), '--no-markdown-linebreak-ext')) == 0
|
2018-10-13 09:10:02 +08:00
|
|
|
out, _ = capsys.readouterr()
|
|
|
|
assert out == '--no-markdown-linebreak-ext now does nothing!\n'
|
2015-05-10 16:00:54 +08:00
|
|
|
|
|
|
|
|
2016-08-18 22:39:06 +08:00
|
|
|
def test_preserve_non_utf8_file(tmpdir):
|
2016-08-20 05:40:15 +08:00
|
|
|
non_utf8_bytes_content = b'<a>\xe9 \n</a>\n'
|
2016-08-18 22:39:06 +08:00
|
|
|
path = tmpdir.join('file.txt')
|
2016-08-20 05:40:15 +08:00
|
|
|
path.write_binary(non_utf8_bytes_content)
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main([str(path)])
|
2016-08-20 05:40:15 +08:00
|
|
|
assert ret == 1
|
|
|
|
assert path.size() == (len(non_utf8_bytes_content) - 1)
|
2019-10-25 23:34:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_custom_charset_change(tmpdir):
|
|
|
|
# strip spaces only, no tabs
|
|
|
|
path = tmpdir.join('file.txt')
|
|
|
|
path.write('\ta \t \n')
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main([str(path), '--chars', ' '])
|
2019-10-25 23:34:26 +08:00
|
|
|
assert ret == 1
|
|
|
|
assert path.read() == '\ta \t\n'
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_charset_no_change(tmpdir):
|
|
|
|
path = tmpdir.join('file.txt')
|
|
|
|
path.write('\ta \t\n')
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main([str(path), '--chars', ' '])
|
2019-10-25 23:34:26 +08:00
|
|
|
assert ret == 0
|
2019-10-26 00:28:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_markdown_with_custom_charset(tmpdir):
|
|
|
|
path = tmpdir.join('file.md')
|
|
|
|
path.write('\ta \t \n')
|
2020-05-21 00:07:45 +08:00
|
|
|
ret = main([str(path), '--chars', ' ', '--markdown-linebreak-ext', '*'])
|
2019-10-26 00:28:50 +08:00
|
|
|
assert ret == 1
|
|
|
|
assert path.read() == '\ta \t \n'
|