mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 04:26:52 +00:00
trailing-whitespace hook: restoring original file in case of failure - fixes #134
This commit is contained in:
parent
775a7906cd
commit
bc5e7f2d72
2 changed files with 24 additions and 8 deletions
|
|
@ -9,7 +9,7 @@ from pre_commit_hooks.util import cmd_output
|
||||||
|
|
||||||
|
|
||||||
def _fix_file(filename, markdown=False):
|
def _fix_file(filename, markdown=False):
|
||||||
for line in fileinput.input([filename], inplace=True):
|
for line in fileinput.input([filename], inplace=True, backup='.bak'):
|
||||||
# preserve trailing two-space for non-blank lines in markdown files
|
# preserve trailing two-space for non-blank lines in markdown files
|
||||||
if markdown and (not line.isspace()) and (line.endswith(" \n")):
|
if markdown and (not line.isspace()) and (line.endswith(" \n")):
|
||||||
line = line.rstrip(' \n')
|
line = line.rstrip(' \n')
|
||||||
|
|
@ -64,14 +64,20 @@ def fix_trailing_whitespace(argv=None):
|
||||||
.format(ext)
|
.format(ext)
|
||||||
)
|
)
|
||||||
|
|
||||||
if bad_whitespace_files:
|
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))
|
print('Fixing {0}'.format(bad_whitespace_file))
|
||||||
_, extension = os.path.splitext(bad_whitespace_file.lower())
|
_, extension = os.path.splitext(bad_whitespace_file.lower())
|
||||||
|
try:
|
||||||
_fix_file(bad_whitespace_file, all_markdown or extension in md_exts)
|
_fix_file(bad_whitespace_file, all_markdown or extension in md_exts)
|
||||||
return 1
|
return_code = 1
|
||||||
else:
|
# pylint: disable=broad-except
|
||||||
return 0
|
except Exception as error: # pragma: no cover
|
||||||
|
# e.g. error can be a UnicodeDecodeError in Python 3
|
||||||
|
print('Ignoring {} that caused a {}'.format(bad_whitespace_file, error.__class__))
|
||||||
|
os.remove(bad_whitespace_file)
|
||||||
|
os.rename(bad_whitespace_file + '.bak', bad_whitespace_file)
|
||||||
|
return return_code
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit_hooks.trailing_whitespace_fixer import fix_trailing_whitespace
|
from pre_commit_hooks.trailing_whitespace_fixer import fix_trailing_whitespace
|
||||||
|
|
@ -103,3 +105,11 @@ def test_no_markdown_linebreak_ext_opt(filename, input_s, output, tmpdir):
|
||||||
|
|
||||||
def test_returns_zero_for_no_changes():
|
def test_returns_zero_for_no_changes():
|
||||||
assert fix_trailing_whitespace([__file__]) == 0
|
assert fix_trailing_whitespace([__file__]) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_preserve_non_utf8_file(tmpdir):
|
||||||
|
path = tmpdir.join('file.txt')
|
||||||
|
path.write_binary(b'<a>\xe9 \n</a>')
|
||||||
|
ret = fix_trailing_whitespace([path.strpath])
|
||||||
|
assert ret == (1 if sys.version_info[0] < 3 else 0) # a UnicodeDecodeError is only triggered in Python 3
|
||||||
|
assert path.size() > 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue