Skip to content

Commit 90ac5a8

Browse files
committed
Fix test strings
Worth noting expecting strings for expected errors is brittle should at least search for regex or substrings update all internal use to diags.Append
1 parent b72105a commit 90ac5a8

File tree

4 files changed

+68
-55
lines changed

4 files changed

+68
-55
lines changed

diag/diagnostic.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func (diags Diagnostics) Append(i interface{}) Diagnostics {
1616
if len(v) != 0 {
1717
diags = append(diags, v...)
1818
}
19+
case Diagnostic:
20+
diags = append(diags, &v)
1921
case *Diagnostic:
2022
diags = append(diags, v)
2123
case error:

helper/schema/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func (r *Resource) Validate(c *terraform.ResourceConfig) diag.Diagnostics {
441441
diags := schemaMap(r.Schema).Validate(c)
442442

443443
if r.DeprecationMessage != "" {
444-
diags = append(diags, &diag.Diagnostic{
444+
diags = diags.Append(&diag.Diagnostic{
445445
Severity: diag.Warning,
446446
Summary: "Deprecated Resource",
447447
Detail: r.DeprecationMessage,

helper/schema/schema.go

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,18 @@ func (s *Schema) validateFunc(decoded interface{}, k string, path cty.Path) diag
417417
diags = s.ValidateDiagFunc(decoded, k)
418418
for _, d := range diags {
419419
d.AttributePath = append(path.Copy(), d.AttributePath...)
420-
421420
}
422421
} else if s.ValidateFunc != nil {
423422
ws, es := s.ValidateFunc(decoded, k)
424423
for _, w := range ws {
425-
diags = append(diags, &diag.Diagnostic{
424+
diags = diags.Append(&diag.Diagnostic{
426425
Severity: diag.Warning,
427426
Summary: w,
428427
AttributePath: path,
429428
})
430429
}
431430
for _, e := range es {
432-
diags = append(diags, &diag.Diagnostic{
431+
diags = diags.Append(&diag.Diagnostic{
433432
Severity: diag.Error,
434433
Summary: e.Error(),
435434
AttributePath: path,
@@ -1375,7 +1374,7 @@ func (m schemaMap) validate(
13751374
var err error
13761375
raw, err = schema.DefaultFunc()
13771376
if err != nil {
1378-
return append(diags, &diag.Diagnostic{
1377+
return diags.Append(&diag.Diagnostic{
13791378
Severity: diag.Error,
13801379
Summary: "Loading Default",
13811380
Detail: err.Error(),
@@ -1389,7 +1388,7 @@ func (m schemaMap) validate(
13891388

13901389
err := validateExactlyOneAttribute(k, schema, c)
13911390
if err != nil {
1392-
return append(diags, &diag.Diagnostic{
1391+
return diags.Append(&diag.Diagnostic{
13931392
Severity: diag.Error,
13941393
Summary: "ExactlyOne",
13951394
Detail: err.Error(),
@@ -1399,7 +1398,7 @@ func (m schemaMap) validate(
13991398

14001399
err = validateAtLeastOneAttribute(k, schema, c)
14011400
if err != nil {
1402-
return append(diags, &diag.Diagnostic{
1401+
return diags.Append(&diag.Diagnostic{
14031402
Severity: diag.Error,
14041403
Summary: "AtLeastOne",
14051404
Detail: err.Error(),
@@ -1409,7 +1408,7 @@ func (m schemaMap) validate(
14091408

14101409
if !ok {
14111410
if schema.Required {
1412-
return append(diags, &diag.Diagnostic{
1411+
return diags.Append(&diag.Diagnostic{
14131412
Severity: diag.Error,
14141413
Summary: "Required attribute is not set",
14151414
AttributePath: path,
@@ -1420,7 +1419,7 @@ func (m schemaMap) validate(
14201419

14211420
if !schema.Required && !schema.Optional {
14221421
// This is a computed-only field
1423-
return append(diags, &diag.Diagnostic{
1422+
return diags.Append(&diag.Diagnostic{
14241423
Severity: diag.Error,
14251424
Summary: "Computed attribute cannot be set",
14261425
AttributePath: path,
@@ -1439,7 +1438,7 @@ func (m schemaMap) validate(
14391438
// Required fields set via an interpolated value are accepted.
14401439
if !isWhollyKnown(raw) {
14411440
if schema.Deprecated != "" {
1442-
return append(diags, &diag.Diagnostic{
1441+
return diags.Append(&diag.Diagnostic{
14431442
Severity: diag.Warning,
14441443
Summary: "Attribute is deprecated",
14451444
Detail: schema.Deprecated,
@@ -1451,7 +1450,7 @@ func (m schemaMap) validate(
14511450

14521451
err = validateConflictingAttributes(k, schema, c)
14531452
if err != nil {
1454-
return append(diags, &diag.Diagnostic{
1453+
return diags.Append(&diag.Diagnostic{
14551454
Severity: diag.Error,
14561455
Summary: "ConflictsWith",
14571456
Detail: err.Error(),
@@ -1629,7 +1628,7 @@ func (m schemaMap) validateList(
16291628
rawV := reflect.ValueOf(raw)
16301629

16311630
if rawV.Kind() != reflect.Slice {
1632-
return append(diags, &diag.Diagnostic{
1631+
return diags.Append(&diag.Diagnostic{
16331632
Severity: diag.Error,
16341633
Summary: "Attribute should be a list",
16351634
AttributePath: path,
@@ -1647,7 +1646,7 @@ func (m schemaMap) validateList(
16471646

16481647
// Validate length
16491648
if schema.MaxItems > 0 && rawV.Len() > schema.MaxItems {
1650-
return append(diags, &diag.Diagnostic{
1649+
return diags.Append(&diag.Diagnostic{
16511650
Severity: diag.Error,
16521651
Summary: "List longer than MaxItems",
16531652
Detail: fmt.Sprintf("Attribute supports %d item maximum, config has %d declared", schema.MaxItems, rawV.Len()),
@@ -1656,7 +1655,7 @@ func (m schemaMap) validateList(
16561655
}
16571656

16581657
if schema.MinItems > 0 && rawV.Len() < schema.MinItems {
1659-
return append(diags, &diag.Diagnostic{
1658+
return diags.Append(&diag.Diagnostic{
16601659
Severity: diag.Error,
16611660
Summary: "List shorter than MinItems",
16621661
Detail: fmt.Sprintf("Attribute supports %d item minimum, config has %d declared", schema.MinItems, rawV.Len()),
@@ -1689,9 +1688,9 @@ func (m schemaMap) validateList(
16891688
switch t := schema.Elem.(type) {
16901689
case *Resource:
16911690
// This is a sub-resource
1692-
diags = append(diags, m.validateObject(key, t.Schema, c, p)...)
1691+
diags = diags.Append(m.validateObject(key, t.Schema, c, p))
16931692
case *Schema:
1694-
diags = append(diags, m.validateType(key, raw, t, c, p)...)
1693+
diags = diags.Append(m.validateType(key, raw, t, c, p))
16951694
}
16961695

16971696
}
@@ -1728,7 +1727,7 @@ func (m schemaMap) validateMap(
17281727
// be rejected.
17291728
reified, reifiedOk := c.Get(k)
17301729
if reifiedOk && raw == reified && !c.IsComputed(k) {
1731-
return append(diags, &diag.Diagnostic{
1730+
return diags.Append(&diag.Diagnostic{
17321731
Severity: diag.Error,
17331732
Summary: "Attribute should be a map",
17341733
AttributePath: path,
@@ -1739,7 +1738,7 @@ func (m schemaMap) validateMap(
17391738
case reflect.Map:
17401739
case reflect.Slice:
17411740
default:
1742-
return append(diags, &diag.Diagnostic{
1741+
return diags.Append(&diag.Diagnostic{
17431742
Severity: diag.Error,
17441743
Summary: "Attribute should be a map",
17451744
AttributePath: path,
@@ -1749,7 +1748,7 @@ func (m schemaMap) validateMap(
17491748
// If it is not a slice, validate directly
17501749
if rawV.Kind() != reflect.Slice {
17511750
mapIface := rawV.Interface()
1752-
diags = append(diags, validateMapValues(k, mapIface.(map[string]interface{}), schema, path)...)
1751+
diags = diags.Append(validateMapValues(k, mapIface.(map[string]interface{}), schema, path))
17531752
if diags.HasErrors() {
17541753
return diags
17551754
}
@@ -1766,14 +1765,14 @@ func (m schemaMap) validateMap(
17661765
for _, raw := range raws {
17671766
v := reflect.ValueOf(raw)
17681767
if v.Kind() != reflect.Map {
1769-
return append(diags, &diag.Diagnostic{
1768+
return diags.Append(&diag.Diagnostic{
17701769
Severity: diag.Error,
17711770
Summary: "Attribute should be a map",
17721771
AttributePath: path,
17731772
})
17741773
}
17751774
mapIface := v.Interface()
1776-
diags = append(diags, validateMapValues(k, mapIface.(map[string]interface{}), schema, path)...)
1775+
diags = diags.Append(validateMapValues(k, mapIface.(map[string]interface{}), schema, path))
17771776
if diags.HasErrors() {
17781777
return diags
17791778
}
@@ -1797,7 +1796,7 @@ func validateMapValues(k string, m map[string]interface{}, schema *Schema, path
17971796
valueType, err := getValueType(k, schema)
17981797
p := append(path.Copy(), cty.IndexStep{Key: cty.StringVal(key)})
17991798
if err != nil {
1800-
return append(diags, &diag.Diagnostic{
1799+
return diags.Append(&diag.Diagnostic{
18011800
Severity: diag.Error,
18021801
Summary: err.Error(),
18031802
AttributePath: p,
@@ -1808,7 +1807,7 @@ func validateMapValues(k string, m map[string]interface{}, schema *Schema, path
18081807
case TypeBool:
18091808
var n bool
18101809
if err := mapstructure.WeakDecode(raw, &n); err != nil {
1811-
return append(diags, &diag.Diagnostic{
1810+
return diags.Append(&diag.Diagnostic{
18121811
Severity: diag.Error,
18131812
Summary: err.Error(),
18141813
AttributePath: p,
@@ -1817,7 +1816,7 @@ func validateMapValues(k string, m map[string]interface{}, schema *Schema, path
18171816
case TypeInt:
18181817
var n int
18191818
if err := mapstructure.WeakDecode(raw, &n); err != nil {
1820-
return append(diags, &diag.Diagnostic{
1819+
return diags.Append(&diag.Diagnostic{
18211820
Severity: diag.Error,
18221821
Summary: err.Error(),
18231822
AttributePath: p,
@@ -1826,7 +1825,7 @@ func validateMapValues(k string, m map[string]interface{}, schema *Schema, path
18261825
case TypeFloat:
18271826
var n float64
18281827
if err := mapstructure.WeakDecode(raw, &n); err != nil {
1829-
return append(diags, &diag.Diagnostic{
1828+
return diags.Append(&diag.Diagnostic{
18301829
Severity: diag.Error,
18311830
Summary: err.Error(),
18321831
AttributePath: p,
@@ -1835,7 +1834,7 @@ func validateMapValues(k string, m map[string]interface{}, schema *Schema, path
18351834
case TypeString:
18361835
var n string
18371836
if err := mapstructure.WeakDecode(raw, &n); err != nil {
1838-
return append(diags, &diag.Diagnostic{
1837+
return diags.Append(&diag.Diagnostic{
18391838
Severity: diag.Error,
18401839
Summary: err.Error(),
18411840
AttributePath: p,
@@ -1887,7 +1886,7 @@ func (m schemaMap) validateObject(
18871886
}
18881887

18891888
if _, ok := raw.(map[string]interface{}); !ok && !c.IsComputed(k) {
1890-
return append(diags, &diag.Diagnostic{
1889+
return diags.Append(&diag.Diagnostic{
18911890
Severity: diag.Error,
18921891
Summary: "Expected Object Type",
18931892
Detail: fmt.Sprintf("Expected object, got %s", reflect.ValueOf(raw).Kind()),
@@ -1900,7 +1899,7 @@ func (m schemaMap) validateObject(
19001899
if k != "" {
19011900
key = fmt.Sprintf("%s.%s", k, subK)
19021901
}
1903-
diags = append(diags, m.validate(key, s, c, append(path, cty.GetAttrStep{Name: subK}))...)
1902+
diags = diags.Append(m.validate(key, s, c, append(path, cty.GetAttrStep{Name: subK})))
19041903
}
19051904

19061905
// Detect any extra/unknown keys and report those as errors.
@@ -1910,7 +1909,7 @@ func (m schemaMap) validateObject(
19101909
if subk == TimeoutsConfigKey {
19111910
continue
19121911
}
1913-
diags = append(diags, &diag.Diagnostic{
1912+
diags = diags.Append(&diag.Diagnostic{
19141913
Severity: diag.Error,
19151914
Summary: "Invalid or unknown key",
19161915
AttributePath: append(path, cty.GetAttrStep{Name: subk}),
@@ -1943,13 +1942,13 @@ func (m schemaMap) validatePrimitive(
19431942
// doesn't contain Go type system terminology.
19441943
switch reflect.ValueOf(raw).Type().Kind() {
19451944
case reflect.Slice:
1946-
return append(diags, &diag.Diagnostic{
1945+
return diags.Append(&diag.Diagnostic{
19471946
Severity: diag.Error,
19481947
Summary: "Attribute must be a single value, not a list",
19491948
AttributePath: path,
19501949
})
19511950
case reflect.Map:
1952-
return append(diags, &diag.Diagnostic{
1951+
return diags.Append(&diag.Diagnostic{
19531952
Severity: diag.Error,
19541953
Summary: "Attribute must be a single value, not a map",
19551954
AttributePath: path,
@@ -1969,7 +1968,7 @@ func (m schemaMap) validatePrimitive(
19691968
// Verify that we can parse this as the correct type
19701969
var n bool
19711970
if err := mapstructure.WeakDecode(raw, &n); err != nil {
1972-
return append(diags, &diag.Diagnostic{
1971+
return diags.Append(&diag.Diagnostic{
19731972
Severity: diag.Error,
19741973
Summary: err.Error(),
19751974
AttributePath: path,
@@ -1984,7 +1983,7 @@ func (m schemaMap) validatePrimitive(
19841983
if v, ok := raw.(int); ok {
19851984
decoded = v
19861985
} else {
1987-
return append(diags, &diag.Diagnostic{
1986+
return diags.Append(&diag.Diagnostic{
19881987
Severity: diag.Error,
19891988
Summary: fmt.Sprintf("Attribute must be a whole number, got %v", raw),
19901989
AttributePath: path,
@@ -1994,7 +1993,7 @@ func (m schemaMap) validatePrimitive(
19941993
// Verify that we can parse this as an int
19951994
var n float64
19961995
if err := mapstructure.WeakDecode(raw, &n); err != nil {
1997-
return append(diags, &diag.Diagnostic{
1996+
return diags.Append(&diag.Diagnostic{
19981997
Severity: diag.Error,
19991998
Summary: err.Error(),
20001999
AttributePath: path,
@@ -2005,7 +2004,7 @@ func (m schemaMap) validatePrimitive(
20052004
// Verify that we can parse this as a string
20062005
var n string
20072006
if err := mapstructure.WeakDecode(raw, &n); err != nil {
2008-
return append(diags, &diag.Diagnostic{
2007+
return diags.Append(&diag.Diagnostic{
20092008
Severity: diag.Error,
20102009
Summary: err.Error(),
20112010
AttributePath: path,
@@ -2016,7 +2015,7 @@ func (m schemaMap) validatePrimitive(
20162015
panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
20172016
}
20182017

2019-
return append(diags, schema.validateFunc(decoded, k, path)...)
2018+
return diags.Append(schema.validateFunc(decoded, k, path))
20202019
}
20212020

20222021
func (m schemaMap) validateType(
@@ -2040,7 +2039,7 @@ func (m schemaMap) validateType(
20402039
}
20412040

20422041
if schema.Deprecated != "" {
2043-
diags = append(diags, &diag.Diagnostic{
2042+
diags = diags.Append(&diag.Diagnostic{
20442043
Severity: diag.Warning,
20452044
Summary: "Deprecated Attribute",
20462045
Detail: schema.Deprecated,
@@ -2049,7 +2048,7 @@ func (m schemaMap) validateType(
20492048
}
20502049

20512050
if schema.Removed != "" {
2052-
diags = append(diags, &diag.Diagnostic{
2051+
diags = diags.Append(&diag.Diagnostic{
20532052
Severity: diag.Error,
20542053
Summary: "Removed Attribute",
20552054
Detail: schema.Removed,

0 commit comments

Comments
 (0)