Skip to content

test: add more tests and update pre-commit hook to ruff #211

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

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
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
11 changes: 6 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ repos:
- id: trailing-whitespace
- id: name-tests-test
- id: requirements-txt-fixer
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.9.3
hooks:
- id: flake8
args: [--max-line-length=100, --ignore=E501]
exclude: ^commit_check/__init__.py
# Run the linter.
- id: ruff
args: [ --fix ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.14.1
hooks:
Expand Down
4 changes: 2 additions & 2 deletions commit_check/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def check_branch(checks: list) -> int:
result = re.match(check['regex'], branch_name)
if result is None:
if not print_error_header.has_been_called:
print_error_header()
print_error_header() # pragma: no cover
print_error_message(
check['check'], check['regex'],
check['error'], branch_name,
Expand Down Expand Up @@ -45,7 +45,7 @@ def check_merge_base(checks: list) -> int:
result = git_merge_base(target_branch, current_branch)
if result != 0:
if not print_error_header.has_been_called:
print_error_header()
print_error_header() # pragma: no cover
print_error_message(
check['check'], check['regex'],
check['error'], current_branch,
Expand Down
4 changes: 2 additions & 2 deletions commit_check/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def check_commit_msg(checks: list, commit_msg_file: str = "") -> int:
result = re.match(check['regex'], commit_msg)
if result is None:
if not print_error_header.has_been_called:
print_error_header()
print_error_header() # pragma: no cover
print_error_message(
check['check'], check['regex'],
check['error'], commit_msg,
Expand Down Expand Up @@ -67,7 +67,7 @@ def check_commit_signoff(checks: list, commit_msg_file: str = "") -> int:
result = re.search(check['regex'], commit_msg)
if result is None:
if not print_error_header.has_been_called:
print_error_header()
print_error_header() # pragma: no cover
print_error_message(
check['check'], check['regex'],
check['error'], commit_hash,
Expand Down
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def commit_check(session):
@nox.session()
def coverage(session):
session.install(".")
session.install("-r", REQUIREMENTS["dev"])
session.run("coverage", "run", "--source", "commit_check", "-m", "pytest")
session.run("coverage", "report")
session.run("coverage", "xml")
Expand Down
11 changes: 11 additions & 0 deletions tests/branch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ def test_check_merge_base_with_empty_checks(self, mocker):
assert retval == PASS
assert m_check_merge.call_count == 0


def test_check_merge_base_with_empty_regex(self, mocker):
checks = [{
"check": "merge_base",
"regex": ""
}]
m_check_merge = mocker.patch(f"{LOCATION}.check_merge_base")
retval = check_merge_base(checks)
assert retval == PASS
assert m_check_merge.call_count == 0

def test_check_merge_base_with_different_check(self, mocker):
checks = [{
"check": "branch",
Expand Down
19 changes: 19 additions & 0 deletions tests/commit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ def test_read_commit_msg_file_not_found(mocker):
assert m_commits_info.call_count == 0


def test_check_commit_msg_no_commit_msg_file(mocker):
mock_get_default_commit_msg_file = mocker.patch(
"commit_check.commit.get_default_commit_msg_file",
return_value=".git/COMMIT_EDITMSG"
)
mock_read_commit_msg = mocker.patch(
"commit_check.commit.read_commit_msg",
return_value="Sample commit message"
)

checks = [{"regex": ".*", "check": "message", "error": "Invalid", "suggest": None}]

result = check_commit_msg(checks, commit_msg_file="")

mock_get_default_commit_msg_file.assert_called_once()
mock_read_commit_msg.assert_called_once_with(".git/COMMIT_EDITMSG")
assert result == 0


def test_check_commit_with_empty_checks(mocker):
checks = []
m_re_match = mocker.patch(
Expand Down
26 changes: 26 additions & 0 deletions tests/error_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ def test_error_handler_unexpected_error():
assert exit_info.value.code == 3


def test_error_handler_cannot_access(mocker):
with pytest.raises(SystemExit):
store_dir = "/fake/commit-check"
log_path = os.path.join(store_dir, "commit-check.log")
mocker.patch.dict(os.environ, {"COMMIT_CHECK_HOME": store_dir})
mock_os_access = mocker.patch("os.access", return_value=False)
mocker.patch("os.path.exists", return_value=True)
mocker.patch("os.makedirs")
mock_open = mocker.patch("builtins.open", mocker.mock_open())
mocker.patch("commit_check.util.cmd_output", return_value="mock_version")
mocker.patch("sys.version", "Mock Python Version")
mocker.patch("sys.executable", "/mock/path/to/python")

from commit_check.error import log_and_exit
log_and_exit(
msg="Test error message",
ret_code=1,
exc=ValueError("Test exception"),
formatted="Mocked formatted stack trace"
)

mock_os_access.assert_called_once_with(store_dir, os.W_OK)
mock_open.assert_called_with(log_path, "a")
mock_open().write.assert_any_call(f"Failed to write to log at {log_path}\n")


@pytest.mark.xfail
def test_log_and_exit(monkeypatch):
monkeypatch.setenv("COMMIT_CHECK_HOME", "")
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy