Repository: pre-commit/pre-commit-hooks

Issue:
DOS ending files fail, claim they are rewritten, but don't change file contents (trailing-whitespace-fixer)

Discussion:

Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index f261e54..a3bb7a4 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -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
diff --git a/pre_commit_hooks/trailing_whitespace_fixer.py b/pre_commit_hooks/trailing_whitespace_fixer.py
index fa9b7dd..d23d58d 100644
--- a/pre_commit_hooks/trailing_whitespace_fixer.py
+++ b/pre_commit_hooks/trailing_whitespace_fixer.py
@@ -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
 
 


Test patch:
diff --git a/tests/trailing_whitespace_fixer_test.py b/tests/trailing_whitespace_fixer_test.py
index 3a72ccb..eb2a1d0 100644
--- a/tests/trailing_whitespace_fixer_test.py
+++ b/tests/trailing_whitespace_fixer_test.py
@@ -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'),