|
| 1 | +"""#4449: a symbol must not take a same-stem sibling's file id. |
| 2 | +
|
| 3 | +``extract()`` renames every file's OLD file-level id (its path WITH the extension, |
| 4 | +``esquema_x_sql`` for ``esquema/x.sql``) to the canonical one (``esquema_x``). A symbol of |
| 5 | +another file can spell the same string: ``const sql`` in ``x.mjs`` is minted |
| 6 | +``{stem}_{name}`` = ``esquema_x_sql``. The rename used to hit it too, so the symbol took |
| 7 | +the file node's id, was salted next to it, and dedup kept one of the two: the file node |
| 8 | +of ``x.mjs`` ended up labelled ``sql`` and ``graphify explain "x.mjs"`` found nothing. |
| 9 | +Edges and calls that named the symbol followed the wrong rename. |
| 10 | +
|
| 11 | +These tests pin the cause (a key is renamed only for the file that minted it, wherever |
| 12 | +another file's own symbol spells it) and the controls that must not move. |
| 13 | +""" |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from graphify.build import build |
| 21 | +from graphify.extract import extract, extract_js |
| 22 | + |
| 23 | +ISSUE_MJS = ( |
| 24 | + 'import postgres from "postgres";\n' |
| 25 | + "const sql = postgres(process.env.U);\n" |
| 26 | + "async function prueba(){ return sql`select 1` }\n" |
| 27 | +) |
| 28 | +SQL = "create table t(a int);\n" |
| 29 | + |
| 30 | + |
| 31 | +def _write(root: Path, rel: str, text: str) -> Path: |
| 32 | + path = root / rel |
| 33 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 34 | + path.write_text(text, encoding="utf-8") |
| 35 | + return path |
| 36 | + |
| 37 | + |
| 38 | +def _run(root: Path, files: dict[str, str], monkeypatch, *, absolute: bool = False) -> dict: |
| 39 | + """Write ``files`` under ``root`` and run extract() on them, relative or absolute.""" |
| 40 | + for rel, text in files.items(): |
| 41 | + _write(root, rel, text) |
| 42 | + if absolute: |
| 43 | + return extract([root / rel for rel in files], root=root, cache_root=root / "c") |
| 44 | + monkeypatch.chdir(root) |
| 45 | + return extract([Path(rel) for rel in files], root=Path("."), cache_root=root / "c") |
| 46 | + |
| 47 | + |
| 48 | +def _node(result: dict, label: str, source_file: str) -> dict: |
| 49 | + found = [n for n in result["nodes"] if n.get("label") == label and n.get("source_file") == source_file] |
| 50 | + assert len(found) == 1, f"expected one node {label!r} from {source_file}; got {found}" |
| 51 | + return found[0] |
| 52 | + |
| 53 | + |
| 54 | +def _edges(result: dict, relation: str) -> list[tuple[str, str]]: |
| 55 | + return [(e["source"], e["target"]) for e in result["edges"] if e.get("relation") == relation] |
| 56 | + |
| 57 | + |
| 58 | +def _assert_unique_ids(result: dict) -> None: |
| 59 | + ids = [n["id"] for n in result["nodes"]] |
| 60 | + assert len(ids) == len(set(ids)), f"an id is used by two nodes: {sorted(i for i in ids if ids.count(i) > 1)}" |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize("absolute", [False, True], ids=["relative", "absolute"]) |
| 64 | +def test_variable_named_like_the_sibling_extension_keeps_its_own_node(tmp_path, monkeypatch, absolute): |
| 65 | + """The issue: x.mjs with ``const sql`` next to x.sql. Both nodes survive, apart.""" |
| 66 | + result = _run(tmp_path, {"esquema/x.mjs": ISSUE_MJS, "esquema/x.sql": SQL}, monkeypatch, absolute=absolute) |
| 67 | + |
| 68 | + file_node = _node(result, "x.mjs", "esquema/x.mjs") |
| 69 | + variable = _node(result, "sql", "esquema/x.mjs") |
| 70 | + sql_file = _node(result, "x.sql", "esquema/x.sql") |
| 71 | + assert len({file_node["id"], variable["id"], sql_file["id"]}) == 3 |
| 72 | + _assert_unique_ids(result) |
| 73 | + |
| 74 | + |
| 75 | +def test_the_file_node_is_still_found_by_label_after_build(tmp_path, monkeypatch): |
| 76 | + """What ``graphify explain "x.mjs"`` needs: build() must not drop the file node.""" |
| 77 | + result = _run(tmp_path, {"esquema/x.mjs": ISSUE_MJS, "esquema/x.sql": SQL}, monkeypatch) |
| 78 | + labels = {d.get("label") for _, d in build([result]).nodes(data=True)} |
| 79 | + assert {"x.mjs", "sql", "x.sql"} <= labels |
| 80 | + |
| 81 | + |
| 82 | +def test_edges_follow_the_right_node_when_x_mjs_also_imports_x_sql(tmp_path, monkeypatch): |
| 83 | + """One id string, three meanings: the import (stamped with target_file) ends on the |
| 84 | + x.sql FILE node; ``contains`` and ``calls`` end on the ``sql`` VARIABLE; no self-loop.""" |
| 85 | + mjs = 'import raw from "./x.sql";\n' + ISSUE_MJS |
| 86 | + result = _run(tmp_path, {"esquema/x.mjs": mjs, "esquema/x.sql": SQL}, monkeypatch) |
| 87 | + file_node = _node(result, "x.mjs", "esquema/x.mjs")["id"] |
| 88 | + variable = _node(result, "sql", "esquema/x.mjs")["id"] |
| 89 | + sql_file = _node(result, "x.sql", "esquema/x.sql")["id"] |
| 90 | + prueba = _node(result, "prueba()", "esquema/x.mjs")["id"] |
| 91 | + |
| 92 | + assert (file_node, sql_file) in _edges(result, "imports_from") |
| 93 | + assert (file_node, variable) in _edges(result, "contains") |
| 94 | + assert (prueba, variable) in _edges(result, "calls") |
| 95 | + assert not [e for e in result["edges"] if e["source"] == e["target"]] |
| 96 | + |
| 97 | + |
| 98 | +def test_a_sibling_without_nodes_still_keeps_the_variable_apart(tmp_path, monkeypatch): |
| 99 | + """x.ts + x.css: x.css yields no nodes, so nothing is salted. ``css`` still must not |
| 100 | + take x.ts's file id. (collect_files skips .css; a Python caller passes it explicitly.)""" |
| 101 | + ts = "function makeCss(){ return 1 }\nexport const css = makeCss();\nexport function g(){ return css }\n" |
| 102 | + result = _run(tmp_path, {"web/x.ts": ts, "web/x.css": "a { color: red }\n"}, monkeypatch) |
| 103 | + assert _node(result, "x.ts", "web/x.ts")["id"] != _node(result, "css", "web/x.ts")["id"] |
| 104 | + _assert_unique_ids(result) |
| 105 | + |
| 106 | + |
| 107 | +def test_a_variable_keeps_the_id_the_extractor_gave_it_next_to_a_js_map(tmp_path, monkeypatch): |
| 108 | + """x.js + x.js.map: ``js_map`` was renamed to x.js.map's file id (a node that does not exist).""" |
| 109 | + js = "function mk(){ return 1 }\nconst js_map = mk();\nfunction g(){ return js_map }\n" |
| 110 | + result = _run(tmp_path, {"web/x.js": js, "web/x.js.map": '{"version":3}\n'}, monkeypatch) |
| 111 | + minted = {n["label"]: n["id"] for n in extract_js(Path("web/x.js"))["nodes"]} |
| 112 | + assert _node(result, "js_map", "web/x.js")["id"] == minted["js_map"] |
| 113 | + |
| 114 | + |
| 115 | +def test_a_python_function_named_like_the_sibling_extension_keeps_its_calls(tmp_path, monkeypatch): |
| 116 | + """def sql() in x.py next to x.sql: the function survives and its call edge starts at it, |
| 117 | + not at the file node (a caller id that followed the file-level rename).""" |
| 118 | + files = { |
| 119 | + "pyfn/__init__.py": "", |
| 120 | + "pyfn/util.py": "def helper():\n return 1\n", |
| 121 | + "pyfn/x.py": "from pyfn.util import helper\n\n\ndef sql():\n return helper()\n", |
| 122 | + "pyfn/x.sql": SQL, |
| 123 | + } |
| 124 | + result = _run(tmp_path, files, monkeypatch) |
| 125 | + function = _node(result, "sql()", "pyfn/x.py")["id"] |
| 126 | + file_node = _node(result, "x.py", "pyfn/x.py")["id"] |
| 127 | + helper = _node(result, "helper()", "pyfn/util.py")["id"] |
| 128 | + assert function != file_node |
| 129 | + assert (function, helper) in _edges(result, "calls") |
| 130 | + assert (file_node, function) in _edges(result, "contains") |
| 131 | + _assert_unique_ids(result) |
| 132 | + |
| 133 | + |
| 134 | +def test_an_unstamped_import_on_a_contested_key_ends_where_it_ends_without_the_variable(tmp_path, monkeypatch): |
| 135 | + """Pascal ``uses util`` makes an edge with no target_file to util.pas's old file id. A JS |
| 136 | + variable ``pas`` in util.js spells that id. The edge must end exactly where the same |
| 137 | + edge ends when no such variable exists, never on the variable.""" |
| 138 | + pas = {"uses/util.pas": "unit util;\ninterface\nimplementation\nend.\n", |
| 139 | + "uses/main.pas": "unit main;\ninterface\nuses util;\nimplementation\nend.\n"} |
| 140 | + contested = _run(tmp_path / "contested", {**pas, "uses/util.js": "function mk(){ return 1 }\nconst pas = mk();\n"}, monkeypatch) |
| 141 | + control = _run(tmp_path / "control", {**pas, "uses/util.js": "function mk(){ return 1 }\nconst other = mk();\n"}, monkeypatch) |
| 142 | + |
| 143 | + (target,) = [t for s, t in _edges(contested, "imports") if s == _node(contested, "main", "uses/main.pas")["id"]] |
| 144 | + (control_target,) = [t for s, t in _edges(control, "imports") if s == _node(control, "main", "uses/main.pas")["id"]] |
| 145 | + assert target == control_target |
| 146 | + assert target != _node(contested, "pas", "uses/util.js")["id"] |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize("name,mjs,expect", [ |
| 150 | + ("other name", 'import postgres from "postgres";\nconst db = postgres(1);\nfunction prueba(){ return db }\n', |
| 151 | + {"x.mjs": "esquema_x_mjs_esquema_x", "db": "esquema_x_db", "x.sql": "esquema_x_sql_esquema_x"}), |
| 152 | + ("literal", "const sql = 1;\nfunction prueba(){ return sql }\n", |
| 153 | + {"x.mjs": "esquema_x_mjs_esquema_x", "x.sql": "esquema_x_sql_esquema_x"}), |
| 154 | +]) |
| 155 | +def test_controls_with_a_sibling_keep_todays_ids(tmp_path, monkeypatch, name, mjs, expect): |
| 156 | + result = _run(tmp_path, {"esquema/x.mjs": mjs, "esquema/x.sql": SQL}, monkeypatch) |
| 157 | + got = {n["label"]: n["id"] for n in result["nodes"] if n["label"] in expect} |
| 158 | + assert got == expect, name |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.parametrize("sibling", [None, "esquema/y.sql"], ids=["no sibling", "other-stem sibling"]) |
| 162 | +def test_the_variable_beside_no_same_stem_sibling_keeps_todays_ids(tmp_path, monkeypatch, sibling): |
| 163 | + files = {"esquema/x.mjs": 'import postgres from "postgres";\nconst sql = postgres(1);\nfunction prueba(){ return sql }\n'} |
| 164 | + if sibling: |
| 165 | + files[sibling] = SQL |
| 166 | + result = _run(tmp_path, files, monkeypatch) |
| 167 | + assert _node(result, "x.mjs", "esquema/x.mjs")["id"] == "esquema_x" |
| 168 | + assert _node(result, "sql", "esquema/x.mjs")["id"] == "esquema_x_sql" |
| 169 | + |
| 170 | + |
| 171 | +def test_a_stub_from_a_different_stem_keeps_todays_ids(tmp_path, monkeypatch): |
| 172 | + """A Lazarus package names a unit with the unit file's old id. Its stem differs |
| 173 | + from the unit's, so the key is not contested and nothing moves.""" |
| 174 | + lpk = ('<?xml version="1.0"?>\n<CONFIG><Package Version="5"><Name Value="P"/><Files Count="1"><Item1>' |
| 175 | + '<Filename Value="b.pas"/><UnitName Value="b"/></Item1></Files></Package></CONFIG>\n') |
| 176 | + result = _run(tmp_path, {"lazarus/a.lpk": lpk, "lazarus/b.pas": "unit b;\ninterface\nimplementation\nend.\n"}, monkeypatch) |
| 177 | + assert _node(result, "b", "lazarus/a.lpk")["id"] == "lazarus_a_lpk_lazarus_b" |
| 178 | + assert _node(result, "b.pas", "lazarus/b.pas")["id"] == "lazarus_b_pas_lazarus_b" |
| 179 | + |
| 180 | + |
| 181 | +def test_a_swift_type_named_like_the_sibling_extension_keeps_both_file_nodes(tmp_path, monkeypatch): |
| 182 | + """struct sql and ``extension sql`` in x.swift next to x.sql. The recorded extension nid must |
| 183 | + follow the type node. If it follows the file-level rename instead, _merge_swift_extensions |
| 184 | + removes every node with that id, and both file nodes go with it.""" |
| 185 | + files = {"sw/x.swift": "struct sql {\n func run() {}\n}\n\nextension sql {\n func extra() {}\n}\n", |
| 186 | + "sw/x.sql": SQL} |
| 187 | + result = _run(tmp_path, files, monkeypatch) |
| 188 | + |
| 189 | + swift_file = _node(result, "x.swift", "sw/x.swift")["id"] |
| 190 | + sql_file = _node(result, "x.sql", "sw/x.sql")["id"] |
| 191 | + type_node = _node(result, "sql", "sw/x.swift")["id"] |
| 192 | + assert len({swift_file, sql_file, type_node}) == 3 |
| 193 | + run, extra = _node(result, ".run()", "sw/x.swift")["id"], _node(result, ".extra()", "sw/x.swift")["id"] |
| 194 | + assert {(type_node, run), (type_node, extra)} <= set(_edges(result, "method")) |
0 commit comments