upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/test_runtime_paths.py
blob: 97431bd734d9026604f05b7b94c25c0403b2c037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""The Runtime workflow's path-filter scanner and manual-only trigger check.

    python -I tests/test_runtime_paths.py

``.github/workflows/runtime.yml`` builds the deprecated legacy C++ Vulkan ``runtime/`` (superseded by ``d3d12/``
and the new Rust Vulkan runtime in progress), so it is now manual-only (``workflow_dispatch``) and excluded from
automatic ``push``, ``pull_request``, and ``schedule`` CI runs.

If automatic ``push`` / ``pull_request`` path filters are ever restored on ``runtime.yml``, ``problems()``
recomputes every file under ``tools/`` that its jobs read and fails when an entry is missing:

* the seeds are the code that runs during the jobs: the CMake files under ``runtime/`` and the workflow's
  ``jobs:``, with ``#`` comments removed, and the Python under ``runtime/`` plus the scripts the workflow runs.
  A ``tools/<name>`` a CMake line or a job step spells out is needed; so is a ``tools`` module a Python seed
  imports (the scripts and shader checks put ``tools/`` on ``sys.path``) and a ``tools/`` file it names in a
  string, such as ``os.path.join(TOOLS, "rdna2spirv.py")``;
* from there, each needed tool's sibling imports, the ``tools/`` data files it names (``gfx10_formats.json`` is
  loaded by name, which a ``.py``-only scan misses), modules it loads with ``importlib`` and ``tools/``
  subdirectories it names, to a fixed point.

Comments, docstrings and the C++, shader and header sources are not read as dependencies: a ``tools/<name>`` in
them says where a table or a test came from, not that the build runs it, and counting those would make every
such note a required entry.

Only tracked sources and the workflow are read; no game files, no build tools, no PyYAML.
"""
import ast
import os
import re
import subprocess
import sys
import tempfile
import unittest

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(ROOT, "tests", "semantic"))
import piecewise_parse  # noqa: E402  (ast.parse takes 7 s for tools/rdna2spirv.py on CPython 3.12)
WORKFLOW = ".github/workflows/runtime.yml"

# Entries that are needed for reasons a source scan cannot see: the build's own trees, the workflow, and the
# file that decides the line endings every checked-out source gets.
ALWAYS = ("runtime/**", "third_party/**", WORKFLOW, ".gitattributes")

TOOLS_REF = re.compile(r"tools/([A-Za-z0-9_][A-Za-z0-9_.-]*)")
SCRIPT_REF = re.compile(r"scripts/[A-Za-z0-9_./-]+\.py")


def read(root, rel):
    with open(os.path.join(root, rel), encoding="utf-8", errors="replace") as f:
        return f.read()


def path_lists(root):
    """{event: [paths]} for the events that filter on paths, exactly as the workflow spells them."""
    text = read(root, WORKFLOW)
    lists = {}
    for event in ("push", "pull_request"):
        match = re.search(r"^  %s:\n((?:    .*\n)+)" % event, text, re.M)
        if not match:
            raise AssertionError(f"{WORKFLOW} has no `{event}:` trigger the way this test expects it")
        body = match.group(1)
        paths = re.search(r"^    paths:\n((?:      - .*\n)+)", body, re.M)
        if not paths:
            raise AssertionError(f"{WORKFLOW} `{event}:` has no `paths:` list; update this test with the workflow")
        lists[event] = [line.strip()[1:].strip().strip("\"'") for line in paths.group(1).splitlines()]
    return lists


def covered(path, patterns):
    for pattern in patterns:
        if pattern.endswith("/**"):
            if path.startswith(pattern[:-2]):
                return True
        elif path == pattern:
            return True
    return False


def tracked(root, top):
    result = subprocess.run(["git", "ls-files", "-z", "--", top], cwd=root, capture_output=True, text=True)
    if result.returncode == 0 and result.stdout:
        return [n for n in result.stdout.split("\0") if n]
    names = []                                     # no git: walk, as tests/test_docs_links.py does
    for base, dirs, files in os.walk(os.path.join(root, top)):
        dirs[:] = [d for d in dirs if d not in (".git", "__pycache__", "build")]
        for name in files:
            names.append(os.path.relpath(os.path.join(base, name), root).replace(os.sep, "/"))
    return names


def python_refs(source):
    """(modules, strings) of a Python file: what it imports, including importlib.import_module("x") and
    __import__("x"), and its string constants other than docstrings. Comments never reach the AST."""
    try:
        tree = piecewise_parse.parse(source)
    except SyntaxError:
        return set(), set()
    docstrings = set()
    for node in ast.walk(tree):
        if isinstance(node, (ast.Module, ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)) and node.body:
            first = node.body[0]
            if isinstance(first, ast.Expr) and isinstance(first.value, ast.Constant):
                docstrings.add(id(first.value))
    modules, strings = set(), set()
    for node in ast.walk(tree):
        if isinstance(node, ast.Import):
            modules.update(alias.name.split(".")[0] for alias in node.names)
        elif isinstance(node, ast.ImportFrom) and node.module and node.level == 0:
            modules.add(node.module.split(".")[0])
        elif isinstance(node, ast.Call) and node.args and isinstance(node.args[0], ast.Constant):
            func = node.func
            name = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", "")
            if name in ("import_module", "__import__") and isinstance(node.args[0].value, str):
                modules.add(node.args[0].value.split(".")[0])
        if isinstance(node, ast.Constant) and isinstance(node.value, str) and id(node) not in docstrings:
            strings.add(node.value)
    return modules, strings


def strip_hash_comments(text):
    """CMake and YAML without their # comments (a # inside a quoted string is rare enough in both to ignore)."""
    return "\n".join(line.split("#", 1)[0] for line in text.splitlines())


def needed(root):
    """{tools entry: why} for everything under tools/ the Runtime jobs read."""
    tools = os.path.join(root, "tools")
    entries = os.listdir(tools)
    modules = {e[:-3] for e in entries if e.endswith(".py")}
    data = {e for e in entries if os.path.isfile(os.path.join(tools, e)) and not e.endswith(".py")}
    subdirs = {e for e in entries if os.path.isdir(os.path.join(tools, e)) and not e.startswith((".", "__"))}

    def entry(name):
        name = name.rstrip(".")
        if name in subdirs:
            return f"tools/{name}/**"
        if name in modules or name.endswith(".py") and name[:-3] in modules:
            return f"tools/{name if name.endswith('.py') else name + '.py'}"
        if name in data:
            return f"tools/{name}"
        return None

    def spelled(text):
        for name in TOOLS_REF.findall(text):
            first = name.split("/")[0]
            yield first if first in subdirs else name

    def python_names(source, own_dir):
        """tools/ entries a Python file imports or names in a string; own_dir's files shadow tools/."""
        imports, strings = python_refs(source)
        names = {m + ".py" for m in imports
                 if m in modules and not (own_dir and os.path.exists(os.path.join(own_dir, m + ".py")))}
        for s in strings:
            names.update(spelled(s))
            base = s.rstrip("/").split("/")[-1]
            if base in data or base in subdirs or base.endswith(".py") and base[:-3] in modules:
                if not (own_dir and "/" not in s and os.path.exists(os.path.join(own_dir, base))):
                    names.add(base)
        return names

    workflow = read(root, WORKFLOW)
    # Only what the jobs run: the trigger's own list would make every listed entry "needed".
    jobs = strip_hash_comments(workflow[workflow.find("\njobs:"):] if "\njobs:" in workflow else "")
    found = {}
    for name in spelled(jobs):
        hit = entry(name)
        if hit:
            found.setdefault(hit, f"run by {WORKFLOW}")
    runtime = tracked(root, "runtime")
    cmake = [f for f in runtime if f.endswith(".cmake") or f.split("/")[-1] == "CMakeLists.txt"]
    python = [f for f in runtime if f.endswith(".py")] + sorted(set(SCRIPT_REF.findall(jobs)))
    for seed in cmake:
        for name in spelled(strip_hash_comments(read(root, seed))):
            hit = entry(name)
            if hit:
                found.setdefault(hit, f"named in {seed}")
    for seed in python:
        if os.path.isfile(os.path.join(root, seed)):
            own = os.path.dirname(os.path.join(root, seed))
            for name in python_names(read(root, seed), own):
                hit = entry(name)
                if hit:
                    found.setdefault(hit, f"read by {seed}")

    queue = list(found)
    while queue:
        current = queue.pop()
        if current.endswith("/**"):
            sources = [f for f in tracked(root, current[:-3]) if f.endswith(".py")]
        elif current.endswith(".py"):
            sources = [current]
        else:
            continue
        for source in sources:
            for name in python_names(read(root, source), None):
                hit = entry(name)
                if hit and hit not in found:
                    found[hit] = f"via {source}"
                    queue.append(hit)
    return found


def problems(root):
    lists = path_lists(root)
    issues = []
    events = sorted(lists)
    for other in events[1:]:
        if lists[other] != lists[events[0]]:
            issues.append(f"the `{events[0]}` and `{other}` path lists differ")
    patterns = lists[events[0]]
    for must in ALWAYS:
        if must not in patterns:
            issues.append(f"{must}: missing (always needed)")
    for script in sorted(set(SCRIPT_REF.findall(read(root, WORKFLOW)))):
        if not covered(script, patterns):
            issues.append(f"{script}: run by the workflow but not listed")
    for path, why in sorted(needed(root).items()):
        if not covered(path, patterns):
            issues.append(f"{path}: missing ({why})")
    for pattern in patterns:
        if pattern.startswith("tools/") and not pattern.endswith("/**"):
            if not os.path.exists(os.path.join(root, pattern)):
                issues.append(f"{pattern}: listed but does not exist")
    return issues


class RepositoryRuntimePathsTest(unittest.TestCase):
    def test_the_scan_sees_the_repository(self):
        found = needed(ROOT)
        # The translator the GPU tests run through and the stub generator CMake runs at configure time.
        self.assertIn("tools/rdna2spirv.py", found)
        self.assertIn("tools/gen_d3d12_stubs.py", found)

    def test_runtime_workflow_is_manual_dispatch_only(self):
        # Legacy C++ runtime/ is deprecated and excluded from automatic CI runs; manual dispatch stays available.
        text = read(ROOT, WORKFLOW)
        self.assertRegex(text, r"(?m)^  workflow_dispatch:\s*$")
        for trigger in ("push", "pull_request", "schedule"):
            self.assertNotRegex(
                text,
                rf"(?m)^  {trigger}:",
                f"{WORKFLOW} must not run automatically on `{trigger}:` while legacy runtime/ is deprecated",
            )


class SyntheticTreeTest(unittest.TestCase):
    """The scan flags what a path grep misses, on a tree built for the purpose."""

    def build(self, tmp, listed):
        def write(rel, text):
            path = os.path.join(tmp, rel)
            os.makedirs(os.path.dirname(path), exist_ok=True)
            with open(path, "w", encoding="utf-8", newline="\n") as f:
                f.write(text)

        paths = "".join(f'      - "{p}"\n' for p in ALWAYS + ("scripts/**",) + tuple(listed))
        write(WORKFLOW, "name: Runtime\n\non:\n  push:\n    branches: [main]\n    paths:\n" + paths +
              "  pull_request:\n    paths:\n" + paths + "\njobs:\n  x:\n    steps:\n"
              "      - run: python3 -I scripts/run_gpu_tests.py\n")
        write("runtime/CMakeLists.txt", "add_custom_command(COMMAND python tools/translator.py)\n")
        write("scripts/run_gpu_tests.py", "import sys\nimport subgroup_info\n")
        write("tools/translator.py", "import lanes\nimport json\nTABLE = 'formats.json'\nRUN = 'tools/runner.py'\n")
        write("tools/runner.py", "X = 1\n")
        write("tools/lanes.py", "import importlib\nM = importlib.import_module('late')\n")
        write("tools/late.py", "ORACLE = 'oracle'\n")
        write("tools/oracle/run.py", "import helper\n")
        write("tools/helper.py", "X = 1\n")
        write("tools/formats.json", "{}\n")
        write("tools/subgroup_info.py", "X = 1\n")
        write("tools/unrelated.py", "X = 1\n")
        # Notes, not reads: none of these may make tools/unrelated.py a required entry.
        write("runtime/src/table.h", "// generated by tools/unrelated.py\nconst char *k = \"tools/unrelated.py\";\n")
        write("runtime/test/shaders/probe.asm", "; checked with tools/unrelated.py\n")
        write("runtime/test/shaders/check.py", '"""Words from tools/unrelated.py."""\n# see tools/unrelated.py\n')
        write("runtime/cmake/extra.cmake", "# tools/unrelated.py made this list\n")

    def test_a_complete_list_passes(self):
        with tempfile.TemporaryDirectory() as tmp:
            self.build(tmp, ["tools/translator.py", "tools/lanes.py", "tools/late.py", "tools/oracle/**",
                             "tools/helper.py", "tools/formats.json", "tools/subgroup_info.py",
                             "tools/runner.py"])
            self.assertEqual(problems(tmp), [])
            self.assertNotIn("tools/unrelated.py", needed(tmp))

    def test_each_kind_of_dependency_is_reported_when_missing(self):
        with tempfile.TemporaryDirectory() as tmp:
            self.build(tmp, ["tools/translator.py", "tools/gone.py"])
            issues = "\n".join(problems(tmp))
            for missing in ("tools/lanes.py", "tools/late.py", "tools/oracle/**", "tools/helper.py",
                            "tools/formats.json", "tools/subgroup_info.py", "tools/runner.py"):
                self.assertIn(f"{missing}: missing", issues)
            self.assertIn("tools/gone.py: listed but does not exist", issues)

    def test_lists_that_differ_are_reported(self):
        with tempfile.TemporaryDirectory() as tmp:
            self.build(tmp, [])
            path = os.path.join(tmp, WORKFLOW)
            text = read(tmp, WORKFLOW).replace('    paths:\n', '    paths:\n      - "tools/extra.py"\n', 1)
            with open(path, "w", encoding="utf-8", newline="\n") as f:
                f.write(text)
            self.assertTrue(any("path lists differ" in i for i in problems(tmp)))


if __name__ == "__main__":
    unittest.main()