Public Access
Post-merge / detect-type (push) Successful in 11s
Post-merge / validate-commit-msg (push) Successful in 9s
Post-merge / configure-repo (push) Successful in 17s
Post-merge / release (push) Successful in 1m22s
Post-merge / vikunja (push) Successful in 32s
Post-merge / badges (push) Successful in 50s
Post-merge / sync-wiki (push) Successful in 57s
219 lines
9.0 KiB
Python
219 lines
9.0 KiB
Python
"""Unit tests for scripts/ci/notify_failure.py."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from click.testing import CliRunner
|
|
|
|
from devx.ci.notify_failure import _configure_tea_login, main
|
|
from devx.gitea_cli import TeaCLIError
|
|
|
|
|
|
class TestNotifyFailure:
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_creates_issue_with_tea(self, mock_tea_cls: MagicMock) -> None:
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.return_value = [{"id": 5, "name": "bug"}]
|
|
mock_tea.create_issue.return_value = {"index": 42, "title": "test"}
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
[
|
|
"--repo",
|
|
"owner/repo",
|
|
"--run-id",
|
|
"123",
|
|
"--workflow",
|
|
"release",
|
|
"--commit",
|
|
"abc123def456",
|
|
],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "issue #42" in result.output
|
|
mock_tea.create_issue.assert_called_once()
|
|
mock_tea.add_label.assert_called_once_with("owner/repo", 42, ["bug"])
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_tea_creates_issue_without_bug_label(self, mock_tea_cls: MagicMock) -> None:
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.return_value = [{"id": 1, "name": "enhancement"}]
|
|
mock_tea.create_issue.return_value = {"index": 43, "title": "test"}
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "124", "--workflow", "publish", "--commit", "def789"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "issue #43" in result.output
|
|
mock_tea.add_label.assert_not_called()
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_tea_error_raises(self, mock_tea_cls: MagicMock) -> None:
|
|
"""When tea fails, the workflow fails — no fallback."""
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.side_effect = TeaCLIError("network error")
|
|
mock_tea.create_issue.side_effect = TeaCLIError("network error")
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "125", "--workflow", "release", "--commit", "abc"],
|
|
)
|
|
assert result.exit_code != 0
|
|
assert "tea" in result.output.lower()
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_tea_list_labels_error_continues_without_labels(self, mock_tea_cls: MagicMock) -> None:
|
|
"""If listing labels fails via tea, issue is still created without labels."""
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.side_effect = TeaCLIError("network error")
|
|
mock_tea.create_issue.return_value = {"index": 50, "title": "test"}
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "128", "--workflow", "release", "--commit", "abc"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "issue #50" in result.output
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_tea_add_label_error_is_ignored(self, mock_tea_cls: MagicMock) -> None:
|
|
"""If adding label fails via tea, issue is still reported as created."""
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.return_value = [{"id": 5, "name": "bug"}]
|
|
mock_tea.create_issue.return_value = {"index": 51, "title": "test"}
|
|
mock_tea.add_label.side_effect = TeaCLIError("permission denied")
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "129", "--workflow", "release", "--commit", "abc"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "issue #51" in result.output
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": ""}, clear=True)
|
|
def test_missing_token_exits(self) -> None:
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "1", "--workflow", "release", "--commit", "abc"],
|
|
)
|
|
assert result.exit_code != 0
|
|
assert "REPO_TOKEN" in result.output
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.shutil.which", return_value=None)
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_auto_login_no_tea_skips(self, mock_tea_cls: MagicMock, mock_which: MagicMock) -> None:
|
|
"""--auto-login with tea not installed skips login and still creates issue."""
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.return_value = []
|
|
mock_tea.create_issue.return_value = {"index": 60, "title": "test"}
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "1", "--workflow", "release", "--commit", "abc", "--auto-login"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "issue #60" in result.output
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": ""}, clear=True)
|
|
@patch("devx.ci.notify_failure.shutil.which", return_value="/usr/bin/tea")
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_auto_login_no_token_skips_login(self, mock_tea_cls: MagicMock, mock_which: MagicMock) -> None:
|
|
"""--auto-login with no REPO_TOKEN skips login but raises before creating issue."""
|
|
mock_tea = MagicMock()
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "1", "--workflow", "release", "--commit", "abc", "--auto-login"],
|
|
)
|
|
assert result.exit_code != 0
|
|
assert "REPO_TOKEN" in result.output
|
|
|
|
|
|
class TestConfigureTeaLogin:
|
|
@patch.dict("os.environ", {"REPO_TOKEN": ""}, clear=True)
|
|
@patch("devx.ci.notify_failure.shutil.which", return_value="/usr/bin/tea")
|
|
def test_no_token_skips(self, mock_which: MagicMock) -> None:
|
|
"""_configure_tea_login with no token prints skip message and returns."""
|
|
_configure_tea_login()
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.shutil.which", return_value=None)
|
|
def test_no_tea_skips(self, mock_which: MagicMock) -> None:
|
|
"""_configure_tea_login with no tea binary prints skip message and returns."""
|
|
_configure_tea_login()
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.shutil.which", return_value="/usr/bin/tea")
|
|
@patch("devx.ci.notify_failure.subprocess.run")
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_auto_login_configures_tea(
|
|
self, mock_tea_cls: MagicMock, mock_subprocess: MagicMock, mock_which: MagicMock
|
|
) -> None:
|
|
"""--auto-login calls tea login add and default."""
|
|
mock_run = MagicMock()
|
|
mock_run.returncode = 0
|
|
mock_run.stdout = ""
|
|
mock_subprocess.return_value = mock_run
|
|
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.return_value = []
|
|
mock_tea.create_issue.return_value = {"index": 61, "title": "test"}
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "1", "--workflow", "release", "--commit", "abc", "--auto-login"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "issue #61" in result.output
|
|
# tea login add was called
|
|
assert mock_subprocess.call_count >= 2
|
|
|
|
@patch.dict("os.environ", {"REPO_TOKEN": "tok"})
|
|
@patch("devx.ci.notify_failure.shutil.which", return_value="/usr/bin/tea")
|
|
@patch("devx.ci.notify_failure.subprocess.run")
|
|
@patch("devx.ci.notify_failure.TeaCLI")
|
|
def test_auto_login_skips_if_already_configured(
|
|
self, mock_tea_cls: MagicMock, mock_subprocess: MagicMock, mock_which: MagicMock
|
|
) -> None:
|
|
"""--auto-login skips tea login add if login already exists."""
|
|
mock_list = MagicMock()
|
|
mock_list.returncode = 0
|
|
mock_list.stdout = "devx https://git.example.com"
|
|
mock_subprocess.return_value = mock_list
|
|
|
|
mock_tea = MagicMock()
|
|
mock_tea.list_labels.return_value = []
|
|
mock_tea.create_issue.return_value = {"index": 62, "title": "test"}
|
|
mock_tea_cls.return_value = mock_tea
|
|
|
|
runner = CliRunner()
|
|
result = runner.invoke(
|
|
main,
|
|
["--repo", "owner/repo", "--run-id", "1", "--workflow", "release", "--commit", "abc", "--auto-login"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "already configured" in result.output
|