Files
stack/tests/lex/test_js.py

70 lines
2.1 KiB
Python

"""The lex JS unit tests (``tests/lex/*.test.mjs``) under pytest.
The modules are ES modules in ``.js`` files with no ``package.json``
``"type": "module"`` (Gitea serves them as they are), which node runs only
from 22.7 on (syntax detection on by default). Ruling S1-P5: this test skips
when the host has no node, or an older one; 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/lex/*.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_lex_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/lex").glob("*.test.mjs")
)
assert files, "no tests/lex/*.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:]
)