@@ -3,35 +3,100 @@ package testsupport |
3 | 3 | import ( |
4 | 4 | "os" |
5 | 5 | "os/exec" |
| 6 | + "sort" |
6 | 7 | "strings" |
7 | 8 | "testing" |
8 | 9 | ) |
9 | 10 |
|
10 | | -// RequiredBinaries returns the external binaries a test run must have, as named by MCPX_TEST_REQUIRE. |
| 11 | +// requireList is MCPX_TEST_REQUIRE parsed once (#6904). |
11 | 12 | // |
12 | | -// MCPX_TEST_REQUIRE names the external binaries a test run must have, as a |
| 13 | +// MCPX_TEST_REQUIRE names the external things a test run must have, as a |
13 | 14 | // comma-separated list (e.g. MCPX_TEST_REQUIRE=deno,bun,node,python3). |
| 15 | +// Runtime binaries and real-software servers share the one variable, so |
| 16 | +// there is exactly one parser for it: entries are trimmed and lower-cased, |
| 17 | +// and the three non-binary spellings are stated here and nowhere else: |
| 18 | +// |
| 19 | +// - "realsoftware" and "real" name the real-server subset: every real |
| 20 | +// server is demanded. |
| 21 | +// - "*" is the wildcard: every real server is demanded. It does not |
| 22 | +// demand runtime binaries, which have no wildcard meaning. |
| 23 | +// - any other entry names a runtime binary (checked on PATH) and, when a |
| 24 | +// real server has the same name, that server too. |
| 25 | +// |
| 26 | +// Case is insignificant in every position: "RealSoftware", "REALSOFTWARE" |
| 27 | +// and "realsoftware" mean the same thing, for binaries and servers alike. |
| 28 | +type requireList struct { |
| 29 | + binaries map[string]bool // lower-cased runtime binary names demanded |
| 30 | + realAll bool // a subset label or the wildcard demands every real server |
| 31 | +} |
| 32 | + |
| 33 | +// parseRequireList is the single parser for MCPX_TEST_REQUIRE. |
| 34 | +func parseRequireList(raw string) requireList { |
| 35 | + l := requireList{binaries: map[string]bool{}} |
| 36 | + for _, entry := range strings.Split(raw, ",") { |
| 37 | + switch entry = strings.ToLower(strings.TrimSpace(entry)); entry { |
| 38 | + case "": |
| 39 | + case "realsoftware", "real", "*": |
| 40 | + l.realAll = true |
| 41 | + default: |
| 42 | + l.binaries[entry] = true |
| 43 | + } |
| 44 | + } |
| 45 | + return l |
| 46 | +} |
| 47 | + |
| 48 | +// demandsBinary reports whether the list names name as a runtime binary. |
| 49 | +func (l requireList) demandsBinary(name string) bool { |
| 50 | + return l.binaries[strings.ToLower(strings.TrimSpace(name))] |
| 51 | +} |
| 52 | + |
| 53 | +// demandsRealServer reports whether the list demands the named real server: |
| 54 | +// an entry naming the server itself, a subset label, or the wildcard. |
| 55 | +func (l requireList) demandsRealServer(name string) bool { |
| 56 | + return l.realAll || l.demandsBinary(name) |
| 57 | +} |
| 58 | + |
| 59 | +// RequiredBinaries returns the external binaries a test run must have, as |
| 60 | +// named by MCPX_TEST_REQUIRE. |
14 | 61 | // |
15 | 62 | // A missing runtime normally skips the tests that need it, which is right |
16 | 63 | // on a laptop and wrong in CI: a leg that skips is a green leg that tested |
17 | 64 | // nothing (developing-today/code#1983, the #35 lesson). CI and container |
18 | 65 | // runs set MCPX_TEST_REQUIRE so a missing required binary fails the test |
19 | 66 | // instead of silently narrowing what the suite covered. |
| 67 | +// |
| 68 | +// The real-server subset labels and the wildcard are not binaries and are |
| 69 | +// not returned here; see requireList for the rules. |
20 | 70 | func RequiredBinaries() map[string]bool { |
21 | | - out := map[string]bool{} |
22 | | - for _, name := range strings.Split(os.Getenv("MCPX_TEST_REQUIRE"), ",") { |
23 | | - if name = strings.ToLower(strings.TrimSpace(name)); name != "" { |
24 | | - out[name] = true |
25 | | - } |
| 71 | + parsed := parseRequireList(os.Getenv("MCPX_TEST_REQUIRE")) |
| 72 | + out := make(map[string]bool, len(parsed.binaries)) |
| 73 | + for name := range parsed.binaries { |
| 74 | + out[name] = true |
26 | 75 | } |
27 | 76 | return out |
28 | 77 | } |
29 | 78 |
|
| 79 | +// MissingRequiredBinaries returns the required binaries that are not on |
| 80 | +// PATH, sorted. It is the suite-level check (#6904): per-test enforcement |
| 81 | +// only covers entries that some test happens to pass to RequireBinary, so |
| 82 | +// an entry with no consumer -- python3 in CI's list -- would otherwise be |
| 83 | +// enforced by nothing and narrow the run silently. |
| 84 | +func MissingRequiredBinaries() []string { |
| 85 | + var missing []string |
| 86 | + for name := range RequiredBinaries() { |
| 87 | + if _, err := exec.LookPath(name); err != nil { |
| 88 | + missing = append(missing, name) |
| 89 | + } |
| 90 | + } |
| 91 | + sort.Strings(missing) |
| 92 | + return missing |
| 93 | +} |
| 94 | + |
30 | 95 | // MissingRequired reports whether name is missing from PATH while |
31 | 96 | // MCPX_TEST_REQUIRE names it -- the one case where a test that cannot run |
32 | 97 | // must fail rather than skip or continue past the gap. |
33 | 98 | func MissingRequired(name string) bool { |
34 | | - if !RequiredBinaries()[strings.ToLower(name)] { |
| 99 | + if !parseRequireList(os.Getenv("MCPX_TEST_REQUIRE")).demandsBinary(name) { |
35 | 100 | return false |
36 | 101 | } |
37 | 102 | _, err := exec.LookPath(name) |
|