|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestDiscover_ExpectedTags fails when the shipped lite tag set changes — |
| 11 | +// a plugin added/removed or liteKeep edited. Deliberate changes update |
| 12 | +// the want string; unintended changes are caught here. |
| 13 | +func TestDiscover_ExpectedTags(t *testing.T) { |
| 14 | + tags, err := discover(defaultPluginsDir) |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("discover: %v", err) |
| 17 | + } |
| 18 | + got := strings.Join(tags, ",") |
| 19 | + want := "exclude_plugin_a2aparser,exclude_plugin_ibac,exclude_plugin_inferenceparser,exclude_plugin_mcpparser,exclude_plugin_opa,exclude_plugin_sparc,exclude_plugin_tokenbroker,exclude_plugin_toolprune" |
| 20 | + if got != want { |
| 21 | + t.Errorf("output changed\n got: %s\nwant: %s", got, want) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// TestDiscover_FailsClosedOnEmptyTags — an empty result would flow to |
| 26 | +// `go build -tags ""` and ship a full binary as "lite". |
| 27 | +func TestDiscover_FailsClosedOnEmptyTags(t *testing.T) { |
| 28 | + dir := t.TempDir() |
| 29 | + writePlugin(t, dir, "plugins_jwtvalidation.go", "!exclude_plugin_jwtvalidation") |
| 30 | + |
| 31 | + if _, err := discover(dir); err == nil { |
| 32 | + t.Fatal("want error when every plugin is in liteKeep, got nil") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// TestDiscover_CompoundBuildDirective — before dropping the $ anchor, |
| 37 | +// `!exclude_plugin_X && !Y` silently failed to match and the plugin |
| 38 | +// stayed in the lite build. |
| 39 | +func TestDiscover_CompoundBuildDirective(t *testing.T) { |
| 40 | + dir := t.TempDir() |
| 41 | + writePlugin(t, dir, "plugins_examplecompound.go", "!exclude_plugin_examplecompound && !nocgo") |
| 42 | + writePlugin(t, dir, "plugins_jwtvalidation.go", "!exclude_plugin_jwtvalidation") // keep-listed, prevents fail-closed |
| 43 | + |
| 44 | + tags, err := discover(dir) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("discover: %v", err) |
| 47 | + } |
| 48 | + if got := strings.Join(tags, ","); got != "exclude_plugin_examplecompound" { |
| 49 | + t.Errorf("got %q, want exclude_plugin_examplecompound", got) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// TestDiscover_SkipsTestFiles — the plugins_*.go glob would otherwise |
| 54 | +// scan a future plugins_foo_test.go as a plugin definition. |
| 55 | +func TestDiscover_SkipsTestFiles(t *testing.T) { |
| 56 | + dir := t.TempDir() |
| 57 | + writePlugin(t, dir, "plugins_ghost_test.go", "!exclude_plugin_ghost") |
| 58 | + writePlugin(t, dir, "plugins_a2aparser.go", "!exclude_plugin_a2aparser") |
| 59 | + |
| 60 | + tags, err := discover(dir) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("discover: %v", err) |
| 63 | + } |
| 64 | + if got := strings.Join(tags, ","); got != "exclude_plugin_a2aparser" { |
| 65 | + t.Errorf("got %q, want exclude_plugin_a2aparser (test file must be skipped)", got) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func writePlugin(t *testing.T, dir, name, buildConstraint string) { |
| 70 | + t.Helper() |
| 71 | + content := "//go:build " + buildConstraint + "\n\npackage main\n" |
| 72 | + if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { |
| 73 | + t.Fatalf("write %s: %v", name, err) |
| 74 | + } |
| 75 | +} |
0 commit comments