|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/terraform-providers/terraform-provider-datadog/generator/internal/model" |
| 9 | +) |
| 10 | + |
| 11 | +// trackedOp builds a model.Operation carrying tracking metadata for the given |
| 12 | +// artifact name. The kind is irrelevant to duplicate detection. |
| 13 | +func trackedOp(path, method, operationId, artifactName string) *model.Operation { |
| 14 | + return &model.Operation{ |
| 15 | + Path: path, |
| 16 | + Method: method, |
| 17 | + OperationId: operationId, |
| 18 | + Tracking: &model.TrackingFieldMetadata{ |
| 19 | + ArtifactKind: model.ArtifactKindResource, |
| 20 | + ArtifactName: artifactName, |
| 21 | + }, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestCheckDuplicateArtifactNamesUnique(t *testing.T) { |
| 26 | + spec := &model.Spec{Operations: []*model.Operation{ |
| 27 | + trackedOp("/a", "GET", "GetA", "alpha"), |
| 28 | + trackedOp("/b", "GET", "GetB", "beta"), |
| 29 | + trackedOp("/c", "GET", "GetC", "gamma"), |
| 30 | + }} |
| 31 | + if err := CheckDuplicateArtifactNames(spec); err != nil { |
| 32 | + t.Fatalf("expected nil, got %v", err) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestCheckDuplicateArtifactNamesIgnoresUntracked(t *testing.T) { |
| 37 | + spec := &model.Spec{Operations: []*model.Operation{ |
| 38 | + trackedOp("/a", "GET", "GetA", "alpha"), |
| 39 | + {Path: "/health", Method: "GET", OperationId: "GetHealth"}, // Tracking nil |
| 40 | + nil, // defensive: nil entries are skipped |
| 41 | + trackedOp("/b", "GET", "GetB", "beta"), |
| 42 | + }} |
| 43 | + if err := CheckDuplicateArtifactNames(spec); err != nil { |
| 44 | + t.Fatalf("expected nil, got %v", err) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestCheckDuplicateArtifactNamesSingleCollision(t *testing.T) { |
| 49 | + spec := &model.Spec{Operations: []*model.Operation{ |
| 50 | + trackedOp("/teams", "GET", "ListTeams", "team"), |
| 51 | + trackedOp("/teams/{id}", "GET", "GetTeam", "team"), |
| 52 | + }} |
| 53 | + err := CheckDuplicateArtifactNames(spec) |
| 54 | + var dup *DuplicateArtifactNameError |
| 55 | + if !errors.As(err, &dup) { |
| 56 | + t.Fatalf("error %v (%T) is not a *DuplicateArtifactNameError", err, err) |
| 57 | + } |
| 58 | + if len(dup.Collisions) != 1 { |
| 59 | + t.Fatalf("got %d collisions, want 1", len(dup.Collisions)) |
| 60 | + } |
| 61 | + if len(dup.Collisions[0].Sources) != 2 { |
| 62 | + t.Fatalf("got %d sources, want 2", len(dup.Collisions[0].Sources)) |
| 63 | + } |
| 64 | + msg := dup.Error() |
| 65 | + for _, want := range []string{"team", "ListTeams", "GetTeam", "/teams", "/teams/{id}"} { |
| 66 | + if !strings.Contains(msg, want) { |
| 67 | + t.Errorf("error message missing %q:\n%s", want, msg) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestCheckDuplicateArtifactNamesListsAllSources(t *testing.T) { |
| 73 | + spec := &model.Spec{Operations: []*model.Operation{ |
| 74 | + trackedOp("/teams", "GET", "ListTeams", "team"), |
| 75 | + trackedOp("/teams/{id}", "GET", "GetTeam", "team"), |
| 76 | + trackedOp("/teams/search", "POST", "SearchTeams", "team"), |
| 77 | + }} |
| 78 | + err := CheckDuplicateArtifactNames(spec) |
| 79 | + var dup *DuplicateArtifactNameError |
| 80 | + if !errors.As(err, &dup) { |
| 81 | + t.Fatalf("error %v (%T) is not a *DuplicateArtifactNameError", err, err) |
| 82 | + } |
| 83 | + if n := len(dup.Collisions[0].Sources); n != 3 { |
| 84 | + t.Fatalf("got %d sources, want all 3 listed", n) |
| 85 | + } |
| 86 | + for _, want := range []string{"ListTeams", "GetTeam", "SearchTeams"} { |
| 87 | + if !strings.Contains(dup.Error(), want) { |
| 88 | + t.Errorf("error message missing source %q", want) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestCheckDuplicateArtifactNamesMultipleCollisionsSortedByName(t *testing.T) { |
| 94 | + spec := &model.Spec{Operations: []*model.Operation{ |
| 95 | + trackedOp("/z", "GET", "GetZ1", "zeta"), |
| 96 | + trackedOp("/z2", "GET", "GetZ2", "zeta"), |
| 97 | + trackedOp("/a", "GET", "GetA1", "alpha"), |
| 98 | + trackedOp("/a2", "GET", "GetA2", "alpha"), |
| 99 | + }} |
| 100 | + err := CheckDuplicateArtifactNames(spec) |
| 101 | + var dup *DuplicateArtifactNameError |
| 102 | + if !errors.As(err, &dup) { |
| 103 | + t.Fatalf("error %v (%T) is not a *DuplicateArtifactNameError", err, err) |
| 104 | + } |
| 105 | + if len(dup.Collisions) != 2 { |
| 106 | + t.Fatalf("got %d collisions, want 2", len(dup.Collisions)) |
| 107 | + } |
| 108 | + if dup.Collisions[0].Name != "alpha" || dup.Collisions[1].Name != "zeta" { |
| 109 | + t.Errorf("collisions not sorted by name: %q then %q", dup.Collisions[0].Name, dup.Collisions[1].Name) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestCheckDuplicateArtifactNamesDeterministic(t *testing.T) { |
| 114 | + // Same collisions, different declaration orders, must yield identical output. |
| 115 | + build := func(ops ...*model.Operation) *model.Spec { return &model.Spec{Operations: ops} } |
| 116 | + a := build( |
| 117 | + trackedOp("/teams", "GET", "ListTeams", "team"), |
| 118 | + trackedOp("/teams/{id}", "GET", "GetTeam", "team"), |
| 119 | + trackedOp("/users", "GET", "ListUsers", "user"), |
| 120 | + trackedOp("/users/{id}", "GET", "GetUser", "user"), |
| 121 | + ) |
| 122 | + b := build( |
| 123 | + trackedOp("/users/{id}", "GET", "GetUser", "user"), |
| 124 | + trackedOp("/teams/{id}", "GET", "GetTeam", "team"), |
| 125 | + trackedOp("/users", "GET", "ListUsers", "user"), |
| 126 | + trackedOp("/teams", "GET", "ListTeams", "team"), |
| 127 | + ) |
| 128 | + errA, errB := CheckDuplicateArtifactNames(a), CheckDuplicateArtifactNames(b) |
| 129 | + if errA == nil || errB == nil { |
| 130 | + t.Fatal("expected duplicate errors from both specs") |
| 131 | + } |
| 132 | + if errA.Error() != errB.Error() { |
| 133 | + t.Errorf("non-deterministic output:\nA:\n%s\nB:\n%s", errA.Error(), errB.Error()) |
| 134 | + } |
| 135 | +} |
0 commit comments