From d473eb73a6902ab0867ea38ffa875e6d8a4e683d Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 28 Jan 2025 03:00:03 +0200 Subject: [PATCH 1/3] add test and update hook to ruff --- .pre-commit-config.yaml | 11 ++++++----- commit_check/branch.py | 4 ++-- commit_check/commit.py | 2 +- tests/branch_test.py | 11 +++++++++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e3fed27..67f4460 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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.8.6 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: diff --git a/commit_check/branch.py b/commit_check/branch.py index 39bc7ec..1cc8a35 100644 --- a/commit_check/branch.py +++ b/commit_check/branch.py @@ -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, @@ -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, diff --git a/commit_check/commit.py b/commit_check/commit.py index 5505e77..87fd4cc 100644 --- a/commit_check/commit.py +++ b/commit_check/commit.py @@ -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, diff --git a/tests/branch_test.py b/tests/branch_test.py index 64ec3a4..083c5bd 100644 --- a/tests/branch_test.py +++ b/tests/branch_test.py @@ -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", From c5945aa4450336912fe04e7791f37ea739405108 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 28 Jan 2025 10:33:40 +0000 Subject: [PATCH 2/3] feat: add more test --- .pre-commit-config.yaml | 2 +- commit_check/commit.py | 2 +- noxfile.py | 1 + tests/commit_test.py | 19 +++++++++++++++++++ tests/error_test.py | 26 ++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 67f4460..e1d96d8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: - id: requirements-txt-fixer - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.8.6 + rev: v0.9.3 hooks: # Run the linter. - id: ruff diff --git a/commit_check/commit.py b/commit_check/commit.py index 87fd4cc..60b8d2f 100644 --- a/commit_check/commit.py +++ b/commit_check/commit.py @@ -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, diff --git a/noxfile.py b/noxfile.py index 109e2df..079df8a 100644 --- a/noxfile.py +++ b/noxfile.py @@ -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") diff --git a/tests/commit_test.py b/tests/commit_test.py index 5642a52..caba3dc 100644 --- a/tests/commit_test.py +++ b/tests/commit_test.py @@ -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=None) + + 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( diff --git a/tests/error_test.py b/tests/error_test.py index 188a2b1..d0a2880 100644 --- a/tests/error_test.py +++ b/tests/error_test.py @@ -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", "") From 600d2f6b665c3412e352d9cb1ebe3464cf4b681b Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Tue, 28 Jan 2025 10:37:29 +0000 Subject: [PATCH 3/3] fix: update commit_test.py --- tests/commit_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commit_test.py b/tests/commit_test.py index caba3dc..a458636 100644 --- a/tests/commit_test.py +++ b/tests/commit_test.py @@ -42,7 +42,7 @@ def test_check_commit_msg_no_commit_msg_file(mocker): checks = [{"regex": ".*", "check": "message", "error": "Invalid", "suggest": None}] - result = check_commit_msg(checks, commit_msg_file=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") 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