Skip to content

Commit 367e740

Browse files
committed
feat(hook): arg to turn off color in output
1 parent 6d66cf1 commit 367e740

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

conventional_pre_commit/hook.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def main(argv=[]):
1313
)
1414
parser.add_argument("types", type=str, nargs="*", default=format.DEFAULT_TYPES, help="Optional list of types to support")
1515
parser.add_argument("input", type=str, help="A file containing a git commit message")
16+
parser.add_argument("--no-color", action="store_false", default=True, dest="color", help="Disable color in output.")
1617
parser.add_argument(
1718
"--force-scope", action="store_false", default=True, dest="optional_scope", help="Force commit to have scope defined."
1819
)
@@ -47,7 +48,7 @@ def main(argv=[]):
4748
with open(args.input, encoding="utf-8") as f:
4849
commit_msg = f.read()
4950
except UnicodeDecodeError:
50-
print(output.unicode_decode_error())
51+
print(output.unicode_decode_error(args.color))
5152
return RESULT_FAIL
5253
if args.scopes:
5354
scopes = args.scopes.split(",")
@@ -61,12 +62,16 @@ def main(argv=[]):
6162
if format.is_conventional(commit_msg, args.types, args.optional_scope, scopes):
6263
return RESULT_SUCCESS
6364

64-
print(output.fail(commit_msg))
65+
print(output.fail(commit_msg, use_color=args.color))
6566

6667
if not args.verbose:
67-
print(output.verbose_arg())
68+
print(output.verbose_arg(use_color=args.color))
6869
else:
69-
print(output.fail_verbose(commit_msg, args.types, args.optional_scope, scopes))
70+
print(
71+
output.fail_verbose(
72+
commit_msg, types=args.types, optional_scope=args.optional_scope, scopes=scopes, use_color=args.color
73+
)
74+
)
7075

7176
return RESULT_FAIL
7277

tests/test_hook.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from conventional_pre_commit.hook import RESULT_FAIL, RESULT_SUCCESS, main
7+
from conventional_pre_commit.output import Colors
78

89

910
@pytest.fixture
@@ -103,16 +104,30 @@ def test_main_fail__verbose(bad_commit_path, capsys):
103104
captured = capsys.readouterr()
104105
output = captured.out
105106

107+
assert Colors.LBLUE in output
108+
assert Colors.LRED in output
109+
assert Colors.RESTORE in output
110+
assert Colors.YELLOW in output
106111
assert "Conventional Commit messages follow a pattern like" in output
107112
assert f"type(scope): subject{os.linesep}{os.linesep} extended body" in output
108-
assert "Expected value for 'type' but found none." in output
109-
assert "Expected value for 'delim' but found none." in output
110-
assert "Expected value for 'scope' but found none." in output
111-
assert "Expected value for 'subject' but found none." in output
112113
assert "git commit --edit --file=.git/COMMIT_EDITMSG" in output
113114
assert "edit the commit message and retry the commit" in output
114115

115116

117+
def test_main_fail__no_color(bad_commit_path, capsys):
118+
result = main(["--verbose", "--no-color", bad_commit_path])
119+
120+
assert result == RESULT_FAIL
121+
122+
captured = capsys.readouterr()
123+
output = captured.out
124+
125+
assert Colors.LBLUE not in output
126+
assert Colors.LRED not in output
127+
assert Colors.RESTORE not in output
128+
assert Colors.YELLOW not in output
129+
130+
116131
def test_subprocess_fail__missing_args(cmd):
117132
result = subprocess.call(cmd)
118133

0 commit comments

Comments
 (0)