`examples/<name>/` was a tested surface in name only. The source-quality job had no examples check at all, and scripts/smoke_examples.sh accepted any process that had not printed one of its error markers: a project that died during startup, exited after a single frame, or crashed without a marker was reported as PASS. Nothing stopped a malformed or unregistered example from reaching dev, which is how 14 examples arrived without READMEs and 20 were never registered in the overview table. Source side - scripts/check_examples.py classifies every directory under examples/ as either runnable (main.nut plus a config.nut that assigns the engine `config` table) or as a reviewed non-runnable sample listed in NON_RUNNABLE_EXAMPLES with a reason. Both kinds must own a README.md and appear in examples/README.md. Stale exemptions and overview links to missing directories fail as well. - `make check/examples` and a new source-quality CI step run the gate. - scripts/tests/test_check_examples.py covers it and pins each failure mode against fixtures. Runtime side - scripts/smoke_examples.sh now enforces liveness, not just marker absence: every runnable example must still be running after MIN_RUN_SECONDS (default 2s; CI passes 2s), and a process that dies inside that window fails whatever its exit code. A clean self-exit after the window passes with an explicit note instead of being silently accepted as a pass. - The Linux CI step sets MIN_RUN_SECONDS=2 and keeps RUN_SECONDS=6, so the job's wall clock is unchanged (13.7 min of its 20 min budget on dev). Windows / GBK - scripts/utf8_stdio.py re-encodes stdout/stderr as UTF-8 and every scripts/*.py program calls it as the first statement of its __main__ block. On a Chinese Windows a redirected stream falls back to the ANSI code page (cp936), and printing a character that code page cannot encode (arrows, superscripts, bullet separators, emoji - all common in this repository) raised UnicodeEncodeError and turned a checker into a traceback. - The mirror image is input: read_text() without encoding= decoded UTF-8 repository text as cp936 and raised UnicodeDecodeError. - scripts/tests/test_utf8_stdio.py enforces both conventions for every script and test module; text file I/O and child-process decoding now pass encoding="utf-8" explicitly. - Unit-test invocations run with `python3 -X utf8` so the runner's own output never follows the host code page. Violations the new gates found on top of dev, fixed here: 14 examples shipped without a README (fluid3d, mesh-impact-lab, mesh-modifier-lab, spline-tube-lab on the earlier dev tip, plus pcg-depth-of-field, pcg-detail-overwrite, pcg-layer-culling, pcg-location-system, pcg-runtime-orchestration, pcg-scene-player-culling, pcg-snow-wind, pcg-thunder-strike, pcg-ui-scaler and pcg-underwater added by #397), 20 were missing from the overview table (those 14 plus archspace-editor, geometry-stroke-lab, interior-mapping, layered-animation, triplanar-decal, terrain-stamping), and scripts/export_meshret_onnx.py had no UTF-8 stdio guard. Verified locally: `python -m ruff check scripts` (All checks passed); scripts/check_test_manifest.py; scripts/check_quality_metadata.py; scripts/check_architecture_contracts.py --base HEAD; scripts/check_nodiscard.py; `make -n check/examples`; scripts/check_examples.py (125 examples: 118 runnable, 7 reviewed non-runnable); and `python -X utf8 -m unittest discover -s scripts/tests -p "test_*.py"` (155 tests, OK). The runtime half of the contract needs a Linux headless build, so it is verified by the CI examples smoke step rather than locally.
159 lines
6.0 KiB
Python
159 lines
6.0 KiB
Python
"""Tests for the examples/ layout contract enforced by scripts/check_examples.py."""
|
|
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
|
|
import check_examples # noqa: E402
|
|
|
|
MAIN_NUT = "eve_init = function() {}\n"
|
|
CONFIG_NUT = "config <- { width = 800 height = 600 }\n"
|
|
|
|
|
|
def scratch_parent() -> Path:
|
|
"""A writable scratch parent, preferring the (git-ignored) build/ directory."""
|
|
build = ROOT / "build"
|
|
try:
|
|
build.mkdir(parents=True, exist_ok=True)
|
|
except OSError:
|
|
return Path(tempfile.gettempdir())
|
|
return build
|
|
|
|
|
|
class ExampleLayoutTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.root = scratch_parent() / f"check-examples-{uuid.uuid4().hex[:8]}"
|
|
(self.root / "examples").mkdir(parents=True)
|
|
self.index = self.root / "examples" / "README.md"
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.root, ignore_errors=True)
|
|
|
|
def add_example(self, name, *, main=True, config=CONFIG_NUT, readme=True):
|
|
path = self.root / "examples" / name
|
|
path.mkdir(parents=True, exist_ok=True)
|
|
if main:
|
|
(path / "main.nut").write_text(MAIN_NUT, encoding="utf-8")
|
|
if config is not None:
|
|
(path / "config.nut").write_text(config, encoding="utf-8")
|
|
if readme:
|
|
(path / "README.md").write_text(f"# {name}\n\n说明\n", encoding="utf-8")
|
|
return path
|
|
|
|
def write_index(self, names, extra=""):
|
|
rows = "\n".join(f"| [{name}]({name}/README.md) | 说明 |" for name in names)
|
|
self.index.write_text(
|
|
f"# 示例总览\n\n{extra}\n| 示例 | 演示能力 |\n|---|---|\n{rows}\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
def findings(self, exemptions=None):
|
|
return check_examples.conformance_findings(self.root, exemptions or {})
|
|
|
|
def assert_reports(self, needle, exemptions=None):
|
|
findings = self.findings(exemptions)
|
|
self.assertTrue(
|
|
any(needle in finding for finding in findings),
|
|
f"expected a finding containing {needle!r}, got {findings}",
|
|
)
|
|
|
|
def test_conforming_tree_reports_nothing(self):
|
|
self.add_example("alpha")
|
|
self.add_example("beta", main=False, config=None)
|
|
self.write_index(["alpha", "beta"])
|
|
self.assertEqual([], self.findings({"beta": "C++ plugin sample"}))
|
|
|
|
def test_runnable_example_needs_config_nut(self):
|
|
self.add_example("alpha", config=None)
|
|
self.write_index(["alpha"])
|
|
self.assert_reports("alpha/config.nut: runnable example has no config.nut")
|
|
|
|
def test_config_nut_must_assign_the_config_table(self):
|
|
self.add_example("alpha", config='window_title <- "alpha";\nwindow_width <- 960;\n')
|
|
self.write_index(["alpha"])
|
|
self.assert_reports("alpha/config.nut: does not assign the engine `config` table")
|
|
|
|
def test_config_field_assignment_is_accepted(self):
|
|
self.add_example("alpha", config="config.width <- 1024;\nconfig.title <- 'alpha';\n")
|
|
self.write_index(["alpha"])
|
|
self.assertEqual([], self.findings())
|
|
|
|
def test_every_example_needs_a_readme(self):
|
|
self.add_example("alpha", readme=False)
|
|
self.write_index(["alpha"])
|
|
self.assert_reports("alpha/README.md: missing example README")
|
|
|
|
def test_directory_without_main_nut_must_be_exempted(self):
|
|
self.add_example("orphan", main=False, config=None)
|
|
self.write_index(["orphan"])
|
|
self.assert_reports("examples/orphan/: has no main.nut")
|
|
|
|
def test_exemption_without_reason_is_reported(self):
|
|
self.add_example("beta", main=False, config=None)
|
|
self.write_index(["beta"])
|
|
self.assert_reports("NON_RUNNABLE_EXAMPLES entry 'beta' needs a reason", {"beta": " "})
|
|
|
|
def test_stale_exemption_is_reported(self):
|
|
self.add_example("alpha")
|
|
self.write_index(["alpha"])
|
|
self.assert_reports(
|
|
"NON_RUNNABLE_EXAMPLES entry 'ghost' has no matching examples/ directory",
|
|
{"ghost": "removed long ago"},
|
|
)
|
|
|
|
def test_exemption_for_a_runnable_example_is_reported(self):
|
|
self.add_example("alpha")
|
|
self.write_index(["alpha"])
|
|
self.assert_reports("entry 'alpha' is stale", {"alpha": "now runnable"})
|
|
|
|
def test_example_missing_from_the_overview_is_reported(self):
|
|
self.add_example("alpha")
|
|
self.add_example("beta")
|
|
self.write_index(["alpha"])
|
|
self.assert_reports("examples/beta is not listed in the overview table")
|
|
|
|
def test_dead_index_link_is_reported(self):
|
|
self.add_example("alpha")
|
|
self.write_index(["alpha", "ghost"])
|
|
self.assert_reports("links examples/ghost/README.md but that directory does not exist")
|
|
|
|
def test_missing_overview_file_is_reported(self):
|
|
self.add_example("alpha")
|
|
self.assert_reports("examples/README.md: missing example overview")
|
|
|
|
def test_linked_examples_reads_only_example_readme_links(self):
|
|
text = (
|
|
"[Character Motion Lab](character-motion-lab/README.md)\n"
|
|
"[docs](../Readme.md)\n"
|
|
"[nested](tools/gizmo/README.md)\n"
|
|
"[index](README.md)\n"
|
|
)
|
|
self.assertEqual(["character-motion-lab"], check_examples.linked_examples(text))
|
|
|
|
def test_missing_index_rows_are_ready_to_paste(self):
|
|
self.add_example("alpha")
|
|
self.write_index([])
|
|
self.assertEqual(
|
|
["| [alpha](alpha/README.md) | alpha |"],
|
|
check_examples.missing_index_rows(self.root),
|
|
)
|
|
|
|
def test_repository_examples_satisfy_the_contract(self):
|
|
self.assertEqual([], check_examples.conformance_findings(ROOT))
|
|
|
|
def test_repository_exemptions_describe_real_directories(self):
|
|
discovered = {example.name for example in check_examples.discover(ROOT)}
|
|
for name, reason in check_examples.NON_RUNNABLE_EXAMPLES.items():
|
|
self.assertIn(name, discovered)
|
|
self.assertTrue(reason.strip(), f"{name} needs a reason")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|