mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 12:34:17 +00:00
Refactored how entry points work.
This commit is contained in:
parent
5db7f2d807
commit
45741545dc
8 changed files with 71 additions and 42 deletions
|
|
@ -1,28 +1,20 @@
|
||||||
|
|
||||||
-
|
- repo: git@github.com:pre-commit/pre-commit-hooks
|
||||||
repo: git@github.com:pre-commit/pre-commit-hooks
|
|
||||||
sha: 12794c1c19c001e3d05bcfe316b4f93b414035a7
|
sha: 12794c1c19c001e3d05bcfe316b4f93b414035a7
|
||||||
hooks:
|
hooks:
|
||||||
-
|
- id: pyflakes
|
||||||
id: pyflakes
|
|
||||||
files: '\.py$'
|
files: '\.py$'
|
||||||
-
|
- id: debug-statements
|
||||||
id: debug-statements
|
|
||||||
files: '\.py$'
|
files: '\.py$'
|
||||||
-
|
- id: trailing-whitespace
|
||||||
id: trailing-whitespace
|
|
||||||
files: '\.(py|sh|yaml)'
|
files: '\.(py|sh|yaml)'
|
||||||
-
|
- id: name-tests-test
|
||||||
id: name-tests-test
|
|
||||||
files: 'tests/.*\.py'
|
files: 'tests/.*\.py'
|
||||||
|
|
||||||
-
|
- repo: git@github.com:pre-commit/pre-commit
|
||||||
repo: git@github.com:pre-commit/pre-commit
|
|
||||||
sha: c77d65d9cbbcf30e2be005f5ba8b63447deedc1e
|
sha: c77d65d9cbbcf30e2be005f5ba8b63447deedc1e
|
||||||
hooks:
|
hooks:
|
||||||
-
|
- id: validate_manifest
|
||||||
id: validate_manifest
|
|
||||||
files: /manifest.yaml
|
files: /manifest.yaml
|
||||||
-
|
- id: validate_config
|
||||||
id: validate_config
|
|
||||||
files: /\.pre-commit-config.yaml
|
files: /\.pre-commit-config.yaml
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,21 @@
|
||||||
|
|
||||||
-
|
- id: pyflakes
|
||||||
id: pyflakes
|
|
||||||
name: Pyflakes
|
name: Pyflakes
|
||||||
description: This validator runs pyflakes.
|
description: This validator runs pyflakes.
|
||||||
entry: pyflakes
|
entry: pyflakes
|
||||||
language: python
|
language: python
|
||||||
-
|
- id: debug-statements
|
||||||
id: debug-statements
|
|
||||||
name: Debug Statements (Python)
|
name: Debug Statements (Python)
|
||||||
description: This hook checks that debug statements (pdb, ipdb, pudb) are not imported on commit.
|
description: This hook checks that debug statements (pdb, ipdb, pudb) are not imported on commit.
|
||||||
entry: debug-statement-hook
|
entry: debug-statement-hook
|
||||||
language: python
|
language: python
|
||||||
-
|
- id: trailing-whitespace
|
||||||
id: trailing-whitespace
|
|
||||||
name: Trim Trailing Whitespace
|
name: Trim Trailing Whitespace
|
||||||
description: This hook trims trailing whitespace.
|
description: This hook trims trailing whitespace.
|
||||||
entry: trailing-whitespace-fixer
|
entry: trailing-whitespace-fixer
|
||||||
language: python
|
language: python
|
||||||
-
|
- id: name-tests-test
|
||||||
id: name-tests-test
|
|
||||||
name: Tests should end in _test.py
|
name: Tests should end in _test.py
|
||||||
description: This verifies that test files are named correctly
|
description: This verifies that test files are named correctly
|
||||||
entry: name-tests-test
|
entry: name-tests-test
|
||||||
language: python
|
language: python
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import ast
|
||||||
import collections
|
import collections
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from pre_commit_hooks.util import entry
|
||||||
|
|
||||||
|
|
||||||
DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb'])
|
DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb'])
|
||||||
|
|
||||||
|
|
@ -43,6 +45,7 @@ def check_file_for_debug_statements(filename):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
@entry
|
||||||
def debug_statement_hook(argv):
|
def debug_statement_hook(argv):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to run')
|
parser.add_argument('filenames', nargs='*', help='Filenames to run')
|
||||||
|
|
@ -55,9 +58,5 @@ def debug_statement_hook(argv):
|
||||||
return retv
|
return retv
|
||||||
|
|
||||||
|
|
||||||
def entry():
|
|
||||||
return debug_statement_hook(sys.argv[1:])
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(entry())
|
sys.exit(debug_statement_hook())
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,10 @@ from __future__ import print_function
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from pre_commit_hooks.util import entry
|
||||||
|
|
||||||
|
|
||||||
|
@entry
|
||||||
def validate_files(argv):
|
def validate_files(argv):
|
||||||
retcode = 0
|
retcode = 0
|
||||||
for filename in argv:
|
for filename in argv:
|
||||||
|
|
@ -18,9 +21,5 @@ def validate_files(argv):
|
||||||
return retcode
|
return retcode
|
||||||
|
|
||||||
|
|
||||||
def entry():
|
|
||||||
return validate_files(sys.argv[1:])
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(entry())
|
sys.exit(entry())
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,10 @@ import argparse
|
||||||
import sys
|
import sys
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
|
from pre_commit_hooks.util import entry
|
||||||
|
|
||||||
|
|
||||||
|
@entry
|
||||||
def fix_trailing_whitespace(argv):
|
def fix_trailing_whitespace(argv):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||||
|
|
@ -22,9 +25,5 @@ def fix_trailing_whitespace(argv):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def entry():
|
|
||||||
fix_trailing_whitespace(sys.argv[1:])
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(entry())
|
sys.exit(fix_trailing_whitespace())
|
||||||
|
|
|
||||||
16
pre_commit_hooks/util.py
Normal file
16
pre_commit_hooks/util.py
Normal file
|
|
@ -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
|
||||||
6
setup.py
6
setup.py
|
|
@ -13,9 +13,9 @@ setup(
|
||||||
],
|
],
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': [
|
'console_scripts': [
|
||||||
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:entry',
|
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
|
||||||
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:entry',
|
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
|
||||||
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:entry',
|
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
||||||
28
tests/util_test.py
Normal file
28
tests/util_test.py
Normal file
|
|
@ -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:]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue