Apply typing to all of pre-commit-hooks
diff --git a/tests/check_ast_test.py b/tests/check_ast_test.py index 64916ba..c16f5fc 100644 --- a/tests/check_ast_test.py +++ b/tests/check_ast_test.py
@@ -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
diff --git a/tests/check_builtin_literals_test.py b/tests/check_builtin_literals_test.py index 86b79e3..d4ac30f 100644 --- a/tests/check_builtin_literals_test.py +++ b/tests/check_builtin_literals_test.py
@@ -5,7 +5,35 @@ 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_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
diff --git a/tests/check_json_test.py b/tests/check_json_test.py index 6ba26c1..6654ed1 100644 --- a/tests/check_json_test.py +++ b/tests/check_json_test.py
@@ -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 @@ ('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()
diff --git a/tests/check_merge_conflict_test.py b/tests/check_merge_conflict_test.py index b04c70e..50e389c 100644 --- a/tests/check_merge_conflict_test.py +++ b/tests/check_merge_conflict_test.py
@@ -6,7 +6,7 @@ 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 @@ @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_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_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
diff --git a/tests/check_symlinks_test.py b/tests/check_symlinks_test.py index 0414df5..ecbc7ae 100644 --- a/tests/check_symlinks_test.py +++ b/tests/check_symlinks_test.py
@@ -2,7 +2,7 @@ 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 @@ @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
diff --git a/tests/check_xml_test.py b/tests/check_xml_test.py index 84e365d..357bad6 100644 --- a/tests/check_xml_test.py +++ b/tests/check_xml_test.py
@@ -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 @@ ('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
diff --git a/tests/check_yaml_test.py b/tests/check_yaml_test.py index aa357f1..d267150 100644 --- a/tests/check_yaml_test.py +++ b/tests/check_yaml_test.py
@@ -3,7 +3,7 @@ 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 @@ ('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 @@ ' 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))
diff --git a/tests/detect_private_key_test.py b/tests/detect_private_key_test.py index fdd63a2..9266f2b 100644 --- a/tests/detect_private_key_test.py +++ b/tests/detect_private_key_test.py
@@ -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 @@ @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
diff --git a/tests/end_of_file_fixer_test.py b/tests/end_of_file_fixer_test.py index f8710af..7f644e7 100644 --- a/tests/end_of_file_fixer_test.py +++ b/tests/end_of_file_fixer_test.py
@@ -2,8 +2,8 @@ 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 @@ 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
diff --git a/tests/no_commit_to_branch_test.py b/tests/no_commit_to_branch_test.py index c275bf7..e978ba2 100644 --- a/tests/no_commit_to_branch_test.py +++ b/tests/no_commit_to_branch_test.py
@@ -11,24 +11,24 @@ 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):
diff --git a/tests/pretty_format_json_test.py b/tests/pretty_format_json_test.py index 7ce7e16..8d82d74 100644 --- a/tests/pretty_format_json_test.py +++ b/tests/pretty_format_json_test.py
@@ -3,8 +3,8 @@ 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 @@ ('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 @@ ('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 @@ ('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 @@ ) # 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
diff --git a/tests/requirements_txt_fixer_test.py b/tests/requirements_txt_fixer_test.py index 437cebd..b3a7942 100644 --- a/tests/requirements_txt_fixer_test.py +++ b/tests/requirements_txt_fixer_test.py
@@ -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 @@ 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_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()
diff --git a/tests/sort_simple_yaml_test.py b/tests/sort_simple_yaml_test.py index 176d12f..72f5bec 100644 --- a/tests/sort_simple_yaml_test.py +++ b/tests/sort_simple_yaml_test.py
@@ -110,9 +110,9 @@ 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)
diff --git a/tests/tests_should_end_in_test_test.py b/tests/tests_should_end_in_test_test.py index dc686a5..4eb98e7 100644 --- a/tests/tests_should_end_in_test_test.py +++ b/tests/tests_should_end_in_test_test.py
@@ -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