mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-30 02:16:52 +00:00
Merge pull request #207 from dlgallagher/file_contents_sorter_hook_fix_bug_with_files_not_ending_in_newline
Fix bug with the file-contents-sorter hook when processing file that …
This commit is contained in:
commit
e9446ff0cc
3 changed files with 10 additions and 6 deletions
|
|
@ -51,7 +51,7 @@ Add this to your `.pre-commit-config.yaml`
|
|||
with single quoted strings.
|
||||
- `end-of-file-fixer` - Makes sure files end in a newline and only a newline.
|
||||
- `fix-encoding-pragma` - Add `# -*- coding: utf-8 -*-` to the top of python files.
|
||||
- `file-contents-sorter` - Sort the lines in specified files (defaults to alphabetical). You must provide list of target files as input to it.
|
||||
- `file-contents-sorter` - Sort the lines in specified files (defaults to alphabetical). You must provide list of target files as input to it. Note that this hook WILL remove blank lines and does NOT respect any comments.
|
||||
- To remove the coding pragma pass `--remove` (useful in a python3-only codebase)
|
||||
- `flake8` - Run flake8 on your python files.
|
||||
- `forbid-new-submodules` - Prevent addition of new git submodules.
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ FAIL = 1
|
|||
|
||||
|
||||
def sort_file_contents(f):
|
||||
before = tuple(f)
|
||||
after = sorted(before)
|
||||
before = list(f)
|
||||
after = sorted([line.strip(b'\n\r') for line in before if line.strip()])
|
||||
|
||||
before_string = b''.join(before)
|
||||
after_string = b''.join(after)
|
||||
after_string = b'\n'.join(after) + b'\n'
|
||||
|
||||
if before_string == after_string:
|
||||
return PASS
|
||||
|
|
|
|||
|
|
@ -8,15 +8,19 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
|||
@pytest.mark.parametrize(
|
||||
('input_s', 'expected_retval', 'output'),
|
||||
(
|
||||
(b'', PASS, b''),
|
||||
(b'', FAIL, b'\n'),
|
||||
(b'lonesome\n', PASS, b'lonesome\n'),
|
||||
(b'missing_newline', PASS, b'missing_newline'),
|
||||
(b'missing_newline', FAIL, b'missing_newline\n'),
|
||||
(b'newline\nmissing', FAIL, b'missing\nnewline\n'),
|
||||
(b'missing\nnewline', FAIL, b'missing\nnewline\n'),
|
||||
(b'alpha\nbeta\n', PASS, b'alpha\nbeta\n'),
|
||||
(b'beta\nalpha\n', FAIL, b'alpha\nbeta\n'),
|
||||
(b'C\nc\n', PASS, b'C\nc\n'),
|
||||
(b'c\nC\n', FAIL, b'C\nc\n'),
|
||||
(b'mag ical \n tre vor\n', FAIL, b' tre vor\nmag ical \n'),
|
||||
(b'@\n-\n_\n#\n', FAIL, b'#\n-\n@\n_\n'),
|
||||
(b'extra\n\n\nwhitespace\n', FAIL, b'extra\nwhitespace\n'),
|
||||
(b'whitespace\n\n\nextra\n', FAIL, b'extra\nwhitespace\n'),
|
||||
)
|
||||
)
|
||||
def test_integration(input_s, expected_retval, output, tmpdir):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue