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