mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 19:01:54 +00:00
Widens the cache-read lint guard from ``tests/lint/`` to every ``.py`` under ``tests/``. Integration tests show the same compounding cost pattern (one role's compose template / nginx conf / scrape fragment read across many shape tests) that motivated the original lint for lint/. Converts 91 violations across 28 test files: - **Cache (utils.cache.files.read_text)** for project-tracked reads and single-read tempdir fixtures, e.g. test_prometheus.py (32x), test_keycloak_import_flow.py (11x), test_dashboard_oidc.py (3x), test_changelog_md.py (8x), test_package_mirror.py (8x), plus test_logging.py / test_summarize.py / test_version_updater.py / test_get_url_retry.py / test_pull_specific_host.py. - **Cache + UnicodeDecodeError handler** for project-walk scanners that previously used ``errors="replace"`` / ``errors="ignore"`` to defend against non-UTF-8 files (compose volumes, password quoting, template image, mailu dependency, URL reachability). Wraps each read in ``try/except (OSError, UnicodeDecodeError): continue``, which has the same observable effect (skip undecodable files). - **Refactor away the binary heuristic** in test_no_raw_docker.py: ``read_bytes() + b"\x00" not in raw`` becomes ``read_text() / except UnicodeDecodeError``. Same files skipped, with cache benefit. - **Variable extraction** in test_changelog_md.py / test_package_mirror.py for cases where ``self.assertEqual(path.read_text(...), expected)`` exceeded ruff's 88-char limit. Ruff was wrapping the marker onto a line outside the AST span of the read_text call. - **Compute-from-setup** for two test methods that previously read ``self.changelog`` twice (before/after) to verify a no-op: ``before`` is now derived from the known input, dropping the first read. Five ``# nocheck: cache-read`` markers remain, each annotated inline with the reason: idempotency tests where the second read must hit disk to catch non-idempotent regressions, and tests with intermediate ``write_text`` whose subsequent read would return stale cache data. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
184 lines
6.1 KiB
Python
184 lines
6.1 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import unittest.mock
|
|
from pathlib import Path
|
|
|
|
from utils.annotations.summarize import (
|
|
Annotation,
|
|
main,
|
|
parse_log,
|
|
parse_props,
|
|
render_markdown,
|
|
)
|
|
from utils.cache.files import read_text
|
|
|
|
|
|
class TestParseProps(unittest.TestCase):
|
|
def test_single_prop(self) -> None:
|
|
self.assertEqual(parse_props("title=Foo"), {"title": "Foo"})
|
|
|
|
def test_multiple_props(self) -> None:
|
|
result = parse_props("title=My Title,file=foo.py,line=42")
|
|
self.assertEqual(result, {"title": "My Title", "file": "foo.py", "line": "42"})
|
|
|
|
def test_empty_string(self) -> None:
|
|
self.assertEqual(parse_props(""), {})
|
|
|
|
def test_none_like_empty(self) -> None:
|
|
self.assertEqual(parse_props(""), {})
|
|
|
|
|
|
class TestParseLog(unittest.TestCase):
|
|
def _write_log(self, content: str) -> Path:
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
|
|
f.write(content)
|
|
return Path(f.name)
|
|
|
|
def test_parses_warning(self) -> None:
|
|
p = self._write_log("::warning title=T,file=f.py,line=3::bad thing\n")
|
|
result = parse_log(p)
|
|
self.assertEqual(len(result), 1)
|
|
a = result[0]
|
|
self.assertEqual(a.level, "warning")
|
|
self.assertEqual(a.title, "T")
|
|
self.assertEqual(a.file, "f.py")
|
|
self.assertEqual(a.line, 3)
|
|
self.assertEqual(a.message, "bad thing")
|
|
|
|
def test_parses_error(self) -> None:
|
|
p = self._write_log("::error title=Oops::something broke\n")
|
|
result = parse_log(p)
|
|
self.assertEqual(len(result), 1)
|
|
self.assertEqual(result[0].level, "error")
|
|
self.assertEqual(result[0].title, "Oops")
|
|
|
|
def test_parses_notice(self) -> None:
|
|
p = self._write_log("::notice::just info\n")
|
|
result = parse_log(p)
|
|
self.assertEqual(len(result), 1)
|
|
self.assertEqual(result[0].level, "notice")
|
|
self.assertIsNone(result[0].title)
|
|
|
|
def test_ignores_non_annotation_lines(self) -> None:
|
|
p = self._write_log("regular output\nsome other line\n")
|
|
self.assertEqual(parse_log(p), [])
|
|
|
|
def test_mixed_log(self) -> None:
|
|
content = (
|
|
"build started\n"
|
|
"::warning title=W::warn msg\n"
|
|
"some output\n"
|
|
"::error::err msg\n"
|
|
)
|
|
p = self._write_log(content)
|
|
result = parse_log(p)
|
|
self.assertEqual(len(result), 2)
|
|
self.assertEqual(result[0].level, "warning")
|
|
self.assertEqual(result[1].level, "error")
|
|
|
|
def test_multiple_annotations(self) -> None:
|
|
lines = "\n".join(f"::warning::msg {i}" for i in range(15))
|
|
p = self._write_log(lines)
|
|
self.assertEqual(len(parse_log(p)), 15)
|
|
|
|
|
|
class TestRenderMarkdown(unittest.TestCase):
|
|
def test_empty_returns_no_annotations_message(self) -> None:
|
|
md = render_markdown([], "My Title")
|
|
self.assertIn("My Title", md)
|
|
self.assertIn("No annotations found", md)
|
|
|
|
def test_warning_section_present(self) -> None:
|
|
annotations = [Annotation(level="warning", message="watch out", title="T")]
|
|
md = render_markdown(annotations, "Summary")
|
|
self.assertIn("Warnings", md)
|
|
self.assertIn("watch out", md)
|
|
self.assertIn("🟡", md)
|
|
|
|
def test_error_section_present(self) -> None:
|
|
annotations = [Annotation(level="error", message="broken")]
|
|
md = render_markdown(annotations, "Summary")
|
|
self.assertIn("Errors", md)
|
|
self.assertIn("🔴", md)
|
|
|
|
def test_notice_section_present(self) -> None:
|
|
annotations = [Annotation(level="notice", message="fyi")]
|
|
md = render_markdown(annotations, "Summary")
|
|
self.assertIn("Notices", md)
|
|
self.assertIn("🔵", md)
|
|
|
|
def test_file_with_line_shown_as_colon_path(self) -> None:
|
|
annotations = [Annotation(level="warning", message="x", file="a.py", line=7)]
|
|
md = render_markdown(annotations, "S")
|
|
self.assertIn("a.py:7", md)
|
|
|
|
def test_pipe_in_message_escaped(self) -> None:
|
|
annotations = [Annotation(level="warning", message="a|b")]
|
|
md = render_markdown(annotations, "S")
|
|
self.assertIn("a\\|b", md)
|
|
|
|
def test_count_in_heading(self) -> None:
|
|
annotations = [
|
|
Annotation(level="warning", message="w1"),
|
|
Annotation(level="warning", message="w2"),
|
|
]
|
|
md = render_markdown(annotations, "S")
|
|
self.assertIn("Warnings (2)", md)
|
|
|
|
def test_order_error_warning_notice(self) -> None:
|
|
annotations = [
|
|
Annotation(level="notice", message="n"),
|
|
Annotation(level="warning", message="w"),
|
|
Annotation(level="error", message="e"),
|
|
]
|
|
md = render_markdown(annotations, "S")
|
|
error_pos = md.index("Errors")
|
|
warning_pos = md.index("Warnings")
|
|
notice_pos = md.index("Notices")
|
|
self.assertLess(error_pos, warning_pos)
|
|
self.assertLess(warning_pos, notice_pos)
|
|
|
|
|
|
class TestMain(unittest.TestCase):
|
|
def test_writes_to_github_step_summary(self) -> None:
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as log:
|
|
log.write("::warning title=T::msg\n")
|
|
log_path = log.name
|
|
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="w", suffix=".md", delete=False
|
|
) as summary:
|
|
summary_path = summary.name
|
|
|
|
old_argv = sys.argv
|
|
try:
|
|
sys.argv = ["summarize", log_path, "Test Title"]
|
|
with unittest.mock.patch.dict(
|
|
os.environ, {"GITHUB_STEP_SUMMARY": summary_path}
|
|
):
|
|
main()
|
|
finally:
|
|
sys.argv = old_argv
|
|
|
|
content = read_text(summary_path)
|
|
self.assertIn("Test Title", content)
|
|
self.assertIn("msg", content)
|
|
|
|
def test_exits_without_args(self) -> None:
|
|
old_argv = sys.argv
|
|
sys.argv = ["summarize"]
|
|
try:
|
|
with self.assertRaises(SystemExit) as cm:
|
|
main()
|
|
self.assertEqual(cm.exception.code, 1)
|
|
finally:
|
|
sys.argv = old_argv
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|