Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude output only and parent fields from missing test detection #10473

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading