Skip to content

Commit 99453a5

Browse files
committed
Default --no-markdown-linebreak-ext for trailing-whitespace
1 parent 78b2443 commit 99453a5

File tree

3 files changed

+40
-89
lines changed

3 files changed

+40
-89
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ Add this to your `.pre-commit-config.yaml`
9797
- `requirements-txt-fixer` - Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0`
9898
- `sort-simple-yaml` - Sorts simple YAML files which consist only of top-level keys, preserving comments and blocks.
9999
- `trailing-whitespace` - Trims trailing whitespace.
100-
- Markdown linebreak trailing spaces preserved for `.md` and`.markdown`;
101-
use `args: ['--markdown-linebreak-ext=txt,text']` to add other extensions,
102-
`args: ['--markdown-linebreak-ext=*']` to preserve them for all files,
103-
or `args: ['--no-markdown-linebreak-ext']` to disable and always trim.
100+
- To preserve Markdown [hard linebreaks](https://github.github.com/gfm/#hard-line-break)
101+
use `args: [--markdown-linebreak-ext=md]` (or other extensions used
102+
by your markdownfiles). If for some reason you want to treat all files
103+
as markdown, use `--markdown-linebreak-ext=*`.
104104

105105
### As a standalone package
106106

pre_commit_hooks/trailing_whitespace_fixer.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,25 @@ def main(argv=None):
3535
parser = argparse.ArgumentParser()
3636
parser.add_argument(
3737
'--no-markdown-linebreak-ext',
38-
action='store_const',
39-
const=[],
40-
default=argparse.SUPPRESS,
41-
dest='markdown_linebreak_ext',
42-
help='Do not preserve linebreak spaces in Markdown',
38+
action='store_true',
39+
help=argparse.SUPPRESS,
4340
)
4441
parser.add_argument(
4542
'--markdown-linebreak-ext',
4643
action='append',
47-
const='',
48-
default=['md,markdown'],
44+
default=[],
4945
metavar='*|EXT[,EXT,...]',
50-
nargs='?',
51-
help='Markdown extensions (or *) for linebreak spaces',
46+
help=(
47+
'Markdown extensions (or *) to not strip linebreak spaces. '
48+
'default: %(default)s'
49+
),
5250
)
5351
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
5452
args = parser.parse_args(argv)
5553

54+
if args.no_markdown_linebreak_ext:
55+
print('--no-markdown-linebreak-ext now does nothing!')
56+
5657
md_args = args.markdown_linebreak_ext
5758
if '' in md_args:
5859
parser.error('--markdown-linebreak-ext requires a non-empty argument')
@@ -66,7 +67,7 @@ def main(argv=None):
6667
for ext in md_exts:
6768
if any(c in ext[1:] for c in r'./\:'):
6869
parser.error(
69-
"bad --markdown-linebreak-ext extension '{}' (has . / \\ :)\n"
70+
"bad --markdown-linebreak-ext extension {!r} (has . / \\ :)\n"
7071
" (probably filename; use '--markdown-linebreak-ext=EXT')"
7172
.format(ext),
7273
)

tests/trailing_whitespace_fixer_test.py

Lines changed: 25 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
),
1515
)
1616
def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
17-
path = tmpdir.join('file.txt')
17+
path = tmpdir.join('file.md')
1818
path.write(input_s)
1919
assert main((path.strpath,)) == 1
2020
assert path.read() == expected
@@ -36,89 +36,39 @@ def test_ok_with_dos_line_endings(tmpdir):
3636
assert ret == 0
3737

3838

39-
def test_markdown_ok(tmpdir):
40-
filename = tmpdir.join('foo.md')
41-
filename.write_binary(b'foo \n')
42-
ret = main((filename.strpath,))
43-
assert filename.read_binary() == b'foo \n'
44-
assert ret == 0
45-
46-
47-
# filename, expected input, expected output
48-
MD_TESTS_1 = (
49-
('foo.md', 'foo \nbar \n ', 'foo \nbar\n'),
50-
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
51-
('.md', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
52-
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
53-
)
54-
55-
56-
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_1)
57-
def test_fixes_trailing_markdown_whitespace(filename, input_s, output, tmpdir):
58-
path = tmpdir.join(filename)
59-
path.write(input_s)
60-
ret = main([path.strpath])
39+
@pytest.mark.parametrize('ext', ('md', 'Md', '.md', '*'))
40+
def test_fixes_markdown_files(tmpdir, ext):
41+
path = tmpdir.join('test.md')
42+
path.write(
43+
'foo \n' # leaves alone
44+
'bar \n' # less than two so it is removed
45+
'baz \n' # more than two so it becomes two spaces
46+
'\t\n' # trailing tabs are stripped anyway
47+
'\n ', # whitespace at the end of the file is removed
48+
)
49+
ret = main((path.strpath, '--markdown-linebreak-ext={}'.format(ext)))
6150
assert ret == 1
62-
assert path.read() == output
51+
assert path.read() == (
52+
'foo \n'
53+
'bar\n'
54+
'baz \n'
55+
'\n'
56+
'\n'
57+
)
6358

6459

65-
# filename, expected input, expected output
66-
MD_TESTS_2 = (
67-
('foo.txt', 'foo \nbar \n \n', 'foo \nbar\n\n'),
68-
('bar.Markdown', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
69-
('bar.MD', 'bar \nbaz\t \n\t\n', 'bar \nbaz \n\n'),
70-
('.txt', 'baz \nquux \t\n\t\n', 'baz\nquux\n\n'),
71-
('txt', 'foo \nbaz \n\t\n', 'foo\nbaz\n\n'),
72-
)
73-
74-
75-
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_2)
76-
def test_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
77-
path = tmpdir.join(filename)
78-
path.write(input_s)
79-
ret = main(('--markdown-linebreak-ext=TxT', path.strpath))
80-
assert ret == 1
81-
assert path.read() == output
82-
83-
84-
# filename, expected input, expected output
85-
MD_TESTS_3 = (
86-
('foo.baz', 'foo \nbar \n ', 'foo \nbar\n'),
87-
('bar', 'bar \nbaz\t\n\t\n', 'bar \nbaz\n\n'),
88-
)
89-
90-
91-
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_3)
92-
def test_markdown_linebreak_ext_opt_all(filename, input_s, output, tmpdir):
93-
path = tmpdir.join(filename)
94-
path.write(input_s)
95-
# need to make sure filename is not treated as argument to option
96-
ret = main(('--markdown-linebreak-ext=*', path.strpath))
97-
assert ret == 1
98-
assert path.read() == output
99-
100-
101-
@pytest.mark.parametrize(('arg'), ('--', 'a.b', 'a/b'))
60+
@pytest.mark.parametrize('arg', ('--', 'a.b', 'a/b', ''))
10261
def test_markdown_linebreak_ext_badopt(arg):
10362
with pytest.raises(SystemExit) as excinfo:
10463
main(['--markdown-linebreak-ext', arg])
10564
assert excinfo.value.code == 2
10665

10766

108-
# filename, expected input, expected output
109-
MD_TESTS_4 = (
110-
('bar.md', 'bar \nbaz\t \n\t\n', 'bar\nbaz\n\n'),
111-
('bar.markdown', 'baz \nquux \n', 'baz\nquux\n'),
112-
)
113-
114-
115-
@pytest.mark.parametrize(('filename', 'input_s', 'output'), MD_TESTS_4)
116-
def test_no_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
117-
path = tmpdir.join(filename)
118-
path.write(input_s)
119-
ret = main(['--no-markdown-linebreak-ext', path.strpath])
120-
assert ret == 1
121-
assert path.read() == output
67+
def test_prints_warning_with_no_markdown_ext(capsys, tmpdir):
68+
f = tmpdir.join('f').ensure()
69+
assert main((f.strpath, '--no-markdown-linebreak-ext')) == 0
70+
out, _ = capsys.readouterr()
71+
assert out == '--no-markdown-linebreak-ext now does nothing!\n'
12272

12373

12474
def test_preserve_non_utf8_file(tmpdir):

0 commit comments

Comments
 (0)