From 38033b5c45252215bcba79efde0cb09e77eb3e98 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Mon, 12 Aug 2024 14:54:32 +0200 Subject: [PATCH] stacks: sort diags in test before comparison (#35550) --- internal/stacks/stackruntime/apply_test.go | 40 ++++++--------------- internal/stacks/stackruntime/helper_test.go | 38 ++++++++++++++++++++ internal/stacks/stackruntime/plan_test.go | 5 +-- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/internal/stacks/stackruntime/apply_test.go b/internal/stacks/stackruntime/apply_test.go index da0d097c82..a75d4f86dd 100644 --- a/internal/stacks/stackruntime/apply_test.go +++ b/internal/stacks/stackruntime/apply_test.go @@ -1931,36 +1931,16 @@ func TestApplyWithChangedInputValues(t *testing.T) { t.Fatalf("expected exactly two diagnostics, got %s", applyDiags.ErrWithWarnings()) } - gotSeverity, wantSeverity := applyDiags[0].Severity(), tfdiags.Error - gotSummary, wantSummary := applyDiags[0].Description().Summary, "Inconsistent value for input variable during apply" - gotDetail, wantDetail := applyDiags[0].Description().Detail, "The value for non-ephemeral input variable \"input\" was set to a different value during apply than was set during plan. Only ephemeral input variables can change between the plan and apply phases." - - if gotSeverity != wantSeverity { - t.Errorf("expected severity %q, got %q", wantSeverity, gotSeverity) - } - if gotSummary != wantSummary { - t.Errorf("expected summary %q, got %q", wantSummary, gotSummary) - } - if gotDetail != wantDetail { - t.Errorf("expected detail %q, got %q", wantDetail, gotDetail) - } - - // We'll also receive a second error message here, since the invalid input - // has affected the component as well. - - gotSeverity, wantSeverity = applyDiags[1].Severity(), tfdiags.Error - gotSummary, wantSummary = applyDiags[1].Description().Summary, "Planned input variable value changed" - gotDetail, wantDetail = applyDiags[1].Description().Detail, "The planned value for input variable \"input\" has changed between the planning and apply phases for component.self. This is a bug in Terraform - please report it." - - if gotSeverity != wantSeverity { - t.Errorf("expected severity %q, got %q", wantSeverity, gotSeverity) - } - if gotSummary != wantSummary { - t.Errorf("expected summary %q, got %q", wantSummary, gotSummary) - } - if gotDetail != wantDetail { - t.Errorf("expected detail %q, got %q", wantDetail, gotDetail) - } + sort.SliceStable(applyDiags, diagnosticSortFunc(applyDiags)) + expectDiagnosticsForTest(t, applyDiags, + expectDiagnostic( + tfdiags.Error, + "Inconsistent value for input variable during apply", + "The value for non-ephemeral input variable \"input\" was set to a different value during apply than was set during plan. Only ephemeral input variables can change between the plan and apply phases."), + expectDiagnostic( + tfdiags.Error, + "Planned input variable value changed", + "The planned value for input variable \"input\" has changed between the planning and apply phases for component.self. This is a bug in Terraform - please report it.")) wantChanges := []stackstate.AppliedChange{ &stackstate.AppliedChangeComponentInstance{ diff --git a/internal/stacks/stackruntime/helper_test.go b/internal/stacks/stackruntime/helper_test.go index 1bc5c0a086..f8efceb0df 100644 --- a/internal/stacks/stackruntime/helper_test.go +++ b/internal/stacks/stackruntime/helper_test.go @@ -9,6 +9,7 @@ import ( "strings" "testing" + "github.com/google/go-cmp/cmp" "github.com/hashicorp/go-slug/sourceaddrs" "github.com/hashicorp/go-slug/sourcebundle" "github.com/zclconf/go-cty/cty" @@ -212,6 +213,43 @@ func plannedChangeSortKey(change stackplan.PlannedChange) string { } } +func diagnosticSortFunc(diags tfdiags.Diagnostics) func(i, j int) bool { + sortDescription := func(i, j tfdiags.Description) bool { + if i.Summary != j.Summary { + return i.Summary < j.Summary + } + return i.Detail < j.Detail + } + + sortPos := func(i, j tfdiags.SourcePos) bool { + if i.Line != j.Line { + return i.Line < j.Line + } + return i.Column < j.Column + } + + sortRange := func(i, j *tfdiags.SourceRange) bool { + if i.Filename != j.Filename { + return i.Filename < j.Filename + } + if !cmp.Equal(i.Start, j.Start) { + return sortPos(i.Start, j.Start) + } + return sortPos(i.End, j.End) + } + + return func(i, j int) bool { + id, jd := diags[i], diags[j] + if id.Severity() != jd.Severity() { + return id.Severity() == tfdiags.Error + } + if !cmp.Equal(id.Description(), jd.Description()) { + return sortDescription(id.Description(), jd.Description()) + } + return sortRange(id.Source().Subject, jd.Source().Subject) + } +} + func mustDefaultRootProvider(provider string) addrs.AbsProviderConfig { return addrs.AbsProviderConfig{ Module: addrs.RootModule, diff --git a/internal/stacks/stackruntime/plan_test.go b/internal/stacks/stackruntime/plan_test.go index af4c8a27c6..988452fa84 100644 --- a/internal/stacks/stackruntime/plan_test.go +++ b/internal/stacks/stackruntime/plan_test.go @@ -3350,9 +3350,10 @@ func TestPlanInvalidProvidersFailGracefully(t *testing.T) { go Plan(ctx, &req, &resp) changes, diags := collectPlanOutput(changesCh, diagsCh) + sort.SliceStable(diags, diagnosticSortFunc(diags)) expectDiagnosticsForTest(t, diags, - expectDiagnostic(tfdiags.Error, "invalid configuration", "configure_error attribute was set"), - expectDiagnostic(tfdiags.Error, "Provider configuration is invalid", "Cannot plan changes for this resource because its associated provider configuration is invalid.")) + expectDiagnostic(tfdiags.Error, "Provider configuration is invalid", "Cannot plan changes for this resource because its associated provider configuration is invalid."), + expectDiagnostic(tfdiags.Error, "invalid configuration", "configure_error attribute was set")) sort.SliceStable(changes, func(i, j int) bool { return plannedChangeSortKey(changes[i]) < plannedChangeSortKey(changes[j])