68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
"""The focus JS unit tests (``tests/focus/*.test.mjs``) under pytest.
|
|
|
|
The anchor module is an ES module. Node runs that from 22.7 on. This
|
|
test skips when the host has no such node; the gate is the docker run
|
|
|
|
docker run --rm -v /home/kert/stack:/w:ro -w /w node:22-slim \\
|
|
sh -c 'node --test tests/focus/*.test.mjs'
|
|
|
|
which must print ``# fail 0``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
MIN_NODE = (22, 7)
|
|
SKIP_REASON = "node ≥ 22.7 not on PATH; run NODE"
|
|
|
|
|
|
def node_version(text: str) -> tuple[int, int] | None:
|
|
"""``(major, minor)`` from ``node --version`` output such as ``v22.7.0``."""
|
|
m = re.match(r"\s*v(\d+)\.(\d+)", text or "")
|
|
return (int(m.group(1)), int(m.group(2))) if m else None
|
|
|
|
|
|
def node_ok(text: str) -> bool:
|
|
v = node_version(text)
|
|
return v is not None and v >= MIN_NODE
|
|
|
|
|
|
def test_node_gate() -> None:
|
|
assert node_version("v22.7.0\n") == (22, 7)
|
|
assert node_ok("v22.7.0") and node_ok("v24.1.3") and node_ok("v23.0.0")
|
|
assert not node_ok("v22.6.9") and not node_ok("v20.18.0")
|
|
assert not node_ok("") and not node_ok("node: not found")
|
|
|
|
|
|
def test_focus_js_unit_tests() -> None:
|
|
node = shutil.which("node")
|
|
if node is None:
|
|
pytest.skip(SKIP_REASON)
|
|
probe = subprocess.run(
|
|
[node, "--version"], capture_output=True, text=True, check=False
|
|
)
|
|
if not node_ok(probe.stdout):
|
|
pytest.skip(SKIP_REASON)
|
|
files = sorted(
|
|
str(p.relative_to(ROOT)) for p in (ROOT / "tests/focus").glob("*.test.mjs")
|
|
)
|
|
assert files, "no tests/focus/*.test.mjs"
|
|
r = subprocess.run(
|
|
[node, "--test", *files],
|
|
cwd=ROOT,
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=300,
|
|
check=False,
|
|
)
|
|
assert r.returncode == 0 and "# fail 0" in r.stdout, (
|
|
r.stdout[-6000:] + r.stderr[-2000:]
|
|
)
|