diff --git a/internal/plans/policy.go b/internal/plans/policy.go index 2d2f2ea1ef..f899329671 100644 --- a/internal/plans/policy.go +++ b/internal/plans/policy.go @@ -13,7 +13,7 @@ import ( "github.com/hashicorp/terraform/internal/policy" ) -// PolicyResults represents the results of policy evaluation of resources and providers for a single plan. +// PolicyResults represents the results of policy evaluation of resources, modules, and providers for a single plan. type PolicyResults struct { mu *sync.Mutex // Diagnostics holds diagnostics not tied to any policy @@ -23,6 +23,7 @@ type PolicyResults struct { mset addrs.Map[addrs.Module, PolicyEvaluation] } +// PolicyEvaluation holds the result of a policy evaluation for a single resource, module, or provider. type PolicyEvaluation struct { EvaluationResponse policy.EvaluationResponse ConfigDeclRange hcl.Range diff --git a/internal/tfdiags/contextual.go b/internal/tfdiags/contextual.go index 5446199394..a0e360a347 100644 --- a/internal/tfdiags/contextual.go +++ b/internal/tfdiags/contextual.go @@ -62,52 +62,6 @@ func (diags Diagnostics) InConfigBody(body hcl.Body, addr string) Diagnostics { return ret } -// WithSubject returns a copy of the receiver with the given range as the source information -// for all diagnostics. Only hcl-based diagnostics are affected. -func (diags Diagnostics) WithSubject(rng *hcl.Range) Diagnostics { - if len(diags) == 0 || rng == nil { - return nil - } - - ret := make(Diagnostics, len(diags)) - for i, srcDiag := range diags { - ret[i] = diagWithSubject(srcDiag, rng) - if diag, isOverride := srcDiag.(overriddenDiagnostic); isOverride { - ret[i] = overriddenDiagnostic{ - original: diagWithSubject(diag.original, rng), - severity: diag.severity, - extra: diag.extra, - } - } - } - - return ret -} - -func diagWithSubject(diag Diagnostic, rng *hcl.Range) Diagnostic { - if rng == nil { - return nil - } - - if hclDiag, isHCL := diag.(hclDiagnostic); isHCL { - if hclDiag.diag.Subject == nil { - hclDiag.diag.Subject = rng - } - - // If there is a context, it needs to contain the subject, otherwise - // we set it to the subject. - if hclDiag.diag.Context != nil { - contains := hclDiag.diag.Context.Overlaps(*hclDiag.diag.Subject) - if !contains { - hclDiag.diag.Context = rng - } - } - return hclDiag - } - - return diag -} - // AttributeValue returns a diagnostic about an attribute value in an implied current // configuration context. This should be returned only from functions whose // interface specifies a clear configuration context that this will be diff --git a/internal/tfdiags/contextual_test.go b/internal/tfdiags/contextual_test.go index da37fdc04c..300f8ad777 100644 --- a/internal/tfdiags/contextual_test.go +++ b/internal/tfdiags/contextual_test.go @@ -9,7 +9,6 @@ import ( "testing" "github.com/go-test/deep" - "github.com/google/go-cmp/cmp" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/zclconf/go-cty/cty" @@ -585,123 +584,6 @@ simple_attr = "val" } } -func TestWithSubject(t *testing.T) { - subject := &hcl.Range{ - Filename: "test.tf", - Start: hcl.Pos{Line: 2, Column: 3, Byte: 12}, - End: hcl.Pos{Line: 2, Column: 7, Byte: 16}, - } - containingContext := &hcl.Range{ - Filename: "test.tf", - Start: hcl.Pos{Line: 2, Column: 1, Byte: 10}, - End: hcl.Pos{Line: 2, Column: 10, Byte: 19}, - } - nonContainingContext := &hcl.Range{ - Filename: "test.tf", - Start: hcl.Pos{Line: 10, Column: 1, Byte: 100}, - End: hcl.Pos{Line: 10, Column: 5, Byte: 104}, - } - existingSubject := &hcl.Range{ - Filename: "other.tf", - Start: hcl.Pos{Line: 1, Column: 1, Byte: 0}, - End: hcl.Pos{Line: 1, Column: 2, Byte: 1}, - } - - testCases := []struct { - name string - diag Diagnostic - assert func(t *testing.T, got Diagnostic) - }{ - { - name: "adds subject to hcl diagnostic with no source", - diag: hclDiagnostic{diag: &hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: "summary", - Detail: "detail", - }}, - assert: func(t *testing.T, got Diagnostic) { - t.Helper() - hclDiag := got.(hclDiagnostic) - if diff := cmp.Diff(subject, hclDiag.diag.Subject); diff != "" { - t.Fatalf("wrong subject\nwant: %#v\ngot: %#v", subject, hclDiag.diag.Subject) - } - if hclDiag.diag.Context != nil { - t.Fatalf("expected nil context, got %#v", hclDiag.diag.Context) - } - }, - }, - { - name: "preserves existing subject but replaces non-overlapping context", - diag: hclDiagnostic{diag: &hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: "summary", - Detail: "detail", - Subject: existingSubject, - Context: containingContext, - }}, - assert: func(t *testing.T, got Diagnostic) { - t.Helper() - hclDiag := got.(hclDiagnostic) - if diff := cmp.Diff(existingSubject, hclDiag.diag.Subject); diff != "" { - t.Fatalf("wrong subject\nwant: %#v\ngot: %#v", existingSubject, hclDiag.diag.Subject) - } - if diff := cmp.Diff(subject, hclDiag.diag.Context); diff != "" { - t.Fatalf("wrong context\nwant: %#v\ngot: %#v", subject, hclDiag.diag.Context) - } - }, - }, - { - name: "leaves non hcl diagnostic unchanged", - diag: Sourceless(Error, "summary", "detail"), - assert: func(t *testing.T, got Diagnostic) { - t.Helper() - if got.Source().Subject != nil { - t.Fatalf("expected nil subject, got %#v", got.Source().Subject) - } - }, - }, - { - name: "updates wrapped original in overridden diagnostic", - diag: Override(hclDiagnostic{diag: &hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: "summary", - Detail: "detail", - Context: nonContainingContext, - }}, Warning, nil), - assert: func(t *testing.T, got Diagnostic) { - t.Helper() - override, ok := got.(overriddenDiagnostic) - if !ok { - t.Fatalf("wrong diagnostic type %T", got) - } - if override.Severity() != Warning { - t.Fatalf("wrong severity %s", override.Severity()) - } - hclDiag, ok := override.original.(hclDiagnostic) - if !ok { - t.Fatalf("wrong wrapped diagnostic type %T", override.original) - } - if !reflect.DeepEqual(subject, hclDiag.diag.Subject) { - t.Fatalf("wrong subject\nwant: %#v\ngot: %#v", subject, hclDiag.diag.Subject) - } - if !reflect.DeepEqual(subject, hclDiag.diag.Context) { - t.Fatalf("wrong context\nwant: %#v\ngot: %#v", subject, hclDiag.diag.Context) - } - }, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - got := Diagnostics{tc.diag}.WithSubject(subject) - if len(got) != 1 { - t.Fatalf("wrong number of diagnostics %d", len(got)) - } - tc.assert(t, got[0]) - }) - } -} - func TestGetAttribute(t *testing.T) { path := cty.Path{ cty.GetAttrStep{Name: "foo"},