Skip to content

Commit

Permalink
Exclude output only and parent fields from missing test detection (Go…
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge authored and balanaguharsha committed May 2, 2024
1 parent e95c5b1 commit 8960f95
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
15 changes: 15 additions & 0 deletions tools/diff-processor/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/GoogleCloudPlatform/magic-modules/tools/diff-processor/diff"
"github.com/GoogleCloudPlatform/magic-modules/tools/diff-processor/reader"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zclconf/go-cty/cty"
)

Expand Down Expand Up @@ -39,11 +40,25 @@ func DetectMissingTests(schemaDiff diff.SchemaDiff, allTests []*reader.Test) (ma
return getMissingTestsForChanges(changedFields, allTests)
}

// Convert SchemaDiff object to map of ResourceChanges objects.
// Also remove parent fields and output-only fields.
func getChangedFieldsFromSchemaDiff(schemaDiff diff.SchemaDiff) map[string]ResourceChanges {
changedFields := make(map[string]ResourceChanges)
for resource, resourceDiff := range schemaDiff {
resourceChanges := make(ResourceChanges)
for field, fieldDiff := range resourceDiff.Fields {
if fieldDiff.New == nil {
// Skip deleted fields.
continue
}
if fieldDiff.New.Computed && !fieldDiff.New.Optional {
// Skip output-only fields.
continue
}
if _, ok := fieldDiff.New.Elem.(*schema.Resource); ok {
// Skip parent fields.
continue
}
if fieldDiff.Old == nil {
resourceChanges[field] = &Field{Added: true}
} else {
Expand Down
17 changes: 15 additions & 2 deletions tools/diff-processor/detector/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ func TestGetChangedFieldsFromSchemaDiff(t *testing.T) {
schemaDiff: diff.SchemaDiff{
"covered_resource": diff.ResourceDiff{
Fields: map[string]diff.FieldDiff{
"field_one": {},
"field_one": {
New: &schema.Schema{},
},
"field_two.field_three": {
New: &schema.Schema{},
Old: &schema.Schema{},
},
"field_four.field_five.field_six": {},
"field_four": {
New: &schema.Schema{
Elem: &schema.Resource{},
},
},
"field_four.field_five.field_six": {
New: &schema.Schema{},
},
"field_seven": {
New: &schema.Schema{Computed: true},
},
},
},
},
Expand Down
4 changes: 1 addition & 3 deletions tools/diff-processor/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"golang.org/x/exp/maps"
)

// SchemaDiff is a nested map with field names as keys and Field objects
// as bottom-level values.
// Fields are assumed not to be covered until detected in a test.
// SchemaDiff is a nested map with resource names as top-level keys.
type SchemaDiff map[string]ResourceDiff

type ResourceDiff struct {
Expand Down

0 comments on commit 8960f95

Please sign in to comment.