Skip to content

Commit 5833606

Browse files
committed
feat(cli): added the quiet option in cli
1 parent 09d4189 commit 5833606

File tree

2 files changed

+102
-19
lines changed

2 files changed

+102
-19
lines changed

src/commitlint/cli.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ def get_args() -> argparse.Namespace:
6464
action="store_true",
6565
help="Skip the detailed error message check",
6666
)
67-
67+
# --quiet option is optional
68+
parser.add_argument(
69+
"--quiet",
70+
action="store_true",
71+
help="Ignore stdout and stderr",
72+
default=False,
73+
)
6874
# parsing args
6975
args = parser.parse_args()
7076

@@ -119,50 +125,59 @@ def _get_commit_message_from_file(filepath: str) -> str:
119125
return commit_message
120126

121127

122-
def _handle_commit_message(commit_message: str, skip_detail: bool) -> None:
128+
def _handle_commit_message(
129+
commit_message: str, skip_detail: bool, quiet: bool = False
130+
) -> None:
123131
"""
124132
Handles a single commit message, checks its validity, and prints the result.
125133
126134
Args:
127135
commit_message (str): The commit message to be handled.
128136
skip_detail (bool): Whether to skip the detailed error linting.
137+
quiet (bool): Whether to print the detailed errors
129138
130139
Raises:
131140
SystemExit: If the commit message is invalid.
132141
"""
133142
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
134143

135-
if success:
136-
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
137-
else:
138-
_show_errors(commit_message, errors, skip_detail=skip_detail)
144+
if quiet and errors:
139145
sys.exit(1)
140146

147+
elif not quiet:
148+
if success:
149+
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
150+
else:
151+
_show_errors(commit_message, errors, skip_detail=skip_detail)
152+
sys.exit(1)
153+
141154

142155
def _handle_multiple_commit_messages(
143-
commit_messages: List[str], skip_detail: bool
156+
commit_messages: List[str], skip_detail: bool, quiet: bool = False
144157
) -> None:
145158
"""
146159
Handles multiple commit messages, checks their validity, and prints the result.
147160
148161
Args:
149162
commit_messages (List[str]): List of commit messages to be handled.
150163
skip_detail (bool): Whether to skip the detailed error linting.
151-
164+
quiet (bool): Whether to show the error and messages in console
152165
Raises:
153166
SystemExit: If any of the commit messages is invalid.
154167
"""
168+
# if not quiet:
155169
has_error = False
156170
for commit_message in commit_messages:
157171
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
158-
if not success:
172+
if not success and not quiet:
159173
has_error = True
160174
_show_errors(commit_message, errors, skip_detail=skip_detail)
161175
sys.stderr.write("\n")
162176

163177
if has_error:
164178
sys.exit(1)
165-
else:
179+
180+
elif not has_error and not quiet:
166181
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
167182

168183

@@ -175,20 +190,26 @@ def main() -> None:
175190
try:
176191
if args.file:
177192
commit_message = _get_commit_message_from_file(args.file)
178-
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
193+
_handle_commit_message(
194+
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
195+
)
179196
elif args.hash:
180197
commit_message = get_commit_message_of_hash(args.hash)
181-
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
198+
_handle_commit_message(
199+
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
200+
)
182201
elif args.from_hash:
183202
commit_messages = get_commit_messages_of_hash_range(
184203
args.from_hash, args.to_hash
185204
)
186205
_handle_multiple_commit_messages(
187-
commit_messages, skip_detail=args.skip_detail
206+
commit_messages, skip_detail=args.skip_detail, quiet=args.quiet
188207
)
189208
else:
190209
commit_message = args.commit_message.strip()
191-
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
210+
_handle_commit_message(
211+
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
212+
)
192213
except CommitlintException as ex:
193214
sys.stderr.write(f"{ex}\n")
194215
sys.exit(1)

tests/test_cli.py

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class TestCLIGetArgs:
1818
@patch(
1919
"argparse.ArgumentParser.parse_args",
2020
return_value=MagicMock(
21-
commit_message="commit message", file=None, hash=None, from_hash=None
21+
commit_message="commit message",
22+
file=None,
23+
hash=None,
24+
from_hash=None,
25+
quiet=None,
2226
),
2327
)
2428
def test__get_args__with_commit_message(self, *_):
@@ -27,6 +31,7 @@ def test__get_args__with_commit_message(self, *_):
2731
assert args.file is None
2832
assert args.hash is None
2933
assert args.from_hash is None
34+
assert args.quiet is None
3035

3136
@patch(
3237
"argparse.ArgumentParser.parse_args",
@@ -88,6 +93,7 @@ class TestCLIMain:
8893
hash=None,
8994
from_hash=None,
9095
skip_detail=False,
96+
quiet=False,
9197
),
9298
)
9399
@patch("sys.stdout.write")
@@ -107,6 +113,7 @@ def test__main__valid_commit_message(
107113
hash=None,
108114
from_hash=None,
109115
skip_detail=True,
116+
quiet=False,
110117
),
111118
)
112119
@patch("sys.stdout.write")
@@ -126,6 +133,7 @@ def test__main__valid_commit_message_using_skip_detail(
126133
hash=None,
127134
from_hash=None,
128135
skip_detail=False,
136+
quiet=False,
129137
),
130138
)
131139
@patch("sys.stderr.write")
@@ -154,6 +162,7 @@ def test__main__invalid_commit_message(
154162
hash=None,
155163
from_hash=None,
156164
skip_detail=True,
165+
quiet=False,
157166
),
158167
)
159168
@patch("sys.stderr.write")
@@ -177,7 +186,7 @@ def test__main__invalid_commit_message_using_skip_detail(
177186

178187
@patch(
179188
"commitlint.cli.get_args",
180-
return_value=MagicMock(file="path/to/file.txt", skip_detail=False),
189+
return_value=MagicMock(file="path/to/file.txt", skip_detail=False, quiet=False),
181190
)
182191
@patch("sys.stdout.write")
183192
@patch("builtins.open", mock_open(read_data="feat: valid commit message"))
@@ -187,7 +196,7 @@ def test__main__valid_commit_message_with_file(self, mock_stdout_write, *_):
187196

188197
@patch(
189198
"commitlint.cli.get_args",
190-
return_value=MagicMock(file="path/to/file.txt", skip_detail=False),
199+
return_value=MagicMock(file="path/to/file.txt", skip_detail=False, quiet=False),
191200
)
192201
@patch("sys.stderr.write")
193202
@patch("sys.exit")
@@ -209,7 +218,9 @@ def test__main__invalid_commit_message_with_file(
209218

210219
@patch(
211220
"commitlint.cli.get_args",
212-
return_value=MagicMock(file=None, hash="commit_hash", skip_detail=False),
221+
return_value=MagicMock(
222+
file=None, hash="commit_hash", skip_detail=False, quiet=False
223+
),
213224
)
214225
@patch("commitlint.cli.get_commit_message_of_hash")
215226
@patch("sys.stdout.write")
@@ -222,7 +233,9 @@ def test__main__valid_commit_message_with_hash(
222233

223234
@patch(
224235
"commitlint.cli.get_args",
225-
return_value=MagicMock(file=None, hash="commit_hash", skip_detail=False),
236+
return_value=MagicMock(
237+
file=None, hash="commit_hash", skip_detail=False, quiet=False
238+
),
226239
)
227240
@patch("commitlint.cli.get_commit_message_of_hash")
228241
@patch("sys.stderr.write")
@@ -251,6 +264,7 @@ def test__main__invalid_commit_message_with_hash(
251264
from_hash="start_commit_hash",
252265
to_hash="end_commit_hash",
253266
skip_detail=False,
267+
quiet=False,
254268
),
255269
)
256270
@patch("commitlint.cli.get_commit_messages_of_hash_range")
@@ -273,6 +287,7 @@ def test__main__valid_commit_message_with_hash_range(
273287
from_hash="invalid_start_hash",
274288
to_hash="end_commit_hash",
275289
skip_detail=False,
290+
quiet=False,
276291
),
277292
)
278293
@patch("sys.stderr.write")
@@ -308,3 +323,50 @@ def test__main__handle_exceptions(
308323
main()
309324
mock_sys_exit.assert_called_with(1)
310325
mock_stderr_write.assert_called_with("Test message\n")
326+
327+
@patch(
328+
"argparse.ArgumentParser.parse_args",
329+
return_value=MagicMock(
330+
commit_message="Invalid commit message",
331+
file=None,
332+
hash=None,
333+
from_hash=None,
334+
quiet=True,
335+
),
336+
)
337+
@patch(
338+
"commitlint.cli.lint_commit_message",
339+
)
340+
@patch("sys.stderr.write")
341+
@patch("sys.exit")
342+
def test__main__handle_quiet_option(
343+
self, mock_sys_exit, mock_stderr_write, mock_lint_commit_message, *_
344+
):
345+
mock_lint_commit_message.side_effect = CommitlintException(
346+
"Invalid commit message"
347+
)
348+
main()
349+
mock_sys_exit.assert_called_with(1)
350+
mock_stderr_write.assert_called_with("Invalid commit message\n")
351+
352+
@patch(
353+
"commitlint.cli.get_args",
354+
return_value=MagicMock(
355+
commit_message="feat: valid commit message",
356+
file=None,
357+
hash=None,
358+
from_hash=None,
359+
skip_detail=False,
360+
quiet=True,
361+
),
362+
)
363+
@patch("sys.stdout.write")
364+
@patch("sys.stderr.write")
365+
@patch("sys.exit")
366+
def test__main__quiet_option_with_valid_commit_message(
367+
self, mock_sys_exit, mock_stderr_write, mock_stdout_write, *_
368+
):
369+
main()
370+
mock_stderr_write.assert_not_called()
371+
mock_stdout_write.assert_not_called()
372+
mock_sys_exit.assert_not_called()

0 commit comments

Comments
 (0)