Implement Markdown trailing space line break preservation

Markdown uses two or more trailing spaces on a line to indicate a forced
line break `<br/>` - these will be preserved for files with a markdown
extension (default = `.md` or `.markdown`).

Add `--markdown-linebreak-ext=X,Y` to add extensions (`*` matches any),
and `--no-markdown-linebreak-ext` to disable this feature.

If you want to set specific extension `foo` only (and not md/markdown),
use `--no-markdown-linebreak-ext --markdown-linebreak-ext=foo`

Tries to prevent --markdown-linebreak-ext from eating filenames as if they were
extensions by rejecting any with '.' or '/' (or even Windows-style '\' or ':')

Update README.md to include information on these arguments as well as
arguments added to other hooks

Add extensive tests using pytest.mark.parametrize

test that `txt` file is not considered as 'txt' extension
test that `.txt` file is not considered as 'txt' extension

The latter is the (correct) behavior of os.path.splitext(), and an example
of why it is better to use the libraries than to mangle strings yourself.
This commit is contained in:
Alexander Dupuy 2015-05-10 10:00:54 +02:00
parent 822d83a142
commit a6023ac0d7
5 changed files with 170 additions and 14 deletions

View file

@ -1,6 +1,10 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import sys
import pytest
from pre_commit_hooks.trailing_whitespace_fixer import fix_trailing_whitespace
from testing.util import cwd
@ -12,7 +16,7 @@ def test_fixes_trailing_whitespace(tmpdir):
('bar.py', 'bar\t\nbaz\t\n'),
):
with open(filename, 'w') as file_obj:
file_obj.write(contents) # pragma: no cover (26 coverage bug)
file_obj.write(contents) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace(['foo.py', 'bar.py'])
assert ret == 1
@ -24,5 +28,103 @@ def test_fixes_trailing_whitespace(tmpdir):
assert open(filename).read() == after_contents
# filename, expected input, expected output
# pylint: disable=bad-whitespace
MD_TESTS_1 = (
('foo.md', 'foo \nbar \n ', 'foo \nbar\n\n'),
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
('.md', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
)
# pylint: enable=bad-whitespace
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_1)
def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace([filename])
assert ret == 1
assert open(filename).read() == output
# filename, expected input, expected output
# pylint: disable=bad-whitespace
MD_TESTS_2 = (
('foo.txt', 'foo \nbar \n \n', 'foo \nbar\n\n'),
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz\n\n'),
('.txt', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
)
# pylint: enable=bad-whitespace
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_2)
def test_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace(['--markdown-linebreak-ext=TxT',
filename])
assert ret == 1
assert open(filename).read() == output
# filename, expected input, expected output
# pylint: disable=bad-whitespace
MD_TESTS_3 = (
('foo.baz', 'foo \nbar \n ', 'foo \nbar\n\n'),
('bar', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
)
# pylint: enable=bad-whitespace
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_3)
def test_markdown_linebreak_ext_opt_all(filename, input_s, output, tmpdir):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
# need to make sure filename is not treated as argument to option
ret = fix_trailing_whitespace(['--markdown-linebreak-ext=*',
filename])
assert ret == 1
assert open(filename).read() == output
@pytest.mark.parametrize(('arg'), ('--', 'a.b', 'a/b'))
def test_markdown_linebreak_ext_badopt(arg):
try:
ret = fix_trailing_whitespace(['--markdown-linebreak-ext', arg])
except SystemExit:
ret = sys.exc_info()[1].code
finally:
assert ret == 2
# filename, expected input, expected output
# pylint: disable=bad-whitespace
MD_TESTS_4 = (
('bar.md', 'bar \nbaz\t \n\t\n', 'bar\nbaz\n\n'),
('bar.markdown', 'baz \nquux \n', 'baz\nquux\n'),
)
# pylint: enable=bad-whitespace
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_4)
def test_no_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
with cwd(tmpdir.strpath):
with open(filename, 'w') as file_obj:
file_obj.write(input_s) # pragma: no branch (26 coverage bug)
ret = fix_trailing_whitespace(['--no-markdown-linebreak-ext', filename])
assert ret == 1
assert open(filename).read() == output
def test_returns_zero_for_no_changes():
assert fix_trailing_whitespace([__file__]) == 0