From 140f60599cedb8fed935211822b634561285a6d1 Mon Sep 17 00:00:00 2001 From: Svilen Stefanov Date: Thu, 12 Nov 2026 21:13:08 +0100 Subject: [PATCH] fix: build the review diff from Core's parsed analysis, not the document load_analysis already parsed both sides with the pinned release's parse_unified_analysis and rolled relations up with its project_relations_to_level, but the diff then read members, file hashes and method hashes from the raw document: file_methods as {file_path, methods} groups and methods_index. Core #571's "v1" document has neither, so once the Action pins a release with it, the review diff fails with "'str' object has no attribute 'get'". The diff now reads only what Core parsed: each component's file_methods, the root's file index and Core's projected relations at every level. The review analyses both commits with the pinned release, so both sides are always in the format it reads, and a format change in Core needs no change here. The core-compatibility job rendered the committed .codeboarding/analysis.json, which after a pin moves can still be in the format of the release before, which the new one refuses. It now runs InstalledCoreTests: tests/fixtures/ analysis holds one analysis as Core 0.15.5 and Core main (9cf50fb9) wrote it, each release reads only its own, and every fixture the pinned one reads must give the same view. Checked on CodeBoarding's own analysis (two baseline syncs, 22 components, depth 1 and 2) and this repo's: output and summary are byte-identical to main's script on the legacy documents with 0.15.5, and to that again on their v1 rewrites with Core main, where main's script fails. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 12 +- scripts/diff_to_mermaid.py | 56 ++-- tests/fixtures/analysis/core-0.15.5.json | 289 +++++++++++++++++++++ tests/fixtures/analysis/core-9cf50fb9.json | 217 ++++++++++++++++ tests/test_action_sync.py | 2 +- tests/test_diff_to_mermaid.py | 191 ++++++++++---- 6 files changed, 688 insertions(+), 79 deletions(-) create mode 100644 tests/fixtures/analysis/core-0.15.5.json create mode 100644 tests/fixtures/analysis/core-9cf50fb9.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a8d6efd3..d9f8887f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,14 +53,10 @@ jobs: printf '{"components": []}\n' > "$ANALYSIS_DIR/analysis.json" scripts/action/install-sync.sh cmp "$ANALYSIS_DIR/analysis.json" "$CHECKOUT_DIR/.codeboarding/analysis.json" - - name: Render with the installed CodeBoarding release - run: >- - python scripts/diff_to_mermaid.py - --base .codeboarding/analysis.json - --head .codeboarding/analysis.json - --out "${RUNNER_TEMP}/diagram.md" - --direction LR - --render-depth 1 + # Not the committed .codeboarding/analysis.json: after a pin moves, that can still be in the + # format of the release before, which the new one refuses. + - name: Read and diff analyses with the installed CodeBoarding release + run: python -m unittest tests.test_diff_to_mermaid.InstalledCoreTests -v lint: runs-on: ubuntu-latest diff --git a/scripts/diff_to_mermaid.py b/scripts/diff_to_mermaid.py index 4d8283d5..7b2f69b0 100644 --- a/scripts/diff_to_mermaid.py +++ b/scripts/diff_to_mermaid.py @@ -20,7 +20,9 @@ analysed code changed, whatever the component diff says: a component can still read as changed when the analysis grouped the same code differently. -Self-contained stdlib. +Stdlib only, apart from the installed CodeBoarding release: both analyses are read through its parser +and never as a document, so whatever format that release writes is read. The review analyses both +commits with it, so both sides are always in that format. """ from __future__ import annotations @@ -63,23 +65,46 @@ def _relation_dict(relation) -> dict: } -def _project_analysis(data: dict, root_analysis, sub_analyses: dict, id_to_name: dict, projector) -> dict: - """Attach Core's global-relation projection to every nested JSON level.""" +def _diff_view(root_analysis, sub_analyses: dict, id_to_name: dict, projector) -> dict: + """The dict the diff reads, built from Core's parsed analysis rather than the document: the component + tree with each component's methods by file, Core's projection of the global relations at every level, + and the content hash of every analysed file and method.""" global_relations = list(root_analysis.components_relations) - def project_level(container: dict, analysis) -> None: + def level(analysis) -> dict: level_ids = {component.component_id for component in analysis.components} - container["components_relations"] = [ - _relation_dict(relation) for relation in projector(global_relations, level_ids, id_to_name) - ] - for component in container.get("components") or []: - component_id = component.get("component_id", "") - sub_analysis = sub_analyses.get(component_id) + components = [] + for component in analysis.components: + view = { + "name": component.name, + "component_id": component.component_id, + "file_methods": [ + {"file_path": group.file_path, "methods": [method.qualified_name for method in group.methods]} + for group in component.file_methods + ], + } + sub_analysis = sub_analyses.get(component.component_id) if sub_analysis is not None: - project_level(component, sub_analysis) - - project_level(data, root_analysis) - return data + view.update(level(sub_analysis)) + components.append(view) + return { + "components": components, + "components_relations": [ + _relation_dict(relation) for relation in projector(global_relations, level_ids, id_to_name) + ], + } + + files = root_analysis.files + return { + **level(root_analysis), + # files[""] only holds pathless methods; it is no file. + "files": {path: {"content_hash": entry.content_hash} for path, entry in files.items() if path}, + "methods_index": { + f"{path}|{method.qualified_name}": {"file_path": path, "content_hash": method.content_hash} + for path, entry in files.items() + for method in entry.methods + }, + } def read_analysis_json(path: Path) -> dict: @@ -101,8 +126,7 @@ def load_analysis(path: Path) -> dict: root_analysis, sub_analyses = parse_unified_analysis(data) except (KeyError, TypeError, ValueError) as exc: sys.exit(f"::error::Could not parse analysis JSON at {path}: {exc}") - return _project_analysis( - data, + return _diff_view( root_analysis, sub_analyses, build_id_to_name_map(root_analysis, sub_analyses), diff --git a/tests/fixtures/analysis/core-0.15.5.json b/tests/fixtures/analysis/core-0.15.5.json new file mode 100644 index 00000000..d122b4f4 --- /dev/null +++ b/tests/fixtures/analysis/core-0.15.5.json @@ -0,0 +1,289 @@ +{ + "metadata": { + "generated_at": "2026-11-12T00:00:00Z", + "source_tree_hash": "", + "repo_name": "pkg", + "depth_level": 2, + "depth_cap": 2, + "file_coverage_summary": { + "total_files": 3, + "analyzed": 3, + "not_analyzed": 0, + "not_analyzed_by_reason": {} + }, + "tree_spec": {} + }, + "description": "fixture", + "files": { + "pkg/a.py": { + "method_keys": [ + "pkg/a.py|pkg.a.run", + "pkg/a.py|pkg.a.helper" + ], + "content_hash": "", + "module_hash": "" + }, + "pkg/b.py": { + "method_keys": [ + "pkg/b.py|pkg.b.save", + "pkg/b.py|~Weird" + ], + "content_hash": "", + "module_hash": "" + }, + "pkg/c.py": { + "method_keys": [ + "pkg/c.py|pkg.c.load" + ], + "content_hash": "", + "module_hash": "" + } + }, + "methods_index": { + "pkg/a.py|pkg.a.run": { + "file_path": "pkg/a.py", + "qualified_name": "pkg.a.run", + "start_line": 1, + "end_line": 5, + "type": "FUNCTION", + "content_hash": "h1" + }, + "pkg/a.py|pkg.a.helper": { + "file_path": "pkg/a.py", + "qualified_name": "pkg.a.helper", + "start_line": 6, + "end_line": 8, + "type": "FUNCTION", + "content_hash": "h2" + }, + "pkg/b.py|pkg.b.save": { + "file_path": "pkg/b.py", + "qualified_name": "pkg.b.save", + "start_line": 1, + "end_line": 4, + "type": "FUNCTION", + "content_hash": "h3" + }, + "pkg/b.py|~Weird": { + "file_path": "pkg/b.py", + "qualified_name": "~Weird", + "start_line": 10, + "end_line": 12, + "type": "FUNCTION", + "content_hash": "h4" + }, + "pkg/c.py|pkg.c.load": { + "file_path": "pkg/c.py", + "qualified_name": "pkg.c.load", + "start_line": 1, + "end_line": 3, + "type": "FUNCTION", + "content_hash": "h5" + } + }, + "components": [ + { + "name": "Core", + "description": "d1", + "key_entities": [], + "source_cluster_ids": [], + "file_methods": [ + { + "file_path": "pkg/a.py", + "methods": [ + "pkg.a.run", + "pkg.a.helper" + ] + }, + { + "file_path": "pkg/b.py", + "methods": [ + "pkg.b.save", + "~Weird" + ] + } + ], + "component_id": "1", + "can_expand": true, + "components": [ + { + "name": "Run", + "description": "", + "key_entities": [], + "source_cluster_ids": [], + "file_methods": [ + { + "file_path": "pkg/a.py", + "methods": [ + "pkg.a.run", + "pkg.a.helper" + ] + } + ], + "component_id": "1.1", + "can_expand": false, + "components": [], + "components_relations": [] + }, + { + "name": "Store", + "description": "", + "key_entities": [], + "source_cluster_ids": [], + "file_methods": [ + { + "file_path": "pkg/b.py", + "methods": [ + "pkg.b.save", + "~Weird" + ] + } + ], + "component_id": "1.2", + "can_expand": false, + "components": [], + "components_relations": [] + } + ], + "components_relations": [ + { + "relation": "saves", + "src_name": "Run", + "dst_name": "Store", + "evidence": "", + "key_edges": [], + "src_id": "1.1", + "dst_id": "1.2", + "is_static": false, + "all_edges": [ + { + "source": "pkg/a.py|pkg.a.run", + "target": "pkg/b.py|pkg.b.save", + "call_sites": [ + { + "line": 3, + "column": 8 + } + ], + "description": "" + }, + { + "source": "pkg/a.py|pkg.a.helper", + "target": "pkg/b.py|pkg.b.save", + "call_sites": [], + "description": "" + } + ] + } + ] + }, + { + "name": "Also", + "description": "", + "key_entities": [], + "source_cluster_ids": [], + "file_methods": [ + { + "file_path": "pkg/c.py", + "methods": [ + "pkg.c.load" + ] + } + ], + "component_id": "2", + "can_expand": false, + "components": [], + "components_relations": [] + } + ], + "components_relations": [ + { + "relation": "saves", + "src_name": "Run", + "dst_name": "Store", + "evidence": "", + "key_edges": [], + "src_id": "1.1", + "dst_id": "1.2", + "is_static": false, + "all_edges": [ + { + "source": "pkg/a.py|pkg.a.run", + "target": "pkg/b.py|pkg.b.save", + "call_sites": [ + { + "line": 3, + "column": 8 + } + ], + "description": "" + }, + { + "source": "pkg/a.py|pkg.a.helper", + "target": "pkg/b.py|pkg.b.save", + "call_sites": [], + "description": "" + } + ] + }, + { + "relation": "uses", + "src_name": "Run", + "dst_name": "Also", + "evidence": "", + "key_edges": [], + "src_id": "1.1", + "dst_id": "2", + "is_static": false, + "all_edges": [ + { + "source": "pkg/a.py|pkg.a.run", + "target": "pkg/c.py|pkg.c.load", + "call_sites": [ + { + "line": 4, + "column": 4 + } + ], + "description": "" + } + ] + }, + { + "relation": "loads", + "src_name": "Store", + "dst_name": "Also", + "evidence": "", + "key_edges": [], + "src_id": "1.2", + "dst_id": "2", + "is_static": false, + "all_edges": [ + { + "source": "pkg/b.py|~Weird", + "target": "pkg/c.py|pkg.c.load", + "call_sites": [], + "description": "" + } + ] + }, + { + "relation": "reads", + "src_name": "Also", + "dst_name": "Store", + "evidence": "", + "key_edges": [], + "src_id": "2", + "dst_id": "1.2", + "is_static": false, + "all_edges": [ + { + "source": "pkg/c.py|pkg.c.load", + "target": "pkg/b.py|pkg.b.save", + "call_sites": [], + "description": "" + } + ] + } + ] +} diff --git a/tests/fixtures/analysis/core-9cf50fb9.json b/tests/fixtures/analysis/core-9cf50fb9.json new file mode 100644 index 00000000..4ddba2eb --- /dev/null +++ b/tests/fixtures/analysis/core-9cf50fb9.json @@ -0,0 +1,217 @@ +{ + "metadata": { + "generated_at": "2026-11-12T00:00:00Z", + "source_tree_hash": "", + "repo_name": "pkg", + "depth_level": 2, + "depth_cap": 2, + "file_coverage_summary": { + "total_files": 3, + "analyzed": 3, + "not_analyzed": 0, + "not_analyzed_by_reason": {} + }, + "tree_spec": {}, + "format_version": "v1" + }, + "description": "fixture", + "files": { + "pkg/a.py": { + "content_hash": "", + "module_hash": "", + "methods": { + "pkg.a.run": [ + 1, + 5, + "FUNCTION", + "h1", + "W2ZvP4dicxdV" + ], + "pkg.a.helper": [ + 6, + 8, + "FUNCTION", + "h2", + "fHTj2iSkCxvz" + ] + } + }, + "pkg/b.py": { + "content_hash": "", + "module_hash": "", + "methods": { + "pkg.b.save": [ + 1, + 4, + "FUNCTION", + "h3", + "HXVcn6KDWNds" + ], + "~Weird": [ + 10, + 12, + "FUNCTION", + "h4", + "VHMX26c_wkXu" + ] + } + }, + "pkg/c.py": { + "content_hash": "", + "module_hash": "", + "methods": { + "pkg.c.load": [ + 1, + 3, + "FUNCTION", + "h5", + "ai1syDPa_D53" + ] + } + } + }, + "components": [ + { + "name": "Core", + "component_id": "1", + "description": "d1", + "key_entities": [], + "source_cluster_ids": [], + "can_expand": true, + "components": [ + { + "name": "Run", + "component_id": "1.1", + "description": "", + "key_entities": [], + "source_cluster_ids": [], + "can_expand": false, + "file_methods": [ + "W2ZvP4dicxdV", + "fHTj2iSkCxvz" + ], + "components": [], + "components_relations": [] + }, + { + "name": "Store", + "component_id": "1.2", + "description": "", + "key_entities": [], + "source_cluster_ids": [], + "can_expand": false, + "file_methods": [ + "HXVcn6KDWNds", + "VHMX26c_wkXu" + ], + "components": [], + "components_relations": [] + } + ], + "components_relations": [ + { + "relation": "saves", + "src_name": "Run", + "dst_name": "Store", + "evidence": "", + "src_id": "1.1", + "dst_id": "1.2", + "is_static": false, + "key_edges": [] + } + ] + }, + { + "name": "Also", + "component_id": "2", + "description": "", + "key_entities": [], + "source_cluster_ids": [], + "can_expand": false, + "file_methods": [ + "ai1syDPa_D53" + ], + "components": [], + "components_relations": [] + } + ], + "components_relations": [ + { + "relation": "saves", + "src_name": "Run", + "dst_name": "Store", + "evidence": "", + "src_id": "1.1", + "dst_id": "1.2", + "is_static": false, + "key_edges": [] + }, + { + "relation": "uses", + "src_name": "Run", + "dst_name": "Also", + "evidence": "", + "src_id": "1.1", + "dst_id": "2", + "is_static": false, + "key_edges": [] + }, + { + "relation": "loads", + "src_name": "Store", + "dst_name": "Also", + "evidence": "", + "src_id": "1.2", + "dst_id": "2", + "is_static": false, + "key_edges": [] + }, + { + "relation": "reads", + "src_name": "Also", + "dst_name": "Store", + "evidence": "", + "src_id": "2", + "dst_id": "1.2", + "is_static": false, + "key_edges": [] + } + ], + "edges": [ + [ + "fHTj2iSkCxvz", + "HXVcn6KDWNds", + [] + ], + [ + "W2ZvP4dicxdV", + "HXVcn6KDWNds", + [ + [ + 3, + 8 + ] + ] + ], + [ + "W2ZvP4dicxdV", + "ai1syDPa_D53", + [ + [ + 4, + 4 + ] + ] + ], + [ + "VHMX26c_wkXu", + "ai1syDPa_D53", + [] + ], + [ + "ai1syDPa_D53", + "HXVcn6KDWNds", + [] + ] + ] +} diff --git a/tests/test_action_sync.py b/tests/test_action_sync.py index 872036dc..30ed2462 100644 --- a/tests/test_action_sync.py +++ b/tests/test_action_sync.py @@ -317,7 +317,7 @@ def test_empty_review_is_successful(self) -> None: ) (fake_core / "diagram_analysis" / "analysis_json.py").write_text( "from types import SimpleNamespace\n" - "def parse_unified_analysis(data): return SimpleNamespace(components=[], components_relations=[]), {}\n" + "def parse_unified_analysis(data): return SimpleNamespace(components=[], components_relations=[], files={}), {}\n" "def build_id_to_name_map(*args): return {}\n", encoding="utf-8", ) diff --git a/tests/test_diff_to_mermaid.py b/tests/test_diff_to_mermaid.py index 0624787b..48778e05 100644 --- a/tests/test_diff_to_mermaid.py +++ b/tests/test_diff_to_mermaid.py @@ -6,6 +6,7 @@ import sys import tempfile import unittest +from importlib import metadata from pathlib import Path from types import ModuleType, SimpleNamespace from unittest.mock import patch @@ -27,6 +28,15 @@ def comp(name, files=None, cid=None, subs=None, subrels=None): return c +def core_component(cid, name, files=None): + """A component as Core's parser gives it, with the attributes the loader reads.""" + groups = [ + SimpleNamespace(file_path=path, methods=[SimpleNamespace(qualified_name=name) for name in names]) + for path, names in (files or {}).items() + ] + return SimpleNamespace(component_id=cid, name=name, file_methods=groups) + + def rel(src, dst, label="calls"): return {"src_name": src, "dst_name": dst, "src_id": src, "dst_id": dst, "relation": label} @@ -75,14 +85,12 @@ def test_the_count_rides_on_the_machine_readable_summary(self): class TestDiff(unittest.TestCase): def test_core_loader_projects_global_relation_into_rendered_mermaid(self): - root_a = SimpleNamespace(component_id="1") - root_b = SimpleNamespace(component_id="2") - child_a = SimpleNamespace(component_id="1.1") - child_b = SimpleNamespace(component_id="2.1") + root_a, root_b = core_component("1", "Root A"), core_component("2", "Root B") + child_a, child_b = core_component("1.1", "Child A"), core_component("2.1", "Child B") def parse_unified_analysis(data): relations = [SimpleNamespace(**relation) for relation in data.get("components_relations", [])] - root = SimpleNamespace(components=[root_a, root_b], components_relations=relations) + root = SimpleNamespace(components=[root_a, root_b], components_relations=relations, files={}) return root, { "1": SimpleNamespace(components=[child_a]), "2": SimpleNamespace(components=[child_b]), @@ -124,34 +132,13 @@ def project_relations_to_level(relations, level_ids, id_to_name): "output_generators": ModuleType("output_generators"), "output_generators.rendering": rendering, } - analysis = { - "components": [ - {"name": "Root A", "component_id": "1", "components": [{"name": "Child A", "component_id": "1.1"}]}, - {"name": "Root B", "component_id": "2", "components": [{"name": "Child B", "component_id": "2.1"}]}, - ], - "components_relations": [], - } + relation = {"relation": "calls", "src_name": "Child A", "dst_name": "Child B", "src_id": "1.1", "dst_id": "2.1"} with tempfile.TemporaryDirectory() as tmp, patch.dict(sys.modules, modules): base_path = Path(tmp) / "base.json" head_path = Path(tmp) / "head.json" - base_path.write_text(json.dumps(analysis)) - head_path.write_text( - json.dumps( - { - **analysis, - "components_relations": [ - { - "relation": "calls", - "src_name": "Child A", - "dst_name": "Child B", - "src_id": "1.1", - "dst_id": "2.1", - } - ], - } - ) - ) + base_path.write_text(json.dumps({"components_relations": []})) + head_path.write_text(json.dumps({"components_relations": [relation]})) diff = dm.build_diff(dm.load_analysis(base_path), dm.load_analysis(head_path)) text, meta = dm.render_mermaid(diff) @@ -159,11 +146,11 @@ def project_relations_to_level(relations, level_ids, id_to_name): self.assertEqual(meta["n_edges"], 1) self.assertTrue(meta["changed"]) - def test_projects_global_relations_at_every_component_level(self): - child = SimpleNamespace(component_id="1.1") - peer = SimpleNamespace(component_id="1.2") - root_a = SimpleNamespace(component_id="1") - root_b = SimpleNamespace(component_id="2") + def test_reads_members_hashes_and_relations_from_cores_objects(self): + child = core_component("1.1", "Child", {"a.py": ["a.run"]}) + peer = core_component("1.2", "Peer", {"b.py": ["b.save"]}) + root_a = core_component("1", "Root A", {"a.py": ["a.run"], "b.py": ["b.save"]}) + root_b = core_component("2", "Root B") global_relation = SimpleNamespace( relation="calls", src_name="Child", @@ -178,25 +165,20 @@ def test_projects_global_relations_at_every_component_level(self): src_id="1.1", dst_id="1.2", ) + + def method(name, content_hash): + return SimpleNamespace(qualified_name=name, content_hash=content_hash) + root_analysis = SimpleNamespace( - components=[root_a, root_b], components_relations=[global_relation, sibling_relation] + components=[root_a, root_b], + components_relations=[global_relation, sibling_relation], + files={ + "a.py": SimpleNamespace(content_hash="fa", methods=[method("a.run", "ma")]), + "b.py": SimpleNamespace(content_hash="fb", methods=[method("b.save", "mb")]), + "": SimpleNamespace(content_hash="", methods=[method("loose", "ml")]), + }, ) sub_analysis = SimpleNamespace(components=[child, peer]) - data = { - "components": [ - { - "name": "Root A", - "component_id": "1", - "components": [ - {"name": "Child", "component_id": "1.1"}, - {"name": "Peer", "component_id": "1.2"}, - ], - "components_relations": [{"relation": "stale"}], - }, - {"name": "Root B", "component_id": "2"}, - ], - "components_relations": [], - } def projector(relations, level_ids, _id_to_name): self.assertEqual(relations, [global_relation, sibling_relation]) @@ -214,18 +196,35 @@ def projector(relations, level_ids, _id_to_name): return [sibling_relation] self.fail(f"unexpected component level: {level_ids}") - projected = dm._project_analysis( - data, + view = dm._diff_view( root_analysis, {"1": sub_analysis}, {"1": "Root A", "2": "Root B", "1.1": "Child", "1.2": "Peer"}, projector, ) - self.assertEqual(projected["components_relations"][0]["src_id"], "1") - nested_relations = projected["components"][0]["components_relations"] + self.assertEqual(view["components_relations"][0]["src_id"], "1") + nested_relations = view["components"][0]["components_relations"] self.assertEqual(nested_relations[0]["src_id"], "1.1") self.assertEqual(nested_relations[0]["dst_id"], "1.2") + self.assertEqual( + view["components"][0]["file_methods"], + [{"file_path": "a.py", "methods": ["a.run"]}, {"file_path": "b.py", "methods": ["b.save"]}], + ) + self.assertEqual( + view["components"][0]["components"][1]["file_methods"], [{"file_path": "b.py", "methods": ["b.save"]}] + ) + self.assertNotIn("components", view["components"][1]) + # files[""] holds pathless methods, which still count, but it is no file. + self.assertEqual(view["files"], {"a.py": {"content_hash": "fa"}, "b.py": {"content_hash": "fb"}}) + self.assertEqual( + view["methods_index"], + { + "a.py|a.run": {"file_path": "a.py", "content_hash": "ma"}, + "b.py|b.save": {"file_path": "b.py", "content_hash": "mb"}, + "|loose": {"file_path": "", "content_hash": "ml"}, + }, + ) def test_added_modified_deleted_unchanged(self): base = {"components": [comp("A", {"a.py": ["f"]}), comp("B"), comp("D")], "components_relations": []} @@ -531,5 +530,89 @@ def test_no_edge_labels(self): self.assertNotIn(' -- "', text) +def _core_installed() -> bool: + try: + metadata.version("codeboarding") + except metadata.PackageNotFoundError: + return False + return True + + +@unittest.skipUnless(_core_installed(), "the pinned CodeBoarding release is not installed") +class InstalledCoreTests(unittest.TestCase): + """The loader against the release action.yml pins. It runs in the `core-compatibility` CI job. + + tests/fixtures/analysis holds one analysis as two Core releases wrote it: core-0.15.5.json and + core-9cf50fb9.json (main after #571). Each release reads only its own, so the pinned one must read at + least one, and every one it reads must give the same view. A pin whose release reads none needs that + analysis written by it, which is the point: the diff has not been checked against its parser.""" + + def test_the_pinned_release_reads_an_analysis_it_wrote_into_the_diffs_view(self) -> None: + components_relations = [ + {"relation": "uses", "src_name": "Core", "dst_name": "Also", "src_id": "1", "dst_id": "2"}, + {"relation": "reads", "src_name": "Also", "dst_name": "Core", "src_id": "2", "dst_id": "1"}, + ] + expected = { + "components": [ + { + "name": "Core", + "component_id": "1", + "file_methods": [ + {"file_path": "pkg/a.py", "methods": ["pkg.a.run", "pkg.a.helper"]}, + {"file_path": "pkg/b.py", "methods": ["pkg.b.save", "~Weird"]}, + ], + "components": [ + { + "name": "Run", + "component_id": "1.1", + "file_methods": [{"file_path": "pkg/a.py", "methods": ["pkg.a.run", "pkg.a.helper"]}], + }, + { + "name": "Store", + "component_id": "1.2", + "file_methods": [{"file_path": "pkg/b.py", "methods": ["pkg.b.save", "~Weird"]}], + }, + ], + "components_relations": [ + {"relation": "saves", "src_name": "Run", "dst_name": "Store", "src_id": "1.1", "dst_id": "1.2"} + ], + }, + { + "name": "Also", + "component_id": "2", + "file_methods": [{"file_path": "pkg/c.py", "methods": ["pkg.c.load"]}], + }, + ], + "components_relations": components_relations, + "files": {path: {"content_hash": ""} for path in ("pkg/a.py", "pkg/b.py", "pkg/c.py")}, + "methods_index": { + f"{path}|{name}": {"file_path": path, "content_hash": content_hash} + for path, name, content_hash in ( + ("pkg/a.py", "pkg.a.run", "h1"), + ("pkg/a.py", "pkg.a.helper", "h2"), + ("pkg/b.py", "pkg.b.save", "h3"), + ("pkg/b.py", "~Weird", "h4"), + ("pkg/c.py", "pkg.c.load", "h5"), + ) + }, + } + read = [] + for fixture in sorted((Path(__file__).parent / "fixtures" / "analysis").glob("*.json")): + try: + view = dm.load_analysis(fixture) + except SystemExit as refused: + if not str(refused.code).startswith("::error::Could not parse"): + raise + continue # another release's format + read.append(fixture.name) + with self.subTest(fixture=fixture.name): + self.assertEqual(view, expected) + diff = dm.build_diff(view, view) + self.assertFalse(dm.render_mermaid(diff)[1]["changed"]) + self.assertTrue( + read, "the pinned release reads none of tests/fixtures/analysis; add the analysis as it writes it" + ) + + if __name__ == "__main__": unittest.main()