Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor tests for system exit #40

Merged
merged 1 commit into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 35 additions & 36 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from unittest.mock import MagicMock, call, mock_open, patch

import pytest

from commitlint.cli import get_args, main
from commitlint.exceptions import CommitlintException
from commitlint.messages import (
Expand Down Expand Up @@ -137,15 +139,14 @@ def test__main__valid_commit_message_using_skip_detail(
),
)
@patch("sys.stderr.write")
@patch("sys.exit")
def test__main__invalid_commit_message(
self,
mock_sys_exit,
mock_stderr_write,
*_,
):
main()
mock_sys_exit.assert_called_with(1)
with pytest.raises(SystemExit):
main()

mock_stderr_write.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message\n\n"),
Expand All @@ -166,15 +167,14 @@ def test__main__invalid_commit_message(
),
)
@patch("sys.stderr.write")
@patch("sys.exit")
def test__main__invalid_commit_message_using_skip_detail(
self,
mock_sys_exit,
mock_stderr_write,
*_,
):
main()
mock_sys_exit.assert_called_with(1)
with pytest.raises(SystemExit):
main()

mock_stderr_write.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message\n\n"),
Expand All @@ -199,13 +199,11 @@ def test__main__valid_commit_message_with_file(self, mock_stdout_write, *_):
return_value=MagicMock(file="path/to/file.txt", skip_detail=False, quiet=False),
)
@patch("sys.stderr.write")
@patch("sys.exit")
@patch("builtins.open", mock_open(read_data="Invalid commit message 2"))
def test__main__invalid_commit_message_with_file(
self, mock_sys_exit, mock_stderr_write, *_
):
main()
mock_sys_exit.assert_called_with(1)
def test__main__invalid_commit_message_with_file(self, mock_stderr_write, *_):
with pytest.raises(SystemExit):
main()

mock_stderr_write.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message 2\n\n"),
Expand Down Expand Up @@ -239,13 +237,14 @@ def test__main__valid_commit_message_with_hash(
)
@patch("commitlint.cli.get_commit_message_of_hash")
@patch("sys.stderr.write")
@patch("sys.exit")
def test__main__invalid_commit_message_with_hash(
self, mock_sys_exit, mock_stderr_write, mock_get_commit_message_of_hash, *_
self, mock_stderr_write, mock_get_commit_message_of_hash, *_
):
mock_get_commit_message_of_hash.return_value = "Invalid commit message"
main()
mock_sys_exit.assert_called_with(1)

with pytest.raises(SystemExit):
main()

mock_stderr_write.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message\n\n"),
Expand Down Expand Up @@ -292,16 +291,16 @@ def test__main__valid_commit_message_with_hash_range(
)
@patch("sys.stderr.write")
@patch("commitlint.cli.get_commit_messages_of_hash_range")
@patch("sys.exit")
def test__main__invalid_commit_message_with_hash_range(
self, mock_sys_exit, mock_get_commit_messages, *_
self, mock_get_commit_messages, *_
):
mock_get_commit_messages.return_value = [
"Invalid commit message 1",
"Invalid commit message 2",
]
main()
mock_sys_exit.assert_called_with(1)

with pytest.raises(SystemExit):
main()

# main : exception handling

Expand All @@ -315,13 +314,14 @@ def test__main__invalid_commit_message_with_hash_range(
"commitlint.cli.lint_commit_message",
)
@patch("sys.stderr.write")
@patch("sys.exit")
def test__main__handle_exceptions(
self, mock_sys_exit, mock_stderr_write, mock_lint_commit_message, *_
self, mock_stderr_write, mock_lint_commit_message, *_
):
mock_lint_commit_message.side_effect = CommitlintException("Test message")
main()
mock_sys_exit.assert_called_with(1)

with pytest.raises(SystemExit):
main()

mock_stderr_write.assert_called_with("Test message\n")

@patch(
Expand All @@ -337,11 +337,12 @@ def test__main__handle_exceptions(
)
@patch("sys.stdout.write")
@patch("sys.stderr.write")
@patch("sys.exit")
def test__main__quiet_option_with_invalid_commit_message(
self, mock_sys_exit, mock_stderr_write, mock_stdout_write, *_
self, mock_stderr_write, mock_stdout_write, *_
):
main()
with pytest.raises(SystemExit):
main()

mock_stderr_write.assert_not_called()
mock_stdout_write.assert_not_called()

Expand All @@ -358,14 +359,12 @@ def test__main__quiet_option_with_invalid_commit_message(
)
@patch("sys.stdout.write")
@patch("sys.stderr.write")
@patch("sys.exit")
def test__main__quiet_option_with_valid_commit_message(
self, mock_sys_exit, mock_stderr_write, mock_stdout_write, *_
self, mock_stderr_write, mock_stdout_write, *_
):
main()
mock_stderr_write.assert_not_called()
mock_stdout_write.assert_not_called()
mock_sys_exit.assert_not_called()

@patch(
"commitlint.cli.get_args",
Expand Down Expand Up @@ -402,22 +401,22 @@ def test__valid_commit_message_with_hash_range_in_quiet(
),
)
@patch("commitlint.cli.get_commit_messages_of_hash_range")
@patch("sys.exit")
@patch("sys.stdout.write")
@patch("sys.stderr.write")
def test__invalid_commit_message_with_hash_range_in_quiet(
self,
mock_stderr_write,
mock_stdout_write,
mock_sys_exit,
mock_get_commit_messages,
*_,
):
mock_get_commit_messages.return_value = [
"Invalid commit message 1",
"Invalid commit message 2",
]
main()

with pytest.raises(SystemExit):
main()

mock_stderr_write.assert_not_called()
mock_sys_exit.assert_called_once_with(1)
mock_stdout_write.assert_not_called()
Loading