Skip to content

Commit 7624236

Browse files
golesumanaj3sh
authored andcommitted
feat(cli): added the quiet option in cli
1 parent 158a690 commit 7624236

File tree

2 files changed

+157
-19
lines changed

2 files changed

+157
-19
lines changed

src/commitlint/cli.py

+39-14
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ 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+
"-q",
70+
"--quiet",
71+
action="store_true",
72+
help="Ignore stdout and stderr",
73+
default=False,
74+
)
6875
# parsing args
6976
args = parser.parse_args()
7077

@@ -119,36 +126,46 @@ def _get_commit_message_from_file(filepath: str) -> str:
119126
return commit_message
120127

121128

122-
def _handle_commit_message(commit_message: str, skip_detail: bool) -> None:
129+
def _handle_commit_message(
130+
commit_message: str, skip_detail: bool, quiet: bool = False
131+
) -> None:
123132
"""
124133
Handles a single commit message, checks its validity, and prints the result.
125134
126135
Args:
127136
commit_message (str): The commit message to be handled.
128137
skip_detail (bool): Whether to skip the detailed error linting.
138+
quiet (bool): Whether to ignore stout and stderr
129139
130140
Raises:
131141
SystemExit: If the commit message is invalid.
132142
"""
133143
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
134144

145+
if quiet and not success:
146+
sys.exit(1)
147+
135148
if success:
136-
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
137-
else:
138-
_show_errors(commit_message, errors, skip_detail=skip_detail)
149+
if not quiet:
150+
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
151+
152+
if not success:
153+
if not quiet:
154+
_show_errors(commit_message, errors, skip_detail=skip_detail)
155+
139156
sys.exit(1)
140157

141158

142159
def _handle_multiple_commit_messages(
143-
commit_messages: List[str], skip_detail: bool
160+
commit_messages: List[str], skip_detail: bool, quiet: bool = False
144161
) -> None:
145162
"""
146163
Handles multiple commit messages, checks their validity, and prints the result.
147164
148165
Args:
149166
commit_messages (List[str]): List of commit messages to be handled.
150167
skip_detail (bool): Whether to skip the detailed error linting.
151-
168+
quiet (bool): Whether to show the error and messages in console
152169
Raises:
153170
SystemExit: If any of the commit messages is invalid.
154171
"""
@@ -157,12 +174,14 @@ def _handle_multiple_commit_messages(
157174
success, errors = lint_commit_message(commit_message, skip_detail=skip_detail)
158175
if not success:
159176
has_error = True
160-
_show_errors(commit_message, errors, skip_detail=skip_detail)
161-
sys.stderr.write("\n")
177+
if not quiet:
178+
_show_errors(commit_message, errors, skip_detail=skip_detail)
179+
sys.stderr.write("\n")
162180

163181
if has_error:
164182
sys.exit(1)
165-
else:
183+
184+
if not quiet:
166185
sys.stdout.write(f"{VALIDATION_SUCCESSFUL}\n")
167186

168187

@@ -175,20 +194,26 @@ def main() -> None:
175194
try:
176195
if args.file:
177196
commit_message = _get_commit_message_from_file(args.file)
178-
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
197+
_handle_commit_message(
198+
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
199+
)
179200
elif args.hash:
180201
commit_message = get_commit_message_of_hash(args.hash)
181-
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
202+
_handle_commit_message(
203+
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
204+
)
182205
elif args.from_hash:
183206
commit_messages = get_commit_messages_of_hash_range(
184207
args.from_hash, args.to_hash
185208
)
186209
_handle_multiple_commit_messages(
187-
commit_messages, skip_detail=args.skip_detail
210+
commit_messages, skip_detail=args.skip_detail, quiet=args.quiet
188211
)
189212
else:
190213
commit_message = args.commit_message.strip()
191-
_handle_commit_message(commit_message, skip_detail=args.skip_detail)
214+
_handle_commit_message(
215+
commit_message, skip_detail=args.skip_detail, quiet=args.quiet
216+
)
192217
except CommitlintException as ex:
193218
sys.stderr.write(f"{ex}\n")
194219
sys.exit(1)

tests/test_cli.py

+118-5
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,101 @@ 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+
"commitlint.cli.get_args",
329+
return_value=MagicMock(
330+
commit_message="Invalid commit message",
331+
file=None,
332+
hash=None,
333+
from_hash=None,
334+
skip_detail=False,
335+
quiet=True,
336+
),
337+
)
338+
@patch("sys.stdout.write")
339+
@patch("sys.stderr.write")
340+
@patch("sys.exit")
341+
def test__main__quiet_option_with_invalid_commit_message(
342+
self, mock_sys_exit, mock_stderr_write, mock_stdout_write, *_
343+
):
344+
main()
345+
mock_stderr_write.assert_not_called()
346+
mock_stdout_write.assert_not_called()
347+
348+
@patch(
349+
"commitlint.cli.get_args",
350+
return_value=MagicMock(
351+
commit_message="feat: valid commit message",
352+
file=None,
353+
hash=None,
354+
from_hash=None,
355+
skip_detail=False,
356+
quiet=True,
357+
),
358+
)
359+
@patch("sys.stdout.write")
360+
@patch("sys.stderr.write")
361+
@patch("sys.exit")
362+
def test__main__quiet_option_with_valid_commit_message(
363+
self, mock_sys_exit, mock_stderr_write, mock_stdout_write, *_
364+
):
365+
main()
366+
mock_stderr_write.assert_not_called()
367+
mock_stdout_write.assert_not_called()
368+
mock_sys_exit.assert_not_called()
369+
370+
@patch(
371+
"commitlint.cli.get_args",
372+
return_value=MagicMock(
373+
file=None,
374+
hash=None,
375+
from_hash="start_commit_hash",
376+
to_hash="end_commit_hash",
377+
skip_detail=False,
378+
quiet=True,
379+
),
380+
)
381+
@patch("commitlint.cli.get_commit_messages_of_hash_range")
382+
@patch("sys.stdout.write")
383+
def test__valid_commit_message_with_hash_range_in_quiet(
384+
self, mock_stdout_write, mock_get_commit_messages, *_
385+
):
386+
mock_get_commit_messages.return_value = [
387+
"feat: commit message 1",
388+
"fix: commit message 2",
389+
]
390+
main()
391+
mock_stdout_write.assert_not_called()
392+
393+
@patch(
394+
"commitlint.cli.get_args",
395+
return_value=MagicMock(
396+
file=None,
397+
hash=None,
398+
from_hash="start_commit_hash",
399+
to_hash="end_commit_hash",
400+
skip_detail=False,
401+
quiet=True,
402+
),
403+
)
404+
@patch("commitlint.cli.get_commit_messages_of_hash_range")
405+
@patch("sys.exit")
406+
@patch("sys.stdout.write")
407+
@patch("sys.stderr.write")
408+
def test__invalid_commit_message_with_hash_range_in_quiet(
409+
self,
410+
mock_stderr_write,
411+
mock_stdout_write,
412+
mock_sys_exit,
413+
mock_get_commit_messages,
414+
*_,
415+
):
416+
mock_get_commit_messages.return_value = [
417+
"Invalid commit message 1",
418+
"Invalid commit message 2",
419+
]
420+
main()
421+
mock_stderr_write.assert_not_called()
422+
mock_sys_exit.assert_called_once_with(1)
423+
mock_stdout_write.assert_not_called()

0 commit comments

Comments
 (0)