Commit 28cdff42

28cdff429cc0Merged

Sam Ruby <[email protected]> · 5 months ago · 28 files · +27 added · ~1 modified

Description

Add golden tests for the RBS extractor

PR #1262 noted there were no tests for RBS support. spinel_rbs_extract is a pure, deterministic text transform (.rbs in -> seed lines on stdout) and a compiled C binary, so it golden-tests cleanly with no Ruby at test time. It is also the layer where RBS regressions are actually visible: seeds are advisory, so most extractor bugs produce identical program output and slip past the compile-and-run test/.rb suite (this branch's union->poly fix being a case in point).

test/rbs/.rbs + .seed.expected pairs cover the documented subset: scalar vocabulary, nominal types, Array[T]/Hash[K,V] tag selection, nullable, heterogeneous-union -> poly, member kinds (meth/cmeth/self?./attr_*/@ivar), unqualified parent-scope resolution, first-overload-wins, and the signature/type shapes that drop a method.

make rbs-test diffs live output against the goldens and is a prerequisite of make test; it skips gracefully when vendor/rbs has not been fetched. make regen-rbs-expected regenerates the goldens.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
28cdff429cc0
Parent
9dd34a49fe0a

MakefilemodifiedView file

@@ -160,7 +160,7 @@
160160 COMPILER_HELPERS_STAMP := build/stamps/compiler_helpers.rb.stamp
161161 PARSE_STAMP := build/stamps/spinel_parse.c.stamp
162162
163-.PHONY: all parse bootstrap codegen rbs_extract test retest fast-test clean-test-results regen-expected bench optcarrot clean install uninstall deps FORCE
163+.PHONY: all parse bootstrap codegen rbs_extract rbs-test regen-rbs-expected test retest fast-test clean-test-results regen-expected bench optcarrot clean install uninstall deps FORCE
164164
165165 # `make all` includes spinel_rbs_extract when vendor/rbs has been
166166 # fetched (via `make deps`). Without vendor/rbs the extractor is
@@ -441,8 +441,9 @@
441441 TEST_TARGETS := $(patsubst test/%.rb,build/test-results/%.ok,$(TESTS))
442442
443443 # `make test` is incremental via mtime tracking on .ok files;
444-# `make retest` wipes them for a forced rerun.
445-test: $(TEST_TARGETS)
444+# `make retest` wipes them for a forced rerun. The rbs-test prereq
445+# golden-checks the RBS extractor (cheap, runs every invocation).
446+test: rbs-test $(TEST_TARGETS)
446447 @if [ -z "$(TIMEOUT_BIN)" ]; then echo "Note: no 'timeout' command found; running without time limits."; fi
447448 @if [ -t 1 ]; then printf '\n'; fi
448449 @pass=$$(grep -l '^PASS' build/test-results/*.ok 2>/dev/null | wc -l); \
@@ -467,6 +468,46 @@
467468 fast-test: clean-test-results
468469 @$(MAKE) --no-print-directory test OPT=-O0 LTO=0
469470
471+# ---- RBS extractor golden tests ----
472+# spinel_rbs_extract is a pure text transform (*.rbs -> seed lines on
473+# stdout), so it golden-tests cleanly without Ruby at test time. Each
474+# test/rbs/<name>.rbs has a committed test/rbs/<name>.seed.expected;
475+# `rbs-test` diffs live output against it. Regenerate after intentional
476+# extractor changes with `make regen-rbs-expected` and commit the diff.
477+# Skips (rather than fails) when vendor/rbs hasn't been fetched, so a
478+# `make deps`-less checkout can still run the rest of `make test`.
479+RBS_TEST_SRCS := $(sort $(wildcard test/rbs/*.rbs))
480+
481+ifeq ($(wildcard $(RBS_INC)/rbs/parser.h),)
482+rbs-test:
483+ @echo "rbs-test: skipped (vendor/rbs not fetched; run 'make deps')"
484+regen-rbs-expected:
485+ @echo "regen-rbs-expected: skipped (vendor/rbs not fetched; run 'make deps')"
486+else
487+rbs-test: spinel_rbs_extract$(EXE)
488+ @fail=0; n=0; \
489+ for f in $(RBS_TEST_SRCS); do \
490+ n=$$((n+1)); \
491+ exp="$${f%.rbs}.seed.expected"; \
492+ if [ ! -f "$$exp" ]; then echo "rbs-test: MISSING golden $$exp"; fail=1; continue; fi; \
493+ d=$$(./spinel_rbs_extract$(EXE) "$$f" 2>/dev/null | diff -u "$$exp" - 2>&1); \
494+ if [ -z "$$d" ]; then \
495+ if [ -t 1 ]; then printf .; fi; \
496+ else \
497+ echo; echo "rbs-test FAIL: $$f"; echo "$$d"; fail=1; \
498+ fi; \
499+ done; \
500+ if [ -t 1 ]; then printf '\n'; fi; \
501+ if [ $$fail -ne 0 ]; then echo "RBS extractor tests: FAIL"; exit 1; fi; \
502+ echo "RBS extractor tests: $$n pass"
503+
504+regen-rbs-expected: spinel_rbs_extract$(EXE)
505+ @for f in $(RBS_TEST_SRCS); do \
506+ ./spinel_rbs_extract$(EXE) "$$f" > "$${f%.rbs}.seed.expected"; \
507+ echo "regen: $${f%.rbs}.seed.expected"; \
508+ done
509+endif
510+
470511 # The .ok target is the test's stamp; mtime tracking gives per-test
471512 # caching for free. Order-only spinel_parse$(EXE) / spinel_analyze$(EXE)
472513 # / spinel_codegen$(EXE) stop a bootstrap relink from invalidating every test.