Skip to content

Commit 6bfb627

Browse files
committed
test: detect heavy imports to prevent slow shell completions
1 parent 7f69f2c commit 6bfb627

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

tests/test_cli.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,52 @@
1616
)
1717
from vesskel.config import CONFIG_SCHEMA_VERSION, PipelineConfig
1818

19+
HEAVY_MODULES = frozenset(
20+
{"numpy", "PIL", "PIL.Image", "vesskel.pipeline", "vesskel._batch"}
21+
)
22+
23+
24+
class TestCompletionSpeed:
25+
"""Shell completions must exit before importing heavy modules (numpy/PIL)."""
26+
27+
def test_completions_skip_heavy_imports(self):
28+
import subprocess
29+
import sys
30+
31+
heavy_modules = sorted(HEAVY_MODULES)
32+
probe = f"""
33+
import os, sys
34+
35+
def _fake_exit(code=0):
36+
raise SystemExit(code)
37+
os._exit = _fake_exit
38+
39+
os.environ["_ARGCOMPLETE"] = "1"
40+
sys.argv = ["vesskel", "complete", "zsh"]
41+
42+
try:
43+
import vesskel.cli
44+
except SystemExit:
45+
pass
46+
47+
heavy = [m for m in {heavy_modules!r} if m in sys.modules]
48+
if heavy:
49+
sys.stdout.write("HEAVY:" + ",".join(heavy))
50+
"""
51+
result = subprocess.run(
52+
[sys.executable, "-c", probe],
53+
capture_output=True,
54+
text=True,
55+
timeout=15,
56+
)
57+
assert result.returncode == 0, f"Subprocess failed (stderr): {result.stderr}"
58+
assert (
59+
"HEAVY:" not in result.stdout
60+
), f"Heavy modules loaded during completions: {result.stdout}"
61+
assert (
62+
"HEAVY:" not in result.stderr
63+
), f"Heavy modules leaked to stderr: {result.stderr}"
64+
1965

2066
class TestDiscoverInputPaths:
2167
def test_single_png_file(self, tmp_path):

0 commit comments

Comments
 (0)