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.
pull/34108/head
James Bardin 3 years ago
parent 84f375a092
commit 663cc1d1b7

@ -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.")

@ -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) {

Loading…
Cancel
Save