diff --git a/.github/workflows/mcpx.yml b/.github/workflows/mcpx.yml index c1ea64f56b..27acdf9efd 100644 --- a/.github/workflows/mcpx.yml +++ b/.github/workflows/mcpx.yml @@ -173,6 +173,10 @@ jobs: - name: Unit and end-to-end tests working-directory: pkgs/mcpx env: + # #1983: CI always sets MCPX_TEST_REQUIRE -- a runtime this job + # installs (deno, bun, node above; python3 on the runner) that is + # nevertheless missing must fail the suite, never skip silently. + MCPX_TEST_REQUIRE: deno,bun,node,python3 VARS: ${{ toJSON(vars) }} GH_TOKEN: ${{ secrets.MCPX_CONFORMANCE_VARS_TOKEN }} ON_MAIN: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} diff --git a/pkgs/mcpx/internal/e2e/ceiling_test.go b/pkgs/mcpx/internal/e2e/ceiling_test.go index fbdcc386ac..e860553613 100644 --- a/pkgs/mcpx/internal/e2e/ceiling_test.go +++ b/pkgs/mcpx/internal/e2e/ceiling_test.go @@ -3,6 +3,7 @@ package e2e_test import ( "encoding/json" "fmt" + "github.com/developing-today/code/pkgs/mcpx/internal/testsupport" "io" "net/http" "os" @@ -280,11 +281,7 @@ func TestRepairCannotRaiseAutonomyAboveTheCeiling(t *testing.T) { func lookBun(t *testing.T) string { t.Helper() - bun, err := exec.LookPath("bun") - if err != nil { - t.Skip("bun is not installed") - } - return bun + return testsupport.RequireBinary(t, "bun") } func runBun(t *testing.T, bun string, e *env, file string, extra ...string) string { diff --git a/pkgs/mcpx/internal/e2e/e2e_test.go b/pkgs/mcpx/internal/e2e/e2e_test.go index 4b7b78a119..8d9dcbdb8e 100644 --- a/pkgs/mcpx/internal/e2e/e2e_test.go +++ b/pkgs/mcpx/internal/e2e/e2e_test.go @@ -2556,9 +2556,7 @@ func TestScriptsReachTheDaemonOverItsSocketWhereTheRuntimeCan(t *testing.T) { {"deno", "via unix"}, {"bun", "via unix"}, {"node", "via tcp"}, } { t.Run(c.rt, func(t *testing.T) { - if _, err := exec.LookPath(c.rt); err != nil { - t.Skipf("%s not installed", c.rt) - } + testsupport.RequireBinary(t, c.rt) e := newEnv(t, oneServer) out := e.run("exec", "--runtime", c.rt, "--format", "bare", `const r = await demo.echo({message:"x"}); console.log("via", transport());`) diff --git a/pkgs/mcpx/internal/e2e/permissions_test.go b/pkgs/mcpx/internal/e2e/permissions_test.go index 3f9056c52b..f4c1da22c1 100644 --- a/pkgs/mcpx/internal/e2e/permissions_test.go +++ b/pkgs/mcpx/internal/e2e/permissions_test.go @@ -2,6 +2,7 @@ package e2e_test import ( "encoding/json" + "github.com/developing-today/code/pkgs/mcpx/internal/testsupport" "os/exec" "path/filepath" "strings" @@ -18,6 +19,9 @@ try { readFileSync("/etc/hosts"); console.log("ALLOWED"); } catch { console.log( e := newEnv(t, oneServer) for _, rt := range []string{"node", "bun"} { if _, err := exec.LookPath(rt); err != nil { + if testsupport.MissingRequired(rt) { + t.Fatalf("%s is required by MCPX_TEST_REQUIRE but is not on PATH: %v", rt, err) + } t.Logf("%s not installed", rt) continue } @@ -35,9 +39,7 @@ try { readFileSync("/etc/hosts"); console.log("ALLOWED"); } catch { console.log( // under --permission with reads allowed, so a write is refused by node // itself. Without the flags the same write succeeds, which is the control. func TestNodeReceivesTheFlagsAProfileDefinesForIt(t *testing.T) { - if _, err := exec.LookPath("node"); err != nil { - t.Skip("node not installed") - } + testsupport.RequireBinary(t, "node") e := newEnv(t, oneServer) target := filepath.Join(t.TempDir(), "w") probe := `import { writeFileSync } from "node:fs"; @@ -55,10 +57,7 @@ try { writeFileSync(` + jsonQuote(target) + `, "1"); console.log("WROTE"); } cat // A runtime declared by name, with a binary whose name says nothing, runs // with the argv of its kind. func TestADeclaredRuntimeRunsByName(t *testing.T) { - node, err := exec.LookPath("node") - if err != nil { - t.Skip("node not installed") - } + node := testsupport.RequireBinary(t, "node") e := newEnv(t, oneServer).withEnv( `MCPX_SCRIPT_RUNTIMES={"engine":{"kind":"node","bin":` + jsonQuote(node) + `}}`) out, err := e.try("exec", "--runtime", "engine", `console.log("kind=" + process.env.MCPX_RUNTIME)`) diff --git a/pkgs/mcpx/internal/e2e/plugin_test.go b/pkgs/mcpx/internal/e2e/plugin_test.go index 52c9488977..a01feaad50 100644 --- a/pkgs/mcpx/internal/e2e/plugin_test.go +++ b/pkgs/mcpx/internal/e2e/plugin_test.go @@ -2,6 +2,7 @@ package e2e_test import ( "encoding/json" + "github.com/developing-today/code/pkgs/mcpx/internal/testsupport" "os/exec" "strings" "testing" @@ -16,10 +17,7 @@ import ( // real one proves the route reads it -- the difference between the two was // DaemonClient.call(), which sent its arguments under a key /v1/call ignores. func TestThePluginSuiteRunsAgainstARealDaemon(t *testing.T) { - bun, err := exec.LookPath("bun") - if err != nil { - t.Skip("bun is not installed") - } + bun := testsupport.RequireBinary(t, "bun") e := newEnv(t, oneServer) e.run("ls") var where struct { diff --git a/pkgs/mcpx/internal/e2e/runtime_test.go b/pkgs/mcpx/internal/e2e/runtime_test.go index d636a66c6f..6e76f8c650 100644 --- a/pkgs/mcpx/internal/e2e/runtime_test.go +++ b/pkgs/mcpx/internal/e2e/runtime_test.go @@ -2,6 +2,7 @@ package e2e_test import ( "errors" + "github.com/developing-today/code/pkgs/mcpx/internal/testsupport" "os/exec" "strings" "testing" @@ -15,9 +16,7 @@ func TestGeneratedClientWorksOnEveryRuntime(t *testing.T) { for _, rt := range []string{"deno", "bun", "node"} { rt := rt t.Run(rt, func(t *testing.T) { - if _, err := exec.LookPath(rt); err != nil { - t.Skipf("%s not installed", rt) - } + testsupport.RequireBinary(t, rt) e := newEnv(t, oneServer) // MCPX_RUNTIME is what the runner actually sets; read it the way // a script would, from the process environment rather than from a @@ -47,9 +46,7 @@ func TestSessionIsolationHoldsOnEveryRuntime(t *testing.T) { for _, rt := range []string{"deno", "bun", "node"} { rt := rt t.Run(rt, func(t *testing.T) { - if _, err := exec.LookPath(rt); err != nil { - t.Skipf("%s not installed", rt) - } + testsupport.RequireBinary(t, rt) e := newEnv(t, statefulServer) // Two sequential runs with explicit, distinct sessions must not // see each other's writes. @@ -80,9 +77,7 @@ func TestSessionIsolationHoldsOnEveryRuntime(t *testing.T) { func TestDenoExitWorksOnEveryRuntime(t *testing.T) { for _, rt := range []string{"deno", "bun", "node"} { t.Run(rt, func(t *testing.T) { - if _, err := exec.LookPath(rt); err != nil { - t.Skipf("%s not installed", rt) - } + testsupport.RequireBinary(t, rt) e := newEnv(t, oneServer) out, err := e.try("exec", "--runtime", rt, `console.log("before"); (globalThis as any).Deno.exit(3); console.log("after");`) diff --git a/pkgs/mcpx/internal/e2e/skill_examples_test.go b/pkgs/mcpx/internal/e2e/skill_examples_test.go index 6ece80e444..44eaeee265 100644 --- a/pkgs/mcpx/internal/e2e/skill_examples_test.go +++ b/pkgs/mcpx/internal/e2e/skill_examples_test.go @@ -2,6 +2,7 @@ package e2e_test import ( "errors" + "github.com/developing-today/code/pkgs/mcpx/internal/testsupport" "os" "os/exec" "path/filepath" @@ -70,9 +71,7 @@ func TestSkillExamplesRun(t *testing.T) { for _, rt := range []string{"deno", "bun", "node"} { t.Run(rt, func(t *testing.T) { - if _, err := exec.LookPath(rt); err != nil { - t.Skipf("%s not installed", rt) - } + testsupport.RequireBinary(t, rt) e := newEnv(t, oneServer) dir := filepath.Join(e.dir, "examples") if err := os.MkdirAll(dir, 0o755); err != nil { diff --git a/pkgs/mcpx/internal/testsupport/require.go b/pkgs/mcpx/internal/testsupport/require.go new file mode 100644 index 0000000000..7ca9ab8652 --- /dev/null +++ b/pkgs/mcpx/internal/testsupport/require.go @@ -0,0 +1,53 @@ +package testsupport + +import ( + "os" + "os/exec" + "strings" + "testing" +) + +// MCPX_TEST_REQUIRE names the external binaries a test run must have, as a +// comma-separated list (e.g. MCPX_TEST_REQUIRE=deno,bun,node,python3). +// +// A missing runtime normally skips the tests that need it, which is right +// on a laptop and wrong in CI: a leg that skips is a green leg that tested +// nothing (developing-today/code#1983, the #35 lesson). CI and container +// runs set MCPX_TEST_REQUIRE so a missing required binary fails the test +// instead of silently narrowing what the suite covered. +func RequiredBinaries() map[string]bool { + out := map[string]bool{} + for _, name := range strings.Split(os.Getenv("MCPX_TEST_REQUIRE"), ",") { + if name = strings.ToLower(strings.TrimSpace(name)); name != "" { + out[name] = true + } + } + return out +} + +// MissingRequired reports whether name is missing from PATH while +// MCPX_TEST_REQUIRE names it -- the one case where a test that cannot run +// must fail rather than skip or continue past the gap. +func MissingRequired(name string) bool { + if !RequiredBinaries()[strings.ToLower(name)] { + return false + } + _, err := exec.LookPath(name) + return err != nil +} + +// RequireBinary returns the path of name on PATH. When name is missing it +// skips the test, unless MCPX_TEST_REQUIRE names it, in which case the +// test fails: the environment promised the binary and did not provide it. +func RequireBinary(t testing.TB, name string) string { + t.Helper() + path, err := exec.LookPath(name) + if err == nil { + return path + } + if MissingRequired(name) { + t.Fatalf("%s is required by MCPX_TEST_REQUIRE but is not on PATH: %v", name, err) + } + t.Skipf("%s not installed", name) + return "" +} diff --git a/pkgs/mcpx/internal/testsupport/require_test.go b/pkgs/mcpx/internal/testsupport/require_test.go new file mode 100644 index 0000000000..72fe79a10a --- /dev/null +++ b/pkgs/mcpx/internal/testsupport/require_test.go @@ -0,0 +1,90 @@ +package testsupport + +import ( + "strings" + "testing" +) + +func TestRequiredBinariesParsesTheList(t *testing.T) { + t.Setenv("MCPX_TEST_REQUIRE", "deno, bun ,NODE,, python3 ") + got := RequiredBinaries() + for _, want := range []string{"deno", "bun", "node", "python3"} { + if !got[want] { + t.Errorf("%q should be required, parsed %v", want, got) + } + } + if len(got) != 4 { + t.Errorf("want exactly 4 required binaries, got %v", got) + } +} + +func TestRequiredBinariesEmptyWhenUnset(t *testing.T) { + t.Setenv("MCPX_TEST_REQUIRE", "") + if got := RequiredBinaries(); len(got) != 0 { + t.Errorf("unset MCPX_TEST_REQUIRE should require nothing, got %v", got) + } +} + +// requireTB records which of Skipf/Fatalf a RequireBinary call used. +type requireTB struct { + testing.TB + skipped, fataled string +} + +func (r *requireTB) Helper() {} +func (r *requireTB) Skipf(format string, args ...any) { + r.skipped = format + panic("skip") +} +func (r *requireTB) Fatalf(format string, args ...any) { + r.fataled = format + panic("fatal") +} + +func callRequire(name string) (tb *requireTB) { + tb = &requireTB{} + defer func() { _ = recover() }() + RequireBinary(tb, name) + return tb +} + +func TestRequireBinarySkipsAMissingOptionalBinary(t *testing.T) { + t.Setenv("MCPX_TEST_REQUIRE", "deno") + tb := callRequire("mcpx-no-such-binary-5584") + if tb.fataled != "" || !strings.Contains(tb.skipped, "not installed") { + t.Errorf("missing optional binary should skip, got skip=%q fatal=%q", tb.skipped, tb.fataled) + } +} + +func TestRequireBinaryFailsAMissingRequiredBinary(t *testing.T) { + t.Setenv("MCPX_TEST_REQUIRE", "mcpx-no-such-binary-5584") + tb := callRequire("mcpx-no-such-binary-5584") + if tb.skipped != "" || !strings.Contains(tb.fataled, "MCPX_TEST_REQUIRE") { + t.Errorf("missing required binary should fail naming MCPX_TEST_REQUIRE, got skip=%q fatal=%q", tb.skipped, tb.fataled) + } +} + +func TestRequireBinaryReturnsThePathOfAPresentBinary(t *testing.T) { + t.Setenv("MCPX_TEST_REQUIRE", "") + tb := &requireTB{} + if path := RequireBinary(tb, "go"); path == "" { + t.Skip("go not on PATH on this machine; the present-binary path is covered in CI") + } + if tb.skipped != "" || tb.fataled != "" { + t.Errorf("present binary should neither skip nor fail, got skip=%q fatal=%q", tb.skipped, tb.fataled) + } +} + +func TestMissingRequired(t *testing.T) { + t.Setenv("MCPX_TEST_REQUIRE", "mcpx-no-such-binary-5584") + if !MissingRequired("mcpx-no-such-binary-5584") { + t.Error("a required binary that is absent should report missing-required") + } + if MissingRequired("go") { + t.Error("a binary nobody required should not report missing-required") + } + t.Setenv("MCPX_TEST_REQUIRE", "") + if MissingRequired("mcpx-no-such-binary-5584") { + t.Error("with MCPX_TEST_REQUIRE unset, nothing is missing-required") + } +}