From 663cc1d1b706e31cf1e6a4c4c2da3bc9ced4bab5 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 18 Oct 2023 09:13:30 -0400 Subject: [PATCH] move invalid tests Schema attributes must have a type to be valid. These would have panicked anyway when used in other cases, but were passing within the limited test case. The update to hcl makes these panic earlier, so add our own panics to prevent invalid attribute schemas entirely. --- internal/configs/configschema/decoder_spec.go | 6 +-- .../configs/configschema/decoder_spec_test.go | 46 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/internal/configs/configschema/decoder_spec.go b/internal/configs/configschema/decoder_spec.go index e020895288..5fc6e77131 100644 --- a/internal/configs/configschema/decoder_spec.go +++ b/internal/configs/configschema/decoder_spec.go @@ -184,11 +184,11 @@ func (b *Block) DecoderSpec() hcldec.Spec { } func (a *Attribute) decoderSpec(name string) hcldec.Spec { - ret := &hcldec.AttrSpec{Name: name} - if a == nil { - return ret + if a == nil || (a.Type == cty.NilType && a.NestedType == nil) { + panic("Invalid attribute schema: schema is nil.") } + ret := &hcldec.AttrSpec{Name: name} if a.NestedType != nil { if a.Type != cty.NilType { panic("Invalid attribute schema: NestedType and Type cannot both be set. This is a bug in the provider.") diff --git a/internal/configs/configschema/decoder_spec_test.go b/internal/configs/configschema/decoder_spec_test.go index 111ce5fb9b..99d6e700d1 100644 --- a/internal/configs/configschema/decoder_spec_test.go +++ b/internal/configs/configschema/decoder_spec_test.go @@ -445,18 +445,6 @@ func TestAttributeDecoderSpec(t *testing.T) { Want cty.Value DiagCount int }{ - "empty": { - &Attribute{}, - hcl.EmptyBody(), - cty.NilVal, - 0, - }, - "nil": { - nil, - hcl.EmptyBody(), - cty.NilVal, - 0, - }, "optional string (null)": { &Attribute{ Type: cty.String, @@ -878,17 +866,33 @@ func TestAttributeDecoderSpec(t *testing.T) { // be removed when InternalValidate() is extended to validate Attribute specs // (and is used). See the #FIXME in decoderSpec. func TestAttributeDecoderSpec_panic(t *testing.T) { - attrS := &Attribute{ - Type: cty.Object(map[string]cty.Type{ - "nested_attribute": cty.String, - }), - NestedType: &Object{}, - Optional: true, + tests := map[string]struct { + Schema *Attribute + }{ + "empty": { + Schema: &Attribute{}, + }, + "nil": { + Schema: nil, + }, + "nested_attribute": { + Schema: &Attribute{ + Type: cty.Object(map[string]cty.Type{ + "nested_attribute": cty.String, + }), + NestedType: &Object{}, + Optional: true, + }, + }, } - defer func() { recover() }() - attrS.decoderSpec("attr") - t.Errorf("expected panic") + for name, test := range tests { + t.Run(name, func(t *testing.T) { + defer func() { recover() }() + test.Schema.decoderSpec("attr") + t.Errorf("expected panic") + }) + } } func TestListOptionalAttrsFromObject(t *testing.T) {