Skip to content

Commit

Permalink
refactor: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Sugat Bajracharya <[email protected]>
  • Loading branch information
aj3sh and sugat009 committed Mar 29, 2024
1 parent fdcd125 commit 9c94754
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 95 deletions.
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[pytest]
pythonpath = src
python_files = test_*.py
addopts = -vvv
2 changes: 1 addition & 1 deletion src/commitlint/linter/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def validate(self) -> None:
Returns:
None
"""
header = self.commit_message.split("\n").pop()
header = self.commit_message.split("\n")[0]
if len(header) > COMMIT_HEADER_MAX_LENGTH:
self.add_error(HEADER_LENGTH_ERROR)

Expand Down
92 changes: 92 additions & 0 deletions tests/fixtures/linter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# type: ignore
# pylint: disable=all
from typing import List, Tuple

from commitlint.constants import COMMIT_HEADER_MAX_LENGTH
from commitlint.messages import (
COMMIT_TYPE_INVALID_ERROR,
COMMIT_TYPE_MISSING_ERROR,
DESCRIPTION_FULL_STOP_END_ERROR,
DESCRIPTION_LINE_BREAK_ERROR,
DESCRIPTION_MISSING_ERROR,
DESCRIPTION_MULTIPLE_SPACE_START_ERROR,
DESCRIPTION_NO_LEADING_SPACE_ERROR,
HEADER_LENGTH_ERROR,
INCORRECT_FORMAT_ERROR,
SCOPE_EMPTY_ERROR,
SCOPE_WHITESPACE_ERROR,
SPACE_AFTER_COMMIT_TYPE_ERROR,
SPACE_AFTER_SCOPE_ERROR,
)

# LINTER_FIXTURE_PARAMS contains test data for the function `lint_commit_message`. The
# purpose of creating this data is to ensure consistent results for both detailed and
# quick commit message linting processes.
#
# The data consists of tuples, each representing a test case with the following
# structure:
# (commit_message, expected_success, expected_errors)
#
# - commit_message: The commit message to be tested.
# - expected_success: A boolean indicating whether the commit message is expected to
# pass or fail the linting.
# - expected_errors: A list of error messages expected for detailed linting (not for
# quick linting).
LINTER_FIXTURE_PARAMS: Tuple[Tuple[str, bool, List[str], List[str]], ...] = (
# success
("feat: add new feature", True, []),
("feat: add new feature\n\nthis is body", True, []),
(
"feat: add new feature\n\nthis is body" + "a" * COMMIT_HEADER_MAX_LENGTH,
True,
[],
),
("feat: add new feature\n\nthis is body\n\ntest", True, []),
("feat: add new feature\n", True, []),
("build(deps-dev): bump @babel/traverse from 7.22.17 to 7.24.0", True, []),
("feat(scope): add new feature\n#this is a comment", True, []),
(
"fix: fixed a bug\n\nthis is body\n"
"# ------------------------ >8 ------------------------\nDiff message",
True,
[],
),
("Merge pull request #123", True, []),
# incorrect format check
("feat add new feature", False, [INCORRECT_FORMAT_ERROR]),
# header length check
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
(
"Test " + "a" * (COMMIT_HEADER_MAX_LENGTH + 1),
False,
[HEADER_LENGTH_ERROR, INCORRECT_FORMAT_ERROR],
),
# commit type check
(": add new feature", False, [COMMIT_TYPE_MISSING_ERROR]),
("(invalid): add new feature", False, [COMMIT_TYPE_MISSING_ERROR]),
("invalid: add new feature", False, [COMMIT_TYPE_INVALID_ERROR % "invalid"]),
("feat (test): add new feature", False, [SPACE_AFTER_COMMIT_TYPE_ERROR]),
(
"invalid (test): add new feature",
False,
[COMMIT_TYPE_INVALID_ERROR % "invalid", SPACE_AFTER_COMMIT_TYPE_ERROR],
),
# scope check
("feat(): add new feature", False, [SCOPE_EMPTY_ERROR]),
("feat( ): add new feature", False, [SCOPE_WHITESPACE_ERROR]),
("feat(hello world): add new feature", False, [SCOPE_WHITESPACE_ERROR]),
("feat(test) : add new feature", False, [SPACE_AFTER_SCOPE_ERROR]),
(
"feat (test) : add new feature",
False,
[SPACE_AFTER_COMMIT_TYPE_ERROR, SPACE_AFTER_SCOPE_ERROR],
),
# description check
("feat:add new feature", False, [DESCRIPTION_NO_LEADING_SPACE_ERROR]),
("feat: add new feature", False, [DESCRIPTION_MULTIPLE_SPACE_START_ERROR]),
("feat: add new feature\nhello baby", False, [DESCRIPTION_LINE_BREAK_ERROR]),
("feat(test):", False, [DESCRIPTION_MISSING_ERROR]),
("feat(test): ", False, [DESCRIPTION_MISSING_ERROR]),
("feat(test): add new feature.", False, [DESCRIPTION_FULL_STOP_END_ERROR]),
)
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test__main__valid_commit_message_using_quick(
*_,
):
main()
mock_stdout_write.assert_called_with(f"{VALIDATION_SUCCESSFUL}\n")
mock_stdout_write.assert_called_once_with(f"{VALIDATION_SUCCESSFUL}\n")

@patch(
"commitlint.cli.get_args",
Expand Down
107 changes: 14 additions & 93 deletions tests/test_linter/test__linter.py
Original file line number Diff line number Diff line change
@@ -1,112 +1,33 @@
# type: ignore
# pylint: disable=all
from typing import List, Tuple

import pytest

from commitlint.constants import COMMIT_HEADER_MAX_LENGTH
from commitlint.linter import lint_commit_message
from commitlint.messages import (
COMMIT_TYPE_INVALID_ERROR,
COMMIT_TYPE_MISSING_ERROR,
DESCRIPTION_FULL_STOP_END_ERROR,
DESCRIPTION_LINE_BREAK_ERROR,
DESCRIPTION_MISSING_ERROR,
DESCRIPTION_MULTIPLE_SPACE_START_ERROR,
DESCRIPTION_NO_LEADING_SPACE_ERROR,
HEADER_LENGTH_ERROR,
INCORRECT_FORMAT_ERROR,
SCOPE_EMPTY_ERROR,
SCOPE_WHITESPACE_ERROR,
SPACE_AFTER_COMMIT_TYPE_ERROR,
SPACE_AFTER_SCOPE_ERROR,
)
from ..fixtures.linter import LINTER_FIXTURE_PARAMS

# LINTER_TEST_DATA contains test data for the function `lint_commit_message`. The
# purpose of creating this data is to ensure consistent results for both detailed and
# quick commit message linting processes.
#
# The data consists of tuples, each representing a test case with the following
# structure:
# (commit_message, expected_success, expected_errors)
#
# - commit_message: The commit message to be tested.
# - expected_success: A boolean indicating whether the commit message is expected to
# pass or fail the linting.
# - expected_errors: A list of error messages expected for detailed linting (not for
# quick linting).
LINTER_TEST_DATA: Tuple[Tuple[str, bool, List[str], List[str]], ...] = (
# success
("feat: add new feature", True, []),
("feat: add new feature\n\nthis is body", True, []),
("feat: add new feature\n\nthis is body\n\ntest", True, []),
("feat: add new feature\n", True, []),
("build(deps-dev): bump @babel/traverse from 7.22.17 to 7.24.0", True, []),
("feat(scope): add new feature\n#this is a comment", True, []),
(
"fix: fixed a bug\n\nthis is body\n"
"# ------------------------ >8 ------------------------\nDiff message",
True,
[],
),
("Merge pull request #123", True, []),
# incorrect format check
("feat add new feature", False, [INCORRECT_FORMAT_ERROR]),
# header length check
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
(
"Test " + "a" * (COMMIT_HEADER_MAX_LENGTH + 1),
False,
[HEADER_LENGTH_ERROR, INCORRECT_FORMAT_ERROR],
),
# commit type check
(": add new feature", False, [COMMIT_TYPE_MISSING_ERROR]),
("(invalid): add new feature", False, [COMMIT_TYPE_MISSING_ERROR]),
("invalid: add new feature", False, [COMMIT_TYPE_INVALID_ERROR % "invalid"]),
("feat (test): add new feature", False, [SPACE_AFTER_COMMIT_TYPE_ERROR]),
(
"invalid (test): add new feature",
False,
[COMMIT_TYPE_INVALID_ERROR % "invalid", SPACE_AFTER_COMMIT_TYPE_ERROR],
),
# scope check
("feat(): add new feature", False, [SCOPE_EMPTY_ERROR]),
("feat( ): add new feature", False, [SCOPE_WHITESPACE_ERROR]),
("feat(hello world): add new feature", False, [SCOPE_WHITESPACE_ERROR]),
("feat(test) : add new feature", False, [SPACE_AFTER_SCOPE_ERROR]),
(
"feat (test) : add new feature",
False,
[SPACE_AFTER_COMMIT_TYPE_ERROR, SPACE_AFTER_SCOPE_ERROR],
),
# description check
("feat:add new feature", False, [DESCRIPTION_NO_LEADING_SPACE_ERROR]),
("feat: add new feature", False, [DESCRIPTION_MULTIPLE_SPACE_START_ERROR]),
("feat: add new feature\nhello baby", False, [DESCRIPTION_LINE_BREAK_ERROR]),
("feat(test):", False, [DESCRIPTION_MISSING_ERROR]),
("feat(test): ", False, [DESCRIPTION_MISSING_ERROR]),
("feat(test): add new feature.", False, [DESCRIPTION_FULL_STOP_END_ERROR]),
)

@pytest.fixture(params=LINTER_FIXTURE_PARAMS)
def fixture_data(request):
return request.param

@pytest.mark.parametrize(
"commit_message, success, errors",
LINTER_TEST_DATA,
)
def test__lint_commit_message(commit_message, success, errors):
_success, _errors = lint_commit_message(commit_message, quick=False)
assert _success is success
assert _errors == errors

def test_lint_commit_message(fixture_data):
commit_message, expected_success, expected_errors = fixture_data
success, errors = lint_commit_message(commit_message, quick=False)
assert success == expected_success
assert errors == expected_errors

@pytest.mark.parametrize(
"commit_message, success, _",
LINTER_TEST_DATA,
)
def test__lint_commit_message__quick(commit_message, success, _):
_success, _ = lint_commit_message(commit_message, quick=True)
assert _success is success

def test__lint_commit_message__quick(fixture_data):
commit_message, expected_success, _ = fixture_data
success, _ = lint_commit_message(commit_message, quick=True)
assert success == expected_success


def test__lint_commit_message__quick_returns_header_length_error_message():
Expand Down

0 comments on commit 9c94754

Please sign in to comment.