diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f3b723c..b366d31 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,28 +1,20 @@ -- - repo: git@github.com:pre-commit/pre-commit-hooks +- repo: git@github.com:pre-commit/pre-commit-hooks sha: 12794c1c19c001e3d05bcfe316b4f93b414035a7 hooks: - - - id: pyflakes + - id: pyflakes files: '\.py$' - - - id: debug-statements + - id: debug-statements files: '\.py$' - - - id: trailing-whitespace + - id: trailing-whitespace files: '\.(py|sh|yaml)' - - - id: name-tests-test + - id: name-tests-test files: 'tests/.*\.py' -- - repo: git@github.com:pre-commit/pre-commit +- repo: git@github.com:pre-commit/pre-commit sha: c77d65d9cbbcf30e2be005f5ba8b63447deedc1e hooks: - - - id: validate_manifest + - id: validate_manifest files: /manifest.yaml - - - id: validate_config + - id: validate_config files: /\.pre-commit-config.yaml diff --git a/manifest.yaml b/manifest.yaml index 78fc797..00b48d9 100644 --- a/manifest.yaml +++ b/manifest.yaml @@ -1,25 +1,21 @@ -- - id: pyflakes +- id: pyflakes name: Pyflakes description: This validator runs pyflakes. entry: pyflakes language: python -- - id: debug-statements +- id: debug-statements name: Debug Statements (Python) description: This hook checks that debug statements (pdb, ipdb, pudb) are not imported on commit. entry: debug-statement-hook language: python -- - id: trailing-whitespace +- id: trailing-whitespace name: Trim Trailing Whitespace description: This hook trims trailing whitespace. entry: trailing-whitespace-fixer language: python -- - id: name-tests-test +- id: name-tests-test name: Tests should end in _test.py description: This verifies that test files are named correctly entry: name-tests-test - language: python \ No newline at end of file + language: python diff --git a/pre_commit_hooks/debug_statement_hook.py b/pre_commit_hooks/debug_statement_hook.py index 5f16727..565740e 100644 --- a/pre_commit_hooks/debug_statement_hook.py +++ b/pre_commit_hooks/debug_statement_hook.py @@ -4,6 +4,8 @@ import ast import collections import sys +from pre_commit_hooks.util import entry + DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb']) @@ -43,6 +45,7 @@ def check_file_for_debug_statements(filename): return 0 +@entry def debug_statement_hook(argv): parser = argparse.ArgumentParser() parser.add_argument('filenames', nargs='*', help='Filenames to run') @@ -55,9 +58,5 @@ def debug_statement_hook(argv): return retv -def entry(): - return debug_statement_hook(sys.argv[1:]) - - if __name__ == '__main__': - sys.exit(entry()) \ No newline at end of file + sys.exit(debug_statement_hook()) diff --git a/pre_commit_hooks/tests_should_end_in_test.py b/pre_commit_hooks/tests_should_end_in_test.py index 92f2031..37db03c 100644 --- a/pre_commit_hooks/tests_should_end_in_test.py +++ b/pre_commit_hooks/tests_should_end_in_test.py @@ -3,7 +3,10 @@ from __future__ import print_function import sys +from pre_commit_hooks.util import entry + +@entry def validate_files(argv): retcode = 0 for filename in argv: @@ -18,9 +21,5 @@ def validate_files(argv): return retcode -def entry(): - return validate_files(sys.argv[1:]) - - if __name__ == '__main__': sys.exit(entry()) diff --git a/pre_commit_hooks/trailing_whitespace_fixer.py b/pre_commit_hooks/trailing_whitespace_fixer.py index 5038e6e..bf016af 100644 --- a/pre_commit_hooks/trailing_whitespace_fixer.py +++ b/pre_commit_hooks/trailing_whitespace_fixer.py @@ -3,7 +3,10 @@ import argparse import sys from plumbum import local +from pre_commit_hooks.util import entry + +@entry def fix_trailing_whitespace(argv): parser = argparse.ArgumentParser() parser.add_argument('filenames', nargs='*', help='Filenames to fix') @@ -22,9 +25,5 @@ def fix_trailing_whitespace(argv): return 0 -def entry(): - fix_trailing_whitespace(sys.argv[1:]) - - if __name__ == '__main__': - sys.exit(entry()) + sys.exit(fix_trailing_whitespace()) diff --git a/pre_commit_hooks/util.py b/pre_commit_hooks/util.py new file mode 100644 index 0000000..22d13c4 --- /dev/null +++ b/pre_commit_hooks/util.py @@ -0,0 +1,16 @@ + +import functools +import sys + + +def entry(func): + """Allows a function that has `argv` as an argument to be used as a + commandline entry. This will make the function callable using either + explicitly passed argv or defaulting to sys.argv[1:] + """ + @functools.wraps(func) + def wrapper(argv=None): + if argv is None: + argv = sys.argv[1:] + return func(argv) + return wrapper diff --git a/setup.py b/setup.py index ed41748..42afa6a 100644 --- a/setup.py +++ b/setup.py @@ -13,9 +13,9 @@ setup( ], entry_points={ 'console_scripts': [ - 'debug-statement-hook = pre_commit_hooks.debug_statement_hook:entry', - 'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:entry', - 'name-tests-test = pre_commit_hooks.tests_should_end_in_test:entry', + 'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook', + 'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace', + 'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files', ], }, ) diff --git a/tests/util_test.py b/tests/util_test.py new file mode 100644 index 0000000..72f58c3 --- /dev/null +++ b/tests/util_test.py @@ -0,0 +1,28 @@ + +import mock +import pytest +import sys + +from pre_commit.util import entry + + +@pytest.fixture +def entry_func(): + @entry + def func(argv): + return argv + + return func + + +def test_explicitly_passed_argv_are_passed(entry_func): + input = object() + ret = entry_func(input) + assert ret is input + + +def test_no_arguments_passed_uses_argv(entry_func): + argv = [1, 2, 3, 4] + with mock.patch.object(sys, 'argv', argv): + ret = entry_func() + assert ret == argv[1:]