mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-26 01:53:21 +08:00
Remove @entry decorator (and misc cleanup)
This commit is contained in:
@ -4,11 +4,8 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
import simplejson
|
import simplejson
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
|
||||||
|
|
||||||
|
def check_json(argv=None):
|
||||||
@entry
|
|
||||||
def check_json(argv):
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='JSON filenames to check.')
|
parser.add_argument('filenames', nargs='*', help='JSON filenames to check.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
@ -17,8 +14,8 @@ def check_json(argv):
|
|||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
try:
|
try:
|
||||||
simplejson.load(open(filename))
|
simplejson.load(open(filename))
|
||||||
except simplejson.JSONDecodeError as e:
|
except simplejson.JSONDecodeError as exc:
|
||||||
print('{0}: Failed to json encode ({1})'.format(filename, e))
|
print('{0}: Failed to json encode ({1})'.format(filename, exc))
|
||||||
retval = 1
|
retval = 1
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
|
@ -4,11 +4,8 @@ import argparse
|
|||||||
import sys
|
import sys
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
|
||||||
|
|
||||||
|
def check_yaml(argv=None):
|
||||||
@entry
|
|
||||||
def check_yaml(argv):
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('filenames', nargs='*', help='Yaml filenames to check.')
|
parser.add_argument('filenames', nargs='*', help='Yaml filenames to check.')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
@ -17,8 +14,8 @@ def check_yaml(argv):
|
|||||||
for filename in args.filenames:
|
for filename in args.filenames:
|
||||||
try:
|
try:
|
||||||
yaml.load(open(filename))
|
yaml.load(open(filename))
|
||||||
except yaml.YAMLError as e:
|
except yaml.YAMLError as exc:
|
||||||
print(e)
|
print(exc)
|
||||||
retval = 1
|
retval = 1
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
|
@ -6,8 +6,6 @@ import ast
|
|||||||
import collections
|
import collections
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
|
||||||
|
|
||||||
|
|
||||||
DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb'])
|
DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb'])
|
||||||
|
|
||||||
@ -61,8 +59,7 @@ def check_file_for_debug_statements(filename):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@entry
|
def debug_statement_hook(argv=None):
|
||||||
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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
@ -5,8 +5,6 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
|
||||||
|
|
||||||
|
|
||||||
def fix_file(file_obj):
|
def fix_file(file_obj):
|
||||||
# Test for newline at end of file
|
# Test for newline at end of file
|
||||||
@ -46,8 +44,7 @@ def fix_file(file_obj):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@entry
|
def end_of_file_fixer(argv=None):
|
||||||
def end_of_file_fixer(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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
@ -2,8 +2,6 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
|
||||||
|
|
||||||
|
|
||||||
class Requirement(object):
|
class Requirement(object):
|
||||||
|
|
||||||
@ -67,8 +65,7 @@ def fix_requirements(f):
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
@entry
|
def fix_requirements_txt(argv=None):
|
||||||
def fix_requirements_txt(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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
@ -76,7 +73,7 @@ def fix_requirements_txt(argv):
|
|||||||
retv = 0
|
retv = 0
|
||||||
|
|
||||||
for arg in args.filenames:
|
for arg in args.filenames:
|
||||||
with open(arg, 'rb+') as f:
|
with open(arg, 'rb+') as file_obj:
|
||||||
retv |= fix_requirements(f)
|
retv |= fix_requirements(file_obj)
|
||||||
|
|
||||||
return retv
|
return retv
|
||||||
|
@ -2,11 +2,8 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
|
||||||
|
|
||||||
|
def validate_files(argv=None):
|
||||||
@entry
|
|
||||||
def validate_files(argv):
|
|
||||||
retcode = 0
|
retcode = 0
|
||||||
for filename in argv:
|
for filename in argv:
|
||||||
if (
|
if (
|
||||||
|
@ -5,16 +5,13 @@ import fileinput
|
|||||||
import sys
|
import sys
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
|
|
||||||
from pre_commit_hooks.util import entry
|
|
||||||
|
|
||||||
|
|
||||||
def _fix_file(filename):
|
def _fix_file(filename):
|
||||||
for line in fileinput.input([filename], inplace=True):
|
for line in fileinput.input([filename], inplace=True):
|
||||||
print(line.rstrip())
|
print(line.rstrip())
|
||||||
|
|
||||||
|
|
||||||
@entry
|
def fix_trailing_whitespace(argv=None):
|
||||||
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')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
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
|
|
2
pylintrc
2
pylintrc
@ -1,5 +1,5 @@
|
|||||||
[MESSAGES CONTROL]
|
[MESSAGES CONTROL]
|
||||||
disable=missing-docstring,abstract-method,redefined-builtin,invalid-name,no-value-for-parameter,redefined-outer-name,no-member,bad-open-mode
|
disable=bad-open-mode,invalid-name,missing-docstring,redefined-outer-name
|
||||||
|
|
||||||
[REPORTS]
|
[REPORTS]
|
||||||
output-format=colorized
|
output-format=colorized
|
||||||
|
@ -18,21 +18,21 @@ TESTS = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input', 'expected_retval', 'output'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
||||||
def test_fix_file(input, expected_retval, output):
|
def test_fix_file(input_s, expected_retval, output):
|
||||||
file_obj = io.BytesIO()
|
file_obj = io.BytesIO()
|
||||||
file_obj.write(input)
|
file_obj.write(input_s)
|
||||||
ret = fix_file(file_obj)
|
ret = fix_file(file_obj)
|
||||||
assert file_obj.getvalue() == output
|
assert file_obj.getvalue() == output
|
||||||
assert ret == expected_retval
|
assert ret == expected_retval
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input', 'expected_retval', 'output'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
||||||
def test_integration(input, expected_retval, output, tmpdir):
|
def test_integration(input_s, expected_retval, output, tmpdir):
|
||||||
file_path = os.path.join(tmpdir.strpath, 'file.txt')
|
file_path = os.path.join(tmpdir.strpath, 'file.txt')
|
||||||
|
|
||||||
with open(file_path, 'wb') as file_obj:
|
with open(file_path, 'wb') as file_obj:
|
||||||
file_obj.write(input)
|
file_obj.write(input_s)
|
||||||
|
|
||||||
ret = end_of_file_fixer([file_path])
|
ret = end_of_file_fixer([file_path])
|
||||||
file_output = open(file_path, 'rb').read()
|
file_output = open(file_path, 'rb').read()
|
||||||
|
@ -17,12 +17,12 @@ TESTS = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('input', 'expected_retval', 'output'), TESTS)
|
@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), TESTS)
|
||||||
def test_integration(input, expected_retval, output, tmpdir):
|
def test_integration(input_s, expected_retval, output, tmpdir):
|
||||||
path = os.path.join(tmpdir.strpath, 'file.txt')
|
path = os.path.join(tmpdir.strpath, 'file.txt')
|
||||||
|
|
||||||
with open(path, 'wb') as file_obj:
|
with open(path, 'wb') as file_obj:
|
||||||
file_obj.write(input)
|
file_obj.write(input_s)
|
||||||
|
|
||||||
assert fix_requirements_txt([path]) == expected_retval
|
assert fix_requirements_txt([path]) == expected_retval
|
||||||
assert open(path, 'rb').read() == output
|
assert open(path, 'rb').read() == output
|
||||||
|
@ -9,8 +9,8 @@ def test_fixes_trailing_whitespace(tmpdir):
|
|||||||
('foo.py', 'foo \nbar \n'),
|
('foo.py', 'foo \nbar \n'),
|
||||||
('bar.py', 'bar\t\nbaz\t\n'),
|
('bar.py', 'bar\t\nbaz\t\n'),
|
||||||
):
|
):
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as file_obj:
|
||||||
f.write(contents) # pragma: no cover (python 2.6 coverage bug)
|
file_obj.write(contents) # pragma: no cover (26 coverage bug)
|
||||||
|
|
||||||
ret = fix_trailing_whitespace(['foo.py', 'bar.py'])
|
ret = fix_trailing_whitespace(['foo.py', 'bar.py'])
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
import mock
|
|
||||||
import pytest
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from pre_commit_hooks.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:]
|
|
Reference in New Issue
Block a user