Files
computer-playbook/tests/unit/cli/gha/test_annotations.py
T
kevinveenbirkenbachandClaude Sonnet 4.6 0ce6b1347c refactor(annotations): move to utils/annotations, add step summary support
- Move utils/gha/annotations.py → utils/annotations/message.py
- Move scripts/gha/summarize_annotations.py → utils/annotations/summarize.py
- Keep utils/gha/annotations.py as compatibility shim
- Update all imports across codebase to utils.annotations.message
- Wire tee + summary step into test-code-lint, unit, and integration workflows
- Add unit tests for utils/annotations/summarize (20 tests)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-08 14:05:24 +02:00

105 lines
4.0 KiB
Python

from __future__ import annotations
import os
import unittest
from io import StringIO
from unittest.mock import patch
from utils.annotations.message import error, error_each, notice, warning, warning_each
GHA_ENV = {"GITHUB_ACTIONS": "true"}
NO_GHA_ENV = {"GITHUB_ACTIONS": ""}
class TestAnnotationsInGHA(unittest.TestCase):
"""Tests for GHA annotation format (GITHUB_ACTIONS=true)."""
def _capture(self, fn, *args, **kwargs) -> str:
with patch.dict(os.environ, GHA_ENV):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
fn(*args, **kwargs)
return mock_out.getvalue().strip()
def test_warning_no_props(self) -> None:
out = self._capture(warning, "something went wrong")
self.assertEqual(out, "::warning::something went wrong")
def test_warning_with_title(self) -> None:
out = self._capture(warning, "msg", title="My Title")
self.assertEqual(out, "::warning title=My Title::msg")
def test_warning_with_file_and_line(self) -> None:
out = self._capture(warning, "msg", file="foo.py", line=42)
self.assertEqual(out, "::warning file=foo.py,line=42::msg")
def test_error_with_title(self) -> None:
out = self._capture(error, "bad", title="Oops")
self.assertEqual(out, "::error title=Oops::bad")
def test_notice_no_props(self) -> None:
out = self._capture(notice, "info")
self.assertEqual(out, "::notice::info")
def test_warning_each_emits_one_per_item(self) -> None:
with patch.dict(os.environ, GHA_ENV):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
warning_each(["alpha", "beta", "gamma"], title="T")
lines = mock_out.getvalue().splitlines()
self.assertEqual(len(lines), 3)
self.assertIn("alpha", lines[0])
self.assertIn("beta", lines[1])
self.assertIn("gamma", lines[2])
def test_error_each_emits_one_per_item(self) -> None:
with patch.dict(os.environ, GHA_ENV):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
error_each(["x", "y"], title="E")
lines = mock_out.getvalue().splitlines()
self.assertEqual(len(lines), 2)
self.assertTrue(all("::error" in line for line in lines))
def test_warning_each_empty(self) -> None:
with patch.dict(os.environ, GHA_ENV):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
warning_each([])
self.assertEqual(mock_out.getvalue(), "")
class TestAnnotationsPlainText(unittest.TestCase):
"""Tests for plain text format (no GITHUB_ACTIONS)."""
def _capture(self, fn, *args, **kwargs) -> str:
with patch.dict(os.environ, NO_GHA_ENV):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
fn(*args, **kwargs)
return mock_out.getvalue().strip()
def test_warning_no_props(self) -> None:
out = self._capture(warning, "something went wrong")
self.assertEqual(out, "[WARNING]: something went wrong")
def test_warning_with_title(self) -> None:
out = self._capture(warning, "msg", title="My Title")
self.assertEqual(out, "[WARNING] My Title: msg")
def test_error_with_title(self) -> None:
out = self._capture(error, "bad", title="Oops")
self.assertEqual(out, "[ERROR] Oops: bad")
def test_notice_no_props(self) -> None:
out = self._capture(notice, "info")
self.assertEqual(out, "[NOTICE]: info")
def test_warning_each_emits_one_per_item(self) -> None:
with patch.dict(os.environ, NO_GHA_ENV):
with patch("sys.stdout", new_callable=StringIO) as mock_out:
warning_each(["alpha", "beta"], title="T")
lines = mock_out.getvalue().splitlines()
self.assertEqual(len(lines), 2)
self.assertIn("alpha", lines[0])
self.assertIn("beta", lines[1])
if __name__ == "__main__":
unittest.main()