registry: path kinds test reads the kinds from Route.Kind's comment
The kinds list was hard-coded, so a kind added to the Route.Kind comment without a PathEligibilityCases row still passed. Parse registry.go with go/parser, split the field's line comment on "|", and add KindForward.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+41
−5
internal/registry/pathroute_test.go+41 −5
diff --git a/internal/registry/pathroute_test.go b/internal/registry/pathroute_test.go| index 0472e4e8a..6dbcd255f 100644 |
| --- a/internal/registry/pathroute_test.go |
| +++ b/internal/registry/pathroute_test.go |
| @@ -1,6 +1,12 @@ |
| package registry |
| -import "testing" |
| +import ( |
| + "go/ast" |
| + "go/parser" |
| + "go/token" |
| + "strings" |
| + "testing" |
| +) |
| // TestPathEligibility runs the shared case table through both predicates. |
| func TestPathEligibility(t *testing.T) { |
| @@ -18,11 +24,12 @@ func TestPathEligibility(t *testing.T) { |
| // TestPathPredicatesNameEveryKind fails when a route kind has no row in |
| // PathEligibilityCases, so a new kind is classified before it can be a |
| -// path front or target by accident. The kinds are those named in the |
| -// comment on Route.Kind in registry.go, plus KindForward (forward.go); add |
| -// a new kind here when that comment gains one. |
| +// path front or target by accident. The kinds are parsed from the line |
| +// comment on Route.Kind in registry.go ("instance | service | ..."), plus |
| +// KindForward (forward.go), so adding a kind to that comment fails here |
| +// until the table gains a row for it. |
| func TestPathPredicatesNameEveryKind(t *testing.T) { |
| - kinds := []string{"instance", "service", "static", "sidecar", KindForward} |
| + kinds := append(routeKindsFromComment(t), KindForward) |
| seen := map[string]bool{} |
| for _, c := range PathEligibilityCases { |
| seen[c.Route.Kind] = true |
| @@ -33,3 +40,32 @@ func TestPathPredicatesNameEveryKind(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// routeKindsFromComment reads the kinds listed in the line comment on the |
| +// Kind field of type Route in registry.go. |
| +func routeKindsFromComment(t *testing.T) []string { |
| + t.Helper() |
| + f, err := parser.ParseFile(token.NewFileSet(), "registry.go", nil, parser.ParseComments) |
| + if err != nil { |
| + t.Fatalf("parse registry.go: %v", err) |
| + } |
| + var kinds []string |
| + ast.Inspect(f, func(n ast.Node) bool { |
| + ts, ok := n.(*ast.TypeSpec) |
| + if !ok || ts.Name.Name != "Route" { |
| + return true |
| + } |
| + for _, fld := range ts.Type.(*ast.StructType).Fields.List { |
| + if len(fld.Names) == 1 && fld.Names[0].Name == "Kind" && fld.Comment != nil { |
| + for _, k := range strings.Split(fld.Comment.Text(), "|") { |
| + kinds = append(kinds, strings.TrimSpace(k)) |
| + } |
| + } |
| + } |
| + return false |
| + }) |
| + if len(kinds) < 2 { |
| + t.Fatalf("no kinds parsed from the Route.Kind comment in registry.go (got %q)", kinds) |
| + } |
| + return kinds |
| +} |