mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 03:26:53 +00:00
Added end of file fixer hook.
This commit is contained in:
parent
be8b296a0a
commit
57f1533b84
4 changed files with 118 additions and 0 deletions
42
tests/end_of_file_fixer_test.py
Normal file
42
tests/end_of_file_fixer_test.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
|
||||
import cStringIO
|
||||
import os.path
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.end_of_file_fixer import end_of_file_fixer
|
||||
from pre_commit_hooks.end_of_file_fixer import fix_file
|
||||
|
||||
|
||||
# Input, expected return value, expected output
|
||||
TESTS = (
|
||||
('foo\n', 0, 'foo\n'),
|
||||
('', 0, ''),
|
||||
('\n\n', 1, ''),
|
||||
('\n\n\n\n', 1, ''),
|
||||
('foo', 1, 'foo\n'),
|
||||
('foo\n\n\n', 1, 'foo\n'),
|
||||
('\xe2\x98\x83', 1, '\xe2\x98\x83\n'),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('input', 'expected_retval', 'output'), TESTS)
|
||||
def test_fix_file(input, expected_retval, output):
|
||||
file_obj = cStringIO.StringIO()
|
||||
file_obj.write(input)
|
||||
ret = fix_file(file_obj)
|
||||
assert file_obj.getvalue() == output
|
||||
assert ret == expected_retval
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('input', 'expected_retval', 'output'), TESTS)
|
||||
def test_integration(input, expected_retval, output, tmpdir):
|
||||
file_path = os.path.join(tmpdir.strpath, 'file.txt')
|
||||
|
||||
with open(file_path, 'w') as file_obj:
|
||||
file_obj.write(input)
|
||||
|
||||
ret = end_of_file_fixer([file_path])
|
||||
file_output = open(file_path, 'r').read()
|
||||
|
||||
assert file_output == output
|
||||
assert ret == expected_retval
|
||||
Loading…
Add table
Add a link
Reference in a new issue