mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 19:46:54 +00:00
Move FalconSocial pre-commit-python-sorter to offical pre-commit repo, thanks @Dinoshauer @teeberg @amureki @jeffshek @Tenzer @asottile
This commit is contained in:
parent
0ddb3b8527
commit
2ca5460c30
6 changed files with 130 additions and 0 deletions
45
tests/sort_python_imports_test.py
Normal file
45
tests/sort_python_imports_test.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from sys import stdout
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.sort_python_imports import main
|
||||
|
||||
|
||||
def write_file(filename, contents):
|
||||
with open(filename, 'w') as file_obj:
|
||||
file_obj.write(contents)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmpfiles(tmpdir):
|
||||
write_file(tmpdir.join('correct_1.py').strpath, 'import json\nimport sys\n')
|
||||
write_file(tmpdir.join('incorrect_1.py').strpath, 'import sys\n\n\nimport json\n')
|
||||
write_file(tmpdir.join('incorrect_2.py').strpath, 'import sys\n\n\nimport json\n')
|
||||
return tmpdir
|
||||
|
||||
|
||||
def test_sort(tmpfiles):
|
||||
assert main([tmpfiles.join('correct_1.py').strpath]) == 0
|
||||
assert main([tmpfiles.join('incorrect_1.py').strpath]) == 1
|
||||
assert main([tmpfiles.join('incorrect_2.py').strpath, '--check-only']) == 1
|
||||
assert main([tmpfiles.join('incorrect_2.py').strpath, '--silent-overwrite']) == 0
|
||||
|
||||
|
||||
def test_sort_with_diff(tmpfiles):
|
||||
filename = tmpfiles.join('incorrect_1.py').strpath
|
||||
main(['--diff', '--check-only', filename])
|
||||
|
||||
stdout.seek(0)
|
||||
lines = stdout.read().decode("utf-8").splitlines()
|
||||
# Skip diff header
|
||||
lines = "\n".join(lines[4:])
|
||||
assert lines == (
|
||||
'+import json\n'
|
||||
' import sys\n'
|
||||
'-\n'
|
||||
'-\n'
|
||||
'-import json'
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue