mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
"""Check report publication using synthetic results from the report builder."""
|
|
import importlib.util
|
|
import io
|
|
from contextlib import redirect_stderr, redirect_stdout
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
SCRIPT = Path(__file__).resolve().parents[2] / "tools" / "map_trace_segments.py"
|
|
SPEC = importlib.util.spec_from_file_location("map_trace_segments", SCRIPT)
|
|
TOOL = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(TOOL)
|
|
|
|
|
|
class SegmentReportPublicationTests(unittest.TestCase):
|
|
def setUp(self):
|
|
directory = tempfile.TemporaryDirectory()
|
|
self.addCleanup(directory.cleanup)
|
|
self.root = Path(directory.name)
|
|
self.report = self.root / "segments.json"
|
|
self.report.write_text('{"segments": [], "problems": []}\n')
|
|
|
|
def run_tool(self, fresh, *args):
|
|
stdout, stderr = io.StringIO(), io.StringIO()
|
|
with (
|
|
patch.object(TOOL, "build", return_value=fresh),
|
|
patch.object(TOOL, "ROOT", self.root),
|
|
patch.object(TOOL, "REPORT", self.report),
|
|
patch.object(sys, "argv", [str(SCRIPT), *args]),
|
|
redirect_stdout(stdout),
|
|
redirect_stderr(stderr),
|
|
):
|
|
return TOOL.main(), stdout.getvalue(), stderr.getvalue()
|
|
|
|
def test_bad_input_keeps_existing_report(self):
|
|
old = self.report.read_bytes()
|
|
status, stdout, stderr = self.run_tool({"segments": [], "problems": ["anchor mismatch"]})
|
|
self.assertEqual(status, 1)
|
|
self.assertIn("anchor mismatch", stderr)
|
|
self.assertNotIn("wrote", stdout)
|
|
self.assertEqual(self.report.read_bytes(), old)
|
|
|
|
def test_check_does_not_call_bad_report_up_to_date(self):
|
|
bad = {"segments": [], "problems": ["anchor mismatch"]}
|
|
self.report.write_text(json.dumps(bad))
|
|
status, stdout, stderr = self.run_tool(bad, "--check")
|
|
self.assertEqual(status, 1)
|
|
self.assertIn("anchor mismatch", stderr)
|
|
self.assertNotIn("up to date", stdout)
|
|
|
|
def test_valid_input_publishes_and_verifies_report(self):
|
|
fresh = {"segments": [{"id": 1}], "problems": []}
|
|
status, _, _ = self.run_tool(fresh)
|
|
self.assertEqual(status, 0)
|
|
self.assertEqual(json.loads(self.report.read_text()), fresh)
|
|
status, stdout, _ = self.run_tool(fresh, "--check")
|
|
self.assertEqual(status, 0)
|
|
self.assertIn("up to date", stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|