Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04b4219

Browse files
committed
Make entry points simpler
1 parent 6d1a464 commit 04b4219

8 files changed

Lines changed: 59 additions & 31 deletions

File tree

pre_commit/clientlib/validate_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pre_commit.constants as C
88
from pre_commit.clientlib.validate_base import get_validator
9+
from pre_commit.util import entry
910

1011

1112
class InvalidConfigError(ValueError): pass
@@ -63,6 +64,7 @@ def validate_config_extra(config):
6364
)
6465

6566

67+
@entry
6668
def run(argv):
6769
parser = argparse.ArgumentParser()
6870
parser.add_argument(
@@ -85,3 +87,7 @@ def run(argv):
8587
return 1
8688

8789
return 0
90+
91+
92+
if __name__ == '__main__':
93+
run()

pre_commit/clientlib/validate_manifest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pre_commit.constants as C
77
from pre_commit.clientlib.validate_base import get_validator
8+
from pre_commit.util import entry
89

910

1011
class InvalidManifestError(ValueError): pass
@@ -52,6 +53,7 @@ def additional_manifest_check(obj):
5253
)
5354

5455

56+
@entry
5557
def run(argv):
5658
parser = argparse.ArgumentParser()
5759
parser.add_argument(
@@ -73,4 +75,8 @@ def run(argv):
7375
print(str(e.args[1]))
7476
return 1
7577

76-
return 0
78+
return 0
79+
80+
81+
if __name__ == '__main__':
82+
run()

pre_commit/entry_points.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

pre_commit/run.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import argparse
33
import os.path
44
import subprocess
5-
import sys
65

76
from pre_commit import git
87
from pre_commit.clientlib.validate_config import validate_config
98
from pre_commit.repository import Repository
9+
from pre_commit.util import entry
1010

1111

1212
RED = '\033[41m'
@@ -90,6 +90,7 @@ def run_single_hook(hook_id, configs=None, run_all_the_things=False):
9090
return 1
9191

9292

93+
@entry
9394
def run(argv):
9495
parser = argparse.ArgumentParser()
9596

@@ -130,4 +131,4 @@ def run(argv):
130131

131132

132133
if __name__ == '__main__':
133-
run(sys.argv[1:])
134+
run()

pre_commit/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import functools
33
import os
4+
import sys
45

56

67
class cached_property(object):
@@ -35,3 +36,16 @@ def wrapper(*args):
3536
wrapper._cache = {}
3637

3738
return wrapper
39+
40+
41+
def entry(func):
42+
"""Allows a function that has `argv` as an argument to be used as a
43+
commandline entry. This will make the function callable using either
44+
explicitly passed argv or defaulting to sys.argv[1:]
45+
"""
46+
@functools.wraps(func)
47+
def wrapper(argv=None):
48+
if argv is None:
49+
argv = sys.argv[1:]
50+
return func(argv)
51+
return wrapper

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
],
2020
entry_points={
2121
'console_scripts': [
22-
'pre-commit = pre_commit.entry_points:pre_commit_func',
23-
'validate-config = pre_commit.entry_points:validate_config_func',
24-
'validate-manifest = pre_commit.entry_points:validate_manifest_func',
22+
'pre-commit = pre_commit.run:run',
23+
'validate-config = pre_commit.clientlib.validate_config:run',
24+
'validate-manifest = pre_commit.clientlib.validate_manifest:run',
2525
],
2626
},
2727
scripts=[

tests/repository_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def test_run_a_hook_lots_of_files(config_for_python_pre_commit_git_repo):
6565
os.environ.get('slowtests', None) == 'false',
6666
reason="TODO: make this test not super slow",
6767
)
68+
@pytest.mark.integration
6869
def test_run_a_node_hook(config_for_node_pre_commit_git_repo):
6970
repo = Repository(config_for_node_pre_commit_git_repo)
7071
repo.install()

tests/util_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

2+
import mock
23
import pytest
34
import random
5+
import sys
46
from plumbum import local
57

68
from pre_commit.util import cached_property
9+
from pre_commit.util import entry
710
from pre_commit.util import memoize_by_cwd
811

912

@@ -59,3 +62,25 @@ def test_memoized_by_cwd_changes_with_different_cwd(memoized_by_cwd):
5962
ret2 = memoized_by_cwd('baz')
6063

6164
assert ret != ret2
65+
66+
67+
@pytest.fixture
68+
def entry_func():
69+
@entry
70+
def func(argv):
71+
return argv
72+
73+
return func
74+
75+
76+
def test_explicitly_passed_argv_are_passed(entry_func):
77+
input = object()
78+
ret = entry_func(input)
79+
assert ret is input
80+
81+
82+
def test_no_arguments_passed_uses_argv(entry_func):
83+
argv = [1, 2, 3, 4]
84+
with mock.patch.object(sys, 'argv', argv):
85+
ret = entry_func()
86+
assert ret == argv[1:]

0 commit comments

Comments
 (0)