Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions pkgs/mcpx/internal/testsupport/realserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testsupport
import (
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -60,13 +59,9 @@ func realServerPath(dir, name string) (string, bool) {

// realRequired reports whether a MCPX_TEST_REQUIRE comma list demands the
// named server: an entry naming the server itself, the subset label, or
// the wildcard demands it.
// the wildcard demands it. Case is insignificant; the parsing and the
// label/wildcard rules live in requireList (require.go), the one parser
// for the variable (#6904).
func realRequired(requireList, name string) bool {
for _, entry := range strings.Split(requireList, ",") {
switch strings.TrimSpace(entry) {
case name, "realsoftware", "real", "*":
return true
}
}
return false
return parseRequireList(requireList).demandsRealServer(name)
}
102 changes: 94 additions & 8 deletions pkgs/mcpx/internal/testsupport/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,121 @@ package testsupport
import (
"os"
"os/exec"
"sort"
"strings"
"testing"
)

// RequiredBinaries returns the external binaries a test run must have, as named by MCPX_TEST_REQUIRE.
// requireList is MCPX_TEST_REQUIRE parsed once (#6904).
//
// MCPX_TEST_REQUIRE names the external binaries a test run must have, as a
// MCPX_TEST_REQUIRE names the external things a test run must have, as a
// comma-separated list (e.g. MCPX_TEST_REQUIRE=deno,bun,node,python3).
// Runtime binaries and real-software servers share the one variable, so
// there is exactly one parser for it: entries are trimmed and lower-cased,
// and the three non-binary spellings are stated here and nowhere else:
//
// - "realsoftware" and "real" name the real-server subset: every real
// server is demanded.
// - "*" is the wildcard: every real server is demanded. It does not
// demand runtime binaries, which have no wildcard meaning.
// - any other entry names a runtime binary (checked on PATH) and, when a
// real server has the same name, that server too.
//
// Case is insignificant in every position: "RealSoftware", "REALSOFTWARE"
// and "realsoftware" mean the same thing, for binaries and servers alike.
type requireList struct {
binaries map[string]bool // lower-cased runtime binary names demanded
realAll bool // a subset label or the wildcard demands every real server
}

// parseRequireList is the single parser for MCPX_TEST_REQUIRE.
func parseRequireList(raw string) requireList {
l := requireList{binaries: map[string]bool{}}
for _, entry := range strings.Split(raw, ",") {
switch entry = strings.ToLower(strings.TrimSpace(entry)); entry {
case "":
case "realsoftware", "real", "*":
l.realAll = true
default:
l.binaries[entry] = true
}
}
return l
}

// demandsBinary reports whether the list names name as a runtime binary.
func (l requireList) demandsBinary(name string) bool {
return l.binaries[strings.ToLower(strings.TrimSpace(name))]
}

// demandsRealServer reports whether the list demands the named real server:
// an entry naming the server itself, a subset label, or the wildcard.
func (l requireList) demandsRealServer(name string) bool {
return l.realAll || l.demandsBinary(name)
}

// RequiredBinaries returns the external binaries a test run must have, as
// named by MCPX_TEST_REQUIRE.
//
// 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.
//
// The real-server subset labels and the wildcard are not binaries and are
// not returned here; see requireList for the rules.
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
}
parsed := parseRequireList(os.Getenv("MCPX_TEST_REQUIRE"))
out := make(map[string]bool, len(parsed.binaries))
for name := range parsed.binaries {
out[name] = true
}
return out
}

// MissingRequiredBinaries returns the required entries that are not
// available, sorted. It is the suite-level check (#6904): per-test
// enforcement only covers entries that some test happens to pass to
// RequireBinary, so an entry with no consumer -- python3 in CI's list --
// would otherwise be enforced by nothing and narrow the run silently.
//
// An entry is available when it is on PATH, or when it names a real
// server that resolves under MCPX_REAL_SERVERS_DIR: entries name real
// servers as well as runtime binaries (see requireList), and RealServer
// resolves those from the configured directory, not from PATH, so
// demanding PATH presence for them would reject a valid configuration.
func MissingRequiredBinaries() []string {
var missing []string
for name := range RequiredBinaries() {
if !requiredEntryAvailable(name) {
missing = append(missing, name)
Comment thread
dezren39 marked this conversation as resolved.
Resolved
}
}
sort.Strings(missing)
return missing
}

// requiredEntryAvailable reports whether a required entry can be
// satisfied in this environment: on PATH as a runtime binary, or under
// MCPX_REAL_SERVERS_DIR as a real server (realServerPath's layouts).
func requiredEntryAvailable(name string) bool {
if _, err := exec.LookPath(name); err == nil {
return true
}
if dir := os.Getenv("MCPX_REAL_SERVERS_DIR"); dir != "" {
if _, ok := realServerPath(dir, name); ok {
return true
}
}
return false
}

// 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)] {
if !parseRequireList(os.Getenv("MCPX_TEST_REQUIRE")).demandsBinary(name) {
return false
}
_, err := exec.LookPath(name)
Expand Down
20 changes: 20 additions & 0 deletions pkgs/mcpx/internal/testsupport/require_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package testsupport

import (
"strings"
"testing"
)

// TestRequiredBinariesAreOnPath is the suite-level enforcement for
// MCPX_TEST_REQUIRE (#6904).
//
// Per-test enforcement only covers an entry that some test passes to
// RequireBinary: an entry with no consumer -- python3 in CI's list -- was
// checked by nothing, so a missing python3 narrowed the run silently.
// This test walks the whole list, so every entry in CI's list is enforced
// even when no individual test names it.
func TestRequiredBinariesAreOnPath(t *testing.T) {
if missing := MissingRequiredBinaries(); len(missing) > 0 {
t.Fatalf("MCPX_TEST_REQUIRE lists entries that are not available (on PATH or under MCPX_REAL_SERVERS_DIR): %s", strings.Join(missing, ", "))
}
}
71 changes: 71 additions & 0 deletions pkgs/mcpx/internal/testsupport/require_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testsupport

import (
"path/filepath"
"strings"
"testing"
)
Expand Down Expand Up @@ -88,3 +89,73 @@ func TestMissingRequired(t *testing.T) {
t.Error("with MCPX_TEST_REQUIRE unset, nothing is missing-required")
}
}

// TestRequireListCaseIsInsignificantEverywhere pins #6904: the one parser
// lower-cases every entry, so a spelling that works for a runtime binary
// means the same thing for a real server. Before the shared parser,
// MCPX_TEST_REQUIRE=RealSoftware demanded a binary named "realsoftware"
// (failing a missing runtime) while demanding no real server at all.
func TestRequireListCaseIsInsignificantEverywhere(t *testing.T) {
for _, raw := range []string{"RealSoftware", "REALSOFTWARE", "realsoftware", " Real , * "} {
parsed := parseRequireList(raw)
if !parsed.demandsRealServer("mcp-server-time") {
t.Errorf("%q should demand every real server", raw)
}
}
for _, tc := range []struct{ raw, binary string }{{"NODE", "node"}, {" Node ,BUN ", "node"}, {" Node ,BUN ", "bun"}} {
if !parseRequireList(tc.raw).demandsBinary(tc.binary) {
t.Errorf("%q should demand %q", tc.raw, tc.binary)
}
}
if got := parseRequireList("RealSoftware"); got.demandsBinary("realsoftware") {
t.Error("a subset label is not a runtime binary name")
}
if got := parseRequireList("*"); got.demandsBinary("*") {
t.Error("the wildcard is not a runtime binary name")
}
}

// TestRequiredBinariesExcludesLabelsAndWildcard pins that the label and
// wildcard spellings never leak into the PATH check as binary names.
func TestRequiredBinariesExcludesLabelsAndWildcard(t *testing.T) {
t.Setenv("MCPX_TEST_REQUIRE", "realsoftware,real,*,deno")
got := RequiredBinaries()
if len(got) != 1 || !got["deno"] {
t.Errorf("want only deno as a binary, got %v", got)
}
}

// TestMissingRequiredBinariesIsTheSuiteLevelCheck pins the enforcement
// that #6904 found missing: every listed binary is checked against PATH,
// whether or not a test happens to pass it to RequireBinary.
func TestMissingRequiredBinariesIsTheSuiteLevelCheck(t *testing.T) {
t.Setenv("MCPX_TEST_REQUIRE", "go,mcpx-no-such-binary-5584,realsoftware")
got := MissingRequiredBinaries()
if len(got) != 1 || got[0] != "mcpx-no-such-binary-5584" {
t.Errorf("want the absent binary named and the present one not, got %v", got)
}
t.Setenv("MCPX_TEST_REQUIRE", "")
if got := MissingRequiredBinaries(); len(got) != 0 {
t.Errorf("with MCPX_TEST_REQUIRE unset nothing is missing, got %v", got)
}
}

// TestMissingRequiredBinariesHonorsRealServersDir pins the other half of
// the availability rule: an entry naming a real server is satisfied by
// the server resolving under MCPX_REAL_SERVERS_DIR, because RealServer
// looks there, not on PATH. Only an entry available in neither place is
// missing.
func TestMissingRequiredBinariesHonorsRealServersDir(t *testing.T) {
dir := t.TempDir()
writeExecutable(t, filepath.Join(dir, "bin", "mcp-server-time"))
t.Setenv("MCPX_REAL_SERVERS_DIR", dir)
t.Setenv("MCPX_TEST_REQUIRE", "mcp-server-time,mcp-server-absent-5584")
got := MissingRequiredBinaries()
if len(got) != 1 || got[0] != "mcp-server-absent-5584" {
t.Errorf("want only the server available in neither place named, got %v", got)
}
t.Setenv("MCPX_REAL_SERVERS_DIR", "")
if got := MissingRequiredBinaries(); len(got) != 2 {
t.Errorf("with no servers dir both entries are missing, got %v", got)
}
}
Loading