Apply typing to all of pre-commit-hooks

This commit is contained in:
Anthony Sottile 2019-01-31 19:19:10 -08:00
parent 63cc3414e9
commit 030bfac7e4
54 changed files with 401 additions and 264 deletions

View file

@ -1,15 +1,15 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from pre_commit_hooks.check_ast import check_ast
from pre_commit_hooks.check_ast import main
from testing.util import get_resource_path
def test_failing_file():
ret = check_ast([get_resource_path('cannot_parse_ast.notpy')])
ret = main([get_resource_path('cannot_parse_ast.notpy')])
assert ret == 1
def test_passing_file():
ret = check_ast([__file__])
ret = main([__file__])
assert ret == 0

View file

@ -5,7 +5,35 @@ import pytest
from pre_commit_hooks.check_builtin_literals import BuiltinTypeCall
from pre_commit_hooks.check_builtin_literals import BuiltinTypeVisitor
from pre_commit_hooks.check_builtin_literals import main
from testing.util import get_resource_path
BUILTIN_CONSTRUCTORS = '''\
from six.moves import builtins
c1 = complex()
d1 = dict()
f1 = float()
i1 = int()
l1 = list()
s1 = str()
t1 = tuple()
c2 = builtins.complex()
d2 = builtins.dict()
f2 = builtins.float()
i2 = builtins.int()
l2 = builtins.list()
s2 = builtins.str()
t2 = builtins.tuple()
'''
BUILTIN_LITERALS = '''\
c1 = 0j
d1 = {}
f1 = 0.0
i1 = 0
l1 = []
s1 = ''
t1 = ()
'''
@pytest.fixture
@ -94,24 +122,26 @@ def test_dict_no_allow_kwargs_exprs(expression, calls):
def test_ignore_constructors():
visitor = BuiltinTypeVisitor(ignore=('complex', 'dict', 'float', 'int', 'list', 'str', 'tuple'))
with open(get_resource_path('builtin_constructors.py'), 'rb') as f:
visitor.visit(ast.parse(f.read(), 'builtin_constructors.py'))
visitor.visit(ast.parse(BUILTIN_CONSTRUCTORS))
assert visitor.builtin_type_calls == []
def test_failing_file():
rc = main([get_resource_path('builtin_constructors.py')])
def test_failing_file(tmpdir):
f = tmpdir.join('f.py')
f.write(BUILTIN_CONSTRUCTORS)
rc = main([f.strpath])
assert rc == 1
def test_passing_file():
rc = main([get_resource_path('builtin_literals.py')])
def test_passing_file(tmpdir):
f = tmpdir.join('f.py')
f.write(BUILTIN_LITERALS)
rc = main([f.strpath])
assert rc == 0
def test_failing_file_ignore_all():
rc = main([
'--ignore=complex,dict,float,int,list,str,tuple',
get_resource_path('builtin_constructors.py'),
])
def test_failing_file_ignore_all(tmpdir):
f = tmpdir.join('f.py')
f.write(BUILTIN_CONSTRUCTORS)
rc = main(['--ignore=complex,dict,float,int,list,str,tuple', f.strpath])
assert rc == 0

View file

@ -1,6 +1,6 @@
import pytest
from pre_commit_hooks.check_json import check_json
from pre_commit_hooks.check_json import main
from testing.util import get_resource_path
@ -11,8 +11,8 @@ from testing.util import get_resource_path
('ok_json.json', 0),
),
)
def test_check_json(capsys, filename, expected_retval):
ret = check_json([get_resource_path(filename)])
def test_main(capsys, filename, expected_retval):
ret = main([get_resource_path(filename)])
assert ret == expected_retval
if expected_retval == 1:
stdout, _ = capsys.readouterr()

View file

@ -6,7 +6,7 @@ import shutil
import pytest
from pre_commit_hooks.check_merge_conflict import detect_merge_conflict
from pre_commit_hooks.check_merge_conflict import main
from pre_commit_hooks.util import cmd_output
from testing.util import get_resource_path
@ -102,7 +102,7 @@ def repository_pending_merge(tmpdir):
@pytest.mark.usefixtures('f1_is_a_conflict_file')
def test_merge_conflicts_git():
assert detect_merge_conflict(['f1']) == 1
assert main(['f1']) == 1
@pytest.mark.parametrize(
@ -110,7 +110,7 @@ def test_merge_conflicts_git():
)
def test_merge_conflicts_failing(contents, repository_pending_merge):
repository_pending_merge.join('f2').write_binary(contents)
assert detect_merge_conflict(['f2']) == 1
assert main(['f2']) == 1
@pytest.mark.parametrize(
@ -118,22 +118,22 @@ def test_merge_conflicts_failing(contents, repository_pending_merge):
)
def test_merge_conflicts_ok(contents, f1_is_a_conflict_file):
f1_is_a_conflict_file.join('f1').write_binary(contents)
assert detect_merge_conflict(['f1']) == 0
assert main(['f1']) == 0
@pytest.mark.usefixtures('f1_is_a_conflict_file')
def test_ignores_binary_files():
shutil.copy(get_resource_path('img1.jpg'), 'f1')
assert detect_merge_conflict(['f1']) == 0
assert main(['f1']) == 0
def test_does_not_care_when_not_in_a_merge(tmpdir):
f = tmpdir.join('README.md')
f.write_binary(b'problem\n=======\n')
assert detect_merge_conflict([str(f.realpath())]) == 0
assert main([str(f.realpath())]) == 0
def test_care_when_assumed_merge(tmpdir):
f = tmpdir.join('README.md')
f.write_binary(b'problem\n=======\n')
assert detect_merge_conflict([str(f.realpath()), '--assume-in-merge']) == 1
assert main([str(f.realpath()), '--assume-in-merge']) == 1

View file

@ -2,7 +2,7 @@ import os
import pytest
from pre_commit_hooks.check_symlinks import check_symlinks
from pre_commit_hooks.check_symlinks import main
xfail_symlink = pytest.mark.xfail(os.name == 'nt', reason='No symlink support')
@ -12,12 +12,12 @@ xfail_symlink = pytest.mark.xfail(os.name == 'nt', reason='No symlink support')
@pytest.mark.parametrize(
('dest', 'expected'), (('exists', 0), ('does-not-exist', 1)),
)
def test_check_symlinks(tmpdir, dest, expected): # pragma: no cover (symlinks)
def test_main(tmpdir, dest, expected): # pragma: no cover (symlinks)
tmpdir.join('exists').ensure()
symlink = tmpdir.join('symlink')
symlink.mksymlinkto(tmpdir.join(dest))
assert check_symlinks((symlink.strpath,)) == expected
assert main((symlink.strpath,)) == expected
def test_check_symlinks_normal_file(tmpdir):
assert check_symlinks((tmpdir.join('f').ensure().strpath,)) == 0
def test_main_normal_file(tmpdir):
assert main((tmpdir.join('f').ensure().strpath,)) == 0

View file

@ -1,6 +1,6 @@
import pytest
from pre_commit_hooks.check_xml import check_xml
from pre_commit_hooks.check_xml import main
from testing.util import get_resource_path
@ -10,6 +10,6 @@ from testing.util import get_resource_path
('ok_xml.xml', 0),
),
)
def test_check_xml(filename, expected_retval):
ret = check_xml([get_resource_path(filename)])
def test_main(filename, expected_retval):
ret = main([get_resource_path(filename)])
assert ret == expected_retval

View file

@ -3,7 +3,7 @@ from __future__ import unicode_literals
import pytest
from pre_commit_hooks.check_yaml import check_yaml
from pre_commit_hooks.check_yaml import main
from testing.util import get_resource_path
@ -13,29 +13,29 @@ from testing.util import get_resource_path
('ok_yaml.yaml', 0),
),
)
def test_check_yaml(filename, expected_retval):
ret = check_yaml([get_resource_path(filename)])
def test_main(filename, expected_retval):
ret = main([get_resource_path(filename)])
assert ret == expected_retval
def test_check_yaml_allow_multiple_documents(tmpdir):
def test_main_allow_multiple_documents(tmpdir):
f = tmpdir.join('test.yaml')
f.write('---\nfoo\n---\nbar\n')
# should fail without the setting
assert check_yaml((f.strpath,))
assert main((f.strpath,))
# should pass when we allow multiple documents
assert not check_yaml(('--allow-multiple-documents', f.strpath))
assert not main(('--allow-multiple-documents', f.strpath))
def test_fails_even_with_allow_multiple_documents(tmpdir):
f = tmpdir.join('test.yaml')
f.write('[')
assert check_yaml(('--allow-multiple-documents', f.strpath))
assert main(('--allow-multiple-documents', f.strpath))
def test_check_yaml_unsafe(tmpdir):
def test_main_unsafe(tmpdir):
f = tmpdir.join('test.yaml')
f.write(
'some_foo: !vault |\n'
@ -43,12 +43,12 @@ def test_check_yaml_unsafe(tmpdir):
' deadbeefdeadbeefdeadbeef\n',
)
# should fail "safe" check
assert check_yaml((f.strpath,))
assert main((f.strpath,))
# should pass when we allow unsafe documents
assert not check_yaml(('--unsafe', f.strpath))
assert not main(('--unsafe', f.strpath))
def test_check_yaml_unsafe_still_fails_on_syntax_errors(tmpdir):
def test_main_unsafe_still_fails_on_syntax_errors(tmpdir):
f = tmpdir.join('test.yaml')
f.write('[')
assert check_yaml(('--unsafe', f.strpath))
assert main(('--unsafe', f.strpath))

View file

@ -1,6 +1,6 @@
import pytest
from pre_commit_hooks.detect_private_key import detect_private_key
from pre_commit_hooks.detect_private_key import main
# Input, expected return value
TESTS = (
@ -18,7 +18,7 @@ TESTS = (
@pytest.mark.parametrize(('input_s', 'expected_retval'), TESTS)
def test_detect_private_key(input_s, expected_retval, tmpdir):
def test_main(input_s, expected_retval, tmpdir):
path = tmpdir.join('file.txt')
path.write_binary(input_s)
assert detect_private_key([path.strpath]) == expected_retval
assert main([path.strpath]) == expected_retval

View file

@ -2,8 +2,8 @@ import io
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
from pre_commit_hooks.end_of_file_fixer import main
# Input, expected return value, expected output
@ -35,7 +35,7 @@ def test_integration(input_s, expected_retval, output, tmpdir):
path = tmpdir.join('file.txt')
path.write_binary(input_s)
ret = end_of_file_fixer([path.strpath])
ret = main([path.strpath])
file_output = path.read_binary()
assert file_output == output

View file

@ -11,24 +11,24 @@ from pre_commit_hooks.util import cmd_output
def test_other_branch(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'anotherbranch')
assert is_on_branch(('master',)) is False
assert is_on_branch({'master'}) is False
def test_multi_branch(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'another/branch')
assert is_on_branch(('master',)) is False
assert is_on_branch({'master'}) is False
def test_multi_branch_fail(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'another/branch')
assert is_on_branch(('another/branch',)) is True
assert is_on_branch({'another/branch'}) is True
def test_master_branch(temp_git_dir):
with temp_git_dir.as_cwd():
assert is_on_branch(('master',)) is True
assert is_on_branch({'master'}) is True
def test_main_branch_call(temp_git_dir):

View file

@ -3,8 +3,8 @@ import shutil
import pytest
from six import PY2
from pre_commit_hooks.pretty_format_json import main
from pre_commit_hooks.pretty_format_json import parse_num_to_int
from pre_commit_hooks.pretty_format_json import pretty_format_json
from testing.util import get_resource_path
@ -23,8 +23,8 @@ def test_parse_num_to_int():
('pretty_formatted_json.json', 0),
),
)
def test_pretty_format_json(filename, expected_retval):
ret = pretty_format_json([get_resource_path(filename)])
def test_main(filename, expected_retval):
ret = main([get_resource_path(filename)])
assert ret == expected_retval
@ -36,8 +36,8 @@ def test_pretty_format_json(filename, expected_retval):
('pretty_formatted_json.json', 0),
),
)
def test_unsorted_pretty_format_json(filename, expected_retval):
ret = pretty_format_json(['--no-sort-keys', get_resource_path(filename)])
def test_unsorted_main(filename, expected_retval):
ret = main(['--no-sort-keys', get_resource_path(filename)])
assert ret == expected_retval
@ -51,17 +51,17 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
('tab_pretty_formatted_json.json', 0),
),
)
def test_tab_pretty_format_json(filename, expected_retval): # pragma: no cover
ret = pretty_format_json(['--indent', '\t', get_resource_path(filename)])
def test_tab_main(filename, expected_retval): # pragma: no cover
ret = main(['--indent', '\t', get_resource_path(filename)])
assert ret == expected_retval
def test_non_ascii_pretty_format_json():
ret = pretty_format_json(['--no-ensure-ascii', get_resource_path('non_ascii_pretty_formatted_json.json')])
def test_non_ascii_main():
ret = main(['--no-ensure-ascii', get_resource_path('non_ascii_pretty_formatted_json.json')])
assert ret == 0
def test_autofix_pretty_format_json(tmpdir):
def test_autofix_main(tmpdir):
srcfile = tmpdir.join('to_be_json_formatted.json')
shutil.copyfile(
get_resource_path('not_pretty_formatted_json.json'),
@ -69,30 +69,30 @@ def test_autofix_pretty_format_json(tmpdir):
)
# now launch the autofix on that file
ret = pretty_format_json(['--autofix', srcfile.strpath])
ret = main(['--autofix', srcfile.strpath])
# it should have formatted it
assert ret == 1
# file was formatted (shouldn't trigger linter again)
ret = pretty_format_json([srcfile.strpath])
ret = main([srcfile.strpath])
assert ret == 0
def test_orderfile_get_pretty_format():
ret = pretty_format_json(['--top-keys=alist', get_resource_path('pretty_formatted_json.json')])
ret = main(['--top-keys=alist', get_resource_path('pretty_formatted_json.json')])
assert ret == 0
def test_not_orderfile_get_pretty_format():
ret = pretty_format_json(['--top-keys=blah', get_resource_path('pretty_formatted_json.json')])
ret = main(['--top-keys=blah', get_resource_path('pretty_formatted_json.json')])
assert ret == 1
def test_top_sorted_get_pretty_format():
ret = pretty_format_json(['--top-keys=01-alist,alist', get_resource_path('top_sorted_json.json')])
ret = main(['--top-keys=01-alist,alist', get_resource_path('top_sorted_json.json')])
assert ret == 0
def test_badfile_pretty_format_json():
ret = pretty_format_json([get_resource_path('ok_yaml.yaml')])
def test_badfile_main():
ret = main([get_resource_path('ok_yaml.yaml')])
assert ret == 1

View file

@ -1,7 +1,7 @@
import pytest
from pre_commit_hooks.requirements_txt_fixer import FAIL
from pre_commit_hooks.requirements_txt_fixer import fix_requirements_txt
from pre_commit_hooks.requirements_txt_fixer import main
from pre_commit_hooks.requirements_txt_fixer import PASS
from pre_commit_hooks.requirements_txt_fixer import Requirement
@ -36,7 +36,7 @@ def test_integration(input_s, expected_retval, output, tmpdir):
path = tmpdir.join('file.txt')
path.write_binary(input_s)
output_retval = fix_requirements_txt([path.strpath])
output_retval = main([path.strpath])
assert path.read_binary() == output
assert output_retval == expected_retval
@ -44,7 +44,7 @@ def test_integration(input_s, expected_retval, output, tmpdir):
def test_requirement_object():
top_of_file = Requirement()
top_of_file.comments.append('#foo')
top_of_file.comments.append(b'#foo')
top_of_file.value = b'\n'
requirement_foo = Requirement()

View file

@ -110,9 +110,9 @@ def test_first_key():
lines = ['# some comment', '"a": 42', 'b: 17', '', 'c: 19']
assert first_key(lines) == 'a": 42'
# no lines
# no lines (not a real situation)
lines = []
assert first_key(lines) is None
assert first_key(lines) == ''
@pytest.mark.parametrize('bad_lines,good_lines,_', TEST_SORTS)

View file

@ -1,36 +1,36 @@
from pre_commit_hooks.tests_should_end_in_test import validate_files
from pre_commit_hooks.tests_should_end_in_test import main
def test_validate_files_all_pass():
ret = validate_files(['foo_test.py', 'bar_test.py'])
def test_main_all_pass():
ret = main(['foo_test.py', 'bar_test.py'])
assert ret == 0
def test_validate_files_one_fails():
ret = validate_files(['not_test_ending.py', 'foo_test.py'])
def test_main_one_fails():
ret = main(['not_test_ending.py', 'foo_test.py'])
assert ret == 1
def test_validate_files_django_all_pass():
ret = validate_files(['--django', 'tests.py', 'test_foo.py', 'test_bar.py', 'tests/test_baz.py'])
def test_main_django_all_pass():
ret = main(['--django', 'tests.py', 'test_foo.py', 'test_bar.py', 'tests/test_baz.py'])
assert ret == 0
def test_validate_files_django_one_fails():
ret = validate_files(['--django', 'not_test_ending.py', 'test_foo.py'])
def test_main_django_one_fails():
ret = main(['--django', 'not_test_ending.py', 'test_foo.py'])
assert ret == 1
def test_validate_nested_files_django_one_fails():
ret = validate_files(['--django', 'tests/not_test_ending.py', 'test_foo.py'])
ret = main(['--django', 'tests/not_test_ending.py', 'test_foo.py'])
assert ret == 1
def test_validate_files_not_django_fails():
ret = validate_files(['foo_test.py', 'bar_test.py', 'test_baz.py'])
def test_main_not_django_fails():
ret = main(['foo_test.py', 'bar_test.py', 'test_baz.py'])
assert ret == 1
def test_validate_files_django_fails():
ret = validate_files(['--django', 'foo_test.py', 'test_bar.py', 'test_baz.py'])
def test_main_django_fails():
ret = main(['--django', 'foo_test.py', 'test_bar.py', 'test_baz.py'])
assert ret == 1