Files
devx/tests/unit/test_pre_push_check.py
T
emil 44c906a5e6
Post-merge / detect-type (push) Successful in 6s
Post-merge / validate-commit-msg (push) Successful in 6s
Post-merge / configure-repo (push) Successful in 9s
Post-merge / release (push) Successful in 1m0s
Post-merge / vikunja (push) Successful in 14s
Post-merge / sync-wiki (push) Successful in 59s
Post-merge / badges (push) Successful in 1m12s
DEVX-60: feat: add create-task, create-pr, pre-push-check tools and devx.mak fragment
2026-06-26 14:29:47 +00:00

138 lines
5.1 KiB
Python

"""Unit tests for devx.tools.pre_push_check."""
from unittest.mock import MagicMock, patch
import click
import pytest
from click.testing import CliRunner
from devx.tools.pre_push_check import (
cli,
extract_task_id,
get_current_branch,
task_exists,
validate,
)
class TestExtractTaskId:
def test_valid_branch(self) -> None:
assert extract_task_id("DEVX-42-fix-bug") == "DEVX-42"
def test_no_task_id(self) -> None:
assert extract_task_id("feature-branch") == ""
def test_empty_branch(self) -> None:
assert extract_task_id("") == ""
class TestGetCurrentBranch:
@patch("devx.tools.pre_push_check.subprocess.run")
def test_success(self, mock_run: MagicMock) -> None:
mock_run.return_value = MagicMock(stdout="DEVX-42-fix\n", returncode=0)
assert get_current_branch() == "DEVX-42-fix"
@patch("devx.tools.pre_push_check.subprocess.run")
def test_failure(self, mock_run: MagicMock) -> None:
mock_run.return_value = MagicMock(stdout="", returncode=1)
assert get_current_branch() == ""
class TestTaskExists:
@patch("devx.tools.pre_push_check.VikunjaClient")
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_found(self, mock_client_cls: MagicMock) -> None:
mock_client = MagicMock()
mock_client.list_project_tasks.return_value = [{"identifier": "DEVX-42"}]
mock_client_cls.return_value = mock_client
assert task_exists("DEVX-42") is True
@patch("devx.tools.pre_push_check.VikunjaClient")
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_not_found(self, mock_client_cls: MagicMock) -> None:
mock_client = MagicMock()
mock_client.list_project_tasks.return_value = [{"identifier": "DEVX-99"}]
mock_client_cls.return_value = mock_client
assert task_exists("DEVX-42") is False
@patch.dict("os.environ", {}, clear=True)
def test_no_token(self) -> None:
assert task_exists("DEVX-42") is False
@patch("devx.tools.pre_push_check.VikunjaClient")
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_pagination(self, mock_client_cls: MagicMock) -> None:
mock_client = MagicMock()
# First page: full page (50 items, none matching), second page: match
page1 = [{"identifier": f"OTHER-{i}"} for i in range(50)]
page2 = [{"identifier": "DEVX-42"}]
mock_client.list_project_tasks.side_effect = [page1, page2]
mock_client_cls.return_value = mock_client
assert task_exists("DEVX-42") is True
@patch("devx.tools.pre_push_check.VikunjaClient")
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_empty_project(self, mock_client_cls: MagicMock) -> None:
mock_client = MagicMock()
mock_client.list_project_tasks.return_value = []
mock_client_cls.return_value = mock_client
assert task_exists("DEVX-42") is False
@patch("devx.tools.pre_push_check.VikunjaClient")
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_pagination_not_found(self, mock_client_cls: MagicMock) -> None:
from devx.config import DEFAULT_PER_PAGE
mock_client = MagicMock()
page1 = [{"identifier": f"OTHER-{i}"} for i in range(DEFAULT_PER_PAGE)]
page2 = [{"identifier": "OTHER-99"}]
mock_client.list_project_tasks.side_effect = [page1, page2]
mock_client_cls.return_value = mock_client
assert task_exists("DEVX-42") is False
class TestValidate:
def test_master_branch_skips(self) -> None:
validate("master")
def test_main_branch_skips(self) -> None:
validate("main")
def test_empty_branch_skips(self) -> None:
validate("")
def test_no_task_id_raises(self) -> None:
with pytest.raises(click.ClickException, match="does not contain a task ID"):
validate("feature-branch")
@patch.dict("os.environ", {}, clear=True)
def test_no_token_warns(self) -> None:
validate("DEVX-42-fix-bug")
@patch("devx.tools.pre_push_check.task_exists", return_value=True)
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_task_exists_passes(self, mock_exists: MagicMock) -> None:
validate("DEVX-42-fix-bug")
@patch("devx.tools.pre_push_check.task_exists", return_value=False)
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_task_not_found_raises(self, mock_exists: MagicMock) -> None:
with pytest.raises(click.ClickException, match="not found"):
validate("DEVX-42-fix-bug")
class TestCli:
@patch("devx.tools.pre_push_check.get_current_branch", return_value="master")
def test_auto_detect_master(self, mock_branch: MagicMock) -> None:
runner = CliRunner()
result = runner.invoke(cli, [])
assert result.exit_code == 0
@patch("devx.tools.pre_push_check.task_exists", return_value=True)
@patch.dict("os.environ", {"VIKUNJA_TOKEN": "tok"})
def test_explicit_branch(self, mock_exists: MagicMock) -> None:
runner = CliRunner()
result = runner.invoke(cli, ["--branch", "DEVX-42-fix"])
assert result.exit_code == 0
assert "passed" in result.output