mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 04:54:16 +00:00
trailing-whitespace hook: Switching from using fileinput to a tempfile and whitespace substitution in binary mode
This commit is contained in:
parent
bc5e7f2d72
commit
eaad923dd4
2 changed files with 22 additions and 25 deletions
|
|
@ -1,24 +1,29 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import fileinput
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from pre_commit_hooks.util import cmd_output
|
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, backup='.bak'):
|
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
||||||
# preserve trailing two-space for non-blank lines in markdown files
|
with open(filename, 'rb') as original_file:
|
||||||
if markdown and (not line.isspace()) and (line.endswith(" \n")):
|
for line in original_file.readlines():
|
||||||
line = line.rstrip(' \n')
|
# preserve trailing two-space for non-blank lines in markdown files
|
||||||
# only preserve if there are no trailing tabs or unusual whitespace
|
if markdown and (not line.isspace()) and line.endswith(b' \n'):
|
||||||
if not line[-1].isspace():
|
line = line.rstrip(b' \n') # restricted stripping: e.g. \t are not stripped
|
||||||
print(line + " ")
|
# only preserve if there are no trailing tabs or unusual whitespace
|
||||||
continue
|
if not line[-1:].isspace():
|
||||||
|
tmp_file.write(line + b' \n')
|
||||||
print(line.rstrip())
|
else:
|
||||||
|
tmp_file.write(line.rstrip() + b'\n')
|
||||||
|
else:
|
||||||
|
tmp_file.write(line.rstrip() + b'\n')
|
||||||
|
os.remove(filename)
|
||||||
|
os.rename(tmp_file.name, filename)
|
||||||
|
|
||||||
|
|
||||||
def fix_trailing_whitespace(argv=None):
|
def fix_trailing_whitespace(argv=None):
|
||||||
|
|
@ -68,15 +73,8 @@ def fix_trailing_whitespace(argv=None):
|
||||||
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_code = 1
|
||||||
return_code = 1
|
|
||||||
# pylint: disable=broad-except
|
|
||||||
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
|
return return_code
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
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
|
||||||
|
|
@ -108,8 +106,9 @@ def test_returns_zero_for_no_changes():
|
||||||
|
|
||||||
|
|
||||||
def test_preserve_non_utf8_file(tmpdir):
|
def test_preserve_non_utf8_file(tmpdir):
|
||||||
|
non_utf8_bytes_content = b'<a>\xe9 \n</a>\n'
|
||||||
path = tmpdir.join('file.txt')
|
path = tmpdir.join('file.txt')
|
||||||
path.write_binary(b'<a>\xe9 \n</a>')
|
path.write_binary(non_utf8_bytes_content)
|
||||||
ret = fix_trailing_whitespace([path.strpath])
|
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 ret == 1
|
||||||
assert path.size() > 0
|
assert path.size() == (len(non_utf8_bytes_content) - 1)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue