Files
mandy/tests/test_tool_startup.py
fiestorra 1eee5a1aaa tests: a sibling that imports a numeric tool is not a startup failure
test_each_sibling_importing_tool_starts skipped a NUMERIC list when numpy was
missing, but it asked find_spec in this process while the tools run with -I, so
the skip never fired here; and a list of names cannot cover calibrated_texture
and tex_export, which are not numeric themselves and import one that is.

The probe now matches how the tools run, and a child that fails with
"No module named 'numpy'" is skipped rather than failing the startup check.

Measured on main b8bd37111, Windows 11, Python 3.13: 2 failed, 2 passed,
218 subtests before; 2 passed, 214 subtests after (the four are the two
numpy-dependent subtests, skipped). With numpy visible to -I nothing changes.

Written with an AI assistant.
2026-11-30 14:55:47 +01:00

82 lines
3.9 KiB
Python

"""Every tool that imports a sibling module starts under the project's own `python -I` convention.
`-I` implies `-P`, so the script's directory is not put on `sys.path`; a tool that imports the module next to it
has to add that directory itself (the pattern in `tools/anim_clips.py:40`). Two tools did not and died with
`ModuleNotFoundError` before `main()` — `calibrated_texture.py` and `ttpf_assets.py` — which this test caught.
The tool list is derived from the sources rather than written down, so a new tool with a sibling import is covered
without touching this file. `--help` is used because it reaches the argument parser without game files or a GPU;
the three module-only tools (`bcpack`, `calibrated_texture`, `cf_ref`) have no `__main__` and simply exit 0 on the
import. Tools that need numpy are skipped where it is not installed, as the texture tools are allowed to use it.
"""
import ast
import glob
import importlib.util
import os
import subprocess
import sys
import unittest
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
TOOLS = os.path.join(ROOT, "tools")
TOOL_NAMES = {os.path.splitext(os.path.basename(path))[0] for path in glob.glob(os.path.join(TOOLS, "*.py"))}
NUMERIC = {"amd_swizzle", "calibrated_texture", "swizzle_search", "tex_export", "tex_mode_probe", "tile_probe"}
# One process per tool is a few seconds in total; a hung import is a failure, not an infinite test.
TIMEOUT = 120
def imports(path):
"""The module names `path` imports at top level, relative imports excluded."""
names = set()
with open(path, encoding="utf-8") as source:
tree = ast.parse(source.read())
for node in ast.walk(tree):
if isinstance(node, ast.Import):
names |= {alias.name.split(".")[0] for alias in node.names}
elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
names.add(node.module.split(".")[0])
return names
def sibling_importers():
found = []
for path in sorted(glob.glob(os.path.join(TOOLS, "*.py"))):
own = os.path.splitext(os.path.basename(path))[0]
if imports(path) & (TOOL_NAMES - {own}):
found.append(path)
return found
class ToolStartupTests(unittest.TestCase):
def test_each_sibling_importing_tool_starts(self):
paths = sibling_importers()
self.assertGreater(len(paths), 20, "the sibling-import scan found almost nothing")
# Ask the way the tools are launched: `-I` does not see a --user install, so find_spec in this process can
# say yes while the child gets ModuleNotFoundError (tests/test_tile_probe.py carries the same correction).
have_numpy = subprocess.run([sys.executable, "-I", "-c", "import numpy"],
capture_output=True, check=False).returncode == 0
for path in paths:
name = os.path.splitext(os.path.basename(path))[0]
if name in NUMERIC and not have_numpy:
continue
with self.subTest(tool=name):
proc = subprocess.run([sys.executable, "-I", path, "--help"], capture_output=True, text=True,
timeout=TIMEOUT, errors="replace")
# A sibling that is not numeric itself can still import a numeric one: a list of names cannot cover
# those, and a missing third-party module is not a startup failure. CI has numpy, so it never fires.
if not have_numpy and "No module named 'numpy'" in proc.stderr:
continue
self.assertEqual(proc.returncode, 0, proc.stderr[-2000:])
self.assertNotIn("Traceback", proc.stderr)
def test_the_two_fixed_tools_are_in_the_scan(self):
names = {os.path.splitext(os.path.basename(path))[0] for path in sibling_importers()}
self.assertIn("calibrated_texture", names)
self.assertIn("ttpf_assets", names)
if __name__ == "__main__":
unittest.main()