diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4997c199..10909466 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -103,8 +103,10 @@ jobs: GOWASIRUNTIME: wazero run: | GOOS=wasip1 GOARCH=wasm go vet ./cmd/rbgo ./internal/vm - # Exercise the wasm build-tagged statSys (internal/vm/filestat_wasm.go) - # by running its covering test as a wasm binary under wazero. + # Two things run as a wasm binary under wazero: the wasm build-tagged + # statSys (internal/vm/filestat_wasm.go), and the end-to-end claim that + # a program with an observable effect has one. The build steps above + # prove the module links; only this proves it interprets. GOOS=wasip1 GOARCH=wasm go test -c -o vm.test.wasm ./internal/vm "$(go env GOPATH)/bin/wazero" run -mount=/tmp:/tmp -env TMPDIR=/tmp \ - vm.test.wasm -test.run 'TestStatSysExtract' -test.v + vm.test.wasm -test.run 'TestStatSysExtract|TestRunsAProgramOnThisHost' -test.v diff --git a/internal/vm/runs_a_program_test.go b/internal/vm/runs_a_program_test.go new file mode 100644 index 00000000..9e7fe95d --- /dev/null +++ b/internal/vm/runs_a_program_test.go @@ -0,0 +1,48 @@ +package vm + +import ( + "strings" + "testing" + + "github.com/go-embedded-ruby/ruby/internal/compiler" + "github.com/go-ruby-parser/parser" +) + +// TestRunsAProgramOnThisHost is the smallest claim an interpreter can make: a +// program with an observable effect has one. Parse, compile, run, and look for +// the string the program printed — no fixture, no golden file, nothing that can +// pass while the VM produces nothing. +// +// It is written to be run on every host the project targets, and the wasip1 +// lane in ci.yml runs exactly this test under wazero. That is deliberate: a +// build gate proves the module links, not that it interprets. This one costs a +// couple of hundred milliseconds and would have caught, in CI, a wasm build +// that loads and then quietly does nothing. +// +// (It came out of a false alarm worth recording: rbgo appeared not to run +// programs at all under wasip1 — silent CLI, and this very test hanging inside +// vm.New. The stall was in the host runtime, wasmer 7.2.1, whose stopping point +// moved between identical runs; the same .wasm file passes under wazero in +// 0.07s. Hence the runtime named in CI.) +func TestRunsAProgramOnThisHost(t *testing.T) { + prog, err := parser.Parse("puts 'HELLO-FROM-VM'\n") + if err != nil { + t.Fatalf("parse: %v", err) + } + iseq, err := compiler.Compile(prog) + if err != nil { + t.Fatalf("compile: %v", err) + } + if len(iseq.Insns) == 0 { + t.Fatal("compiled to zero instructions") + } + + var out strings.Builder + m := New(&out) + if _, err := m.Run(iseq); err != nil { + t.Fatalf("run: %v", err) + } + if got := out.String(); !strings.Contains(got, "HELLO-FROM-VM") { + t.Fatalf("the program produced no output: %q (%d instructions)", got, len(iseq.Insns)) + } +}