Skip to content

Commit 175cd1d

Browse files
committed
feat: console script entrypoint for hook
usage: conventional-pre-commit [-h] [types ...] input
1 parent 50273d1 commit 175cd1d

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

conventional_pre_commit/hook.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ class Colors:
1414
YELLOW = "\033[00;33m"
1515

1616

17-
def main(argv):
18-
parser = argparse.ArgumentParser()
19-
parser.add_argument("types", nargs="*", default=format.DEFAULT_TYPES)
20-
parser.add_argument("input")
21-
args = parser.parse_args(argv)
17+
def main(argv=[]):
18+
parser = argparse.ArgumentParser(
19+
prog="conventional-pre-commit", description="Check a git commit message for Conventional Commits formatting."
20+
)
21+
parser.add_argument("types", type=str, nargs="*", default=format.DEFAULT_TYPES, help="Optional list of types to support")
22+
parser.add_argument("input", type=str, help="A file containing a git commit message")
23+
24+
if len(argv) < 1:
25+
argv = sys.argv[1:]
26+
27+
try:
28+
args = parser.parse_args(argv)
29+
except SystemExit:
30+
return RESULT_FAIL
2231

2332
with open(args.input) as f:
2433
message = f.read()
@@ -53,5 +62,4 @@ def main(argv):
5362

5463

5564
if __name__ == "__main__":
56-
argv = sys.argv[1:]
57-
raise SystemExit(main(argv))
65+
raise SystemExit(main())

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ classifiers =
1717
packages = find:
1818
python_requires = >=3.7
1919

20+
[options.entry_points]
21+
console_scripts =
22+
conventional-pre-commit = conventional_pre_commit.hook:main
23+
2024
[options.packages.find]
2125
exclude =
2226
tests*

tests/test_hook.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
import subprocess
2+
3+
import pytest
4+
15
from conventional_pre_commit.hook import RESULT_SUCCESS, RESULT_FAIL, main
26

37

8+
@pytest.fixture
9+
def cmd():
10+
return "conventional-pre-commit"
11+
12+
413
def test_main_fail__missing_args():
514
result = main()
615

@@ -35,3 +44,39 @@ def test_main_success__custom_conventional(conventional_commit_path):
3544
result = main(["custom", conventional_commit_path])
3645

3746
assert result == RESULT_SUCCESS
47+
48+
49+
def test_subprocess_fail__missing_args(cmd):
50+
result = subprocess.call(cmd)
51+
52+
assert result == RESULT_FAIL
53+
54+
55+
def test_subprocess_fail__bad(cmd, bad_commit_path):
56+
result = subprocess.call((cmd, bad_commit_path))
57+
58+
assert result == RESULT_FAIL
59+
60+
61+
def test_subprocess_fail__custom(cmd, bad_commit_path):
62+
result = subprocess.call((cmd, "custom", bad_commit_path))
63+
64+
assert result == RESULT_FAIL
65+
66+
67+
def test_subprocess_success__conventional(cmd, conventional_commit_path):
68+
result = subprocess.call((cmd, conventional_commit_path))
69+
70+
assert result == RESULT_SUCCESS
71+
72+
73+
def test_subprocess_success__custom(cmd, custom_commit_path):
74+
result = subprocess.call((cmd, "custom", custom_commit_path))
75+
76+
assert result == RESULT_SUCCESS
77+
78+
79+
def test_subprocess_success__custom_conventional(cmd, conventional_commit_path):
80+
result = subprocess.call((cmd, "custom", conventional_commit_path))
81+
82+
assert result == RESULT_SUCCESS

0 commit comments

Comments
 (0)