mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-30 02:16:52 +00:00
Merge pull request #172 from pre-commit/fix_trailing_whitespace
Only return nonzero when rewriting whitespace
This commit is contained in:
commit
f9ce156915
3 changed files with 34 additions and 12 deletions
|
|
@ -1,5 +1,5 @@
|
|||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
sha: 9ba5af45ce2d29b64c9a348a6fcff5553eea1f2c
|
||||
sha: v0.7.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
- id: end-of-file-fixer
|
||||
|
|
@ -13,12 +13,12 @@
|
|||
- id: requirements-txt-fixer
|
||||
- id: flake8
|
||||
- repo: https://github.com/pre-commit/pre-commit
|
||||
sha: 8dba3281d5051060755459dcf88e28fc26c27526
|
||||
sha: v0.12.2
|
||||
hooks:
|
||||
- id: validate_config
|
||||
- id: validate_manifest
|
||||
- repo: https://github.com/asottile/reorder_python_imports
|
||||
sha: 3d86483455ab5bd06cc1069fdd5ac57be5463f10
|
||||
sha: v0.3.1
|
||||
hooks:
|
||||
- id: reorder-python-imports
|
||||
language_version: python2.7
|
||||
|
|
|
|||
|
|
@ -10,10 +10,14 @@ from pre_commit_hooks.util import cmd_output
|
|||
def _fix_file(filename, is_markdown):
|
||||
with open(filename, mode='rb') as file_processed:
|
||||
lines = file_processed.readlines()
|
||||
lines = [_process_line(line, is_markdown) for line in lines]
|
||||
with open(filename, mode='wb') as file_processed:
|
||||
for line in lines:
|
||||
file_processed.write(line)
|
||||
newlines = [_process_line(line, is_markdown) for line in lines]
|
||||
if newlines != lines:
|
||||
with open(filename, mode='wb') as file_processed:
|
||||
for line in newlines:
|
||||
file_processed.write(line)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def _process_line(line, is_markdown):
|
||||
|
|
@ -55,8 +59,9 @@ def fix_trailing_whitespace(argv=None):
|
|||
parser.error('--markdown-linebreak-ext requires a non-empty argument')
|
||||
all_markdown = '*' in md_args
|
||||
# normalize all extensions; split at ',', lowercase, and force 1 leading '.'
|
||||
md_exts = ['.' + x.lower().lstrip('.')
|
||||
for x in ','.join(md_args).split(',')]
|
||||
md_exts = [
|
||||
'.' + x.lower().lstrip('.') for x in ','.join(md_args).split(',')
|
||||
]
|
||||
|
||||
# reject probable "eaten" filename as extension (skip leading '.' with [1:])
|
||||
for ext in md_exts:
|
||||
|
|
@ -69,10 +74,11 @@ def fix_trailing_whitespace(argv=None):
|
|||
|
||||
return_code = 0
|
||||
for bad_whitespace_file in bad_whitespace_files:
|
||||
print('Fixing {0}'.format(bad_whitespace_file))
|
||||
_, extension = os.path.splitext(bad_whitespace_file.lower())
|
||||
_fix_file(bad_whitespace_file, all_markdown or extension in md_exts)
|
||||
return_code = 1
|
||||
md = all_markdown or extension in md_exts
|
||||
if _fix_file(bad_whitespace_file, md):
|
||||
print('Fixing {}'.format(bad_whitespace_file))
|
||||
return_code = 1
|
||||
return return_code
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,22 @@ def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
|
|||
assert path.read() == expected
|
||||
|
||||
|
||||
def test_ok_with_dos_line_endings(tmpdir):
|
||||
filename = tmpdir.join('f')
|
||||
filename.write_binary(b'foo\r\nbar\r\nbaz\r\n')
|
||||
ret = fix_trailing_whitespace((filename.strpath,))
|
||||
assert filename.read_binary() == b'foo\r\nbar\r\nbaz\r\n'
|
||||
assert ret == 0
|
||||
|
||||
|
||||
def test_markdown_ok(tmpdir):
|
||||
filename = tmpdir.join('foo.md')
|
||||
filename.write_binary(b'foo \n')
|
||||
ret = fix_trailing_whitespace((filename.strpath,))
|
||||
assert filename.read_binary() == b'foo \n'
|
||||
assert ret == 0
|
||||
|
||||
|
||||
# filename, expected input, expected output
|
||||
MD_TESTS_1 = (
|
||||
('foo.md', 'foo \nbar \n ', 'foo \nbar\n\n'),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue