Skip to content

Commit 4c35b8b

Browse files
trodgeDawid212
authored andcommitted
Fix logic for shrinking max items rule (GoogleCloudPlatform#13357)
1 parent 9ba35f1 commit 4c35b8b

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

tools/diff-processor/breaking_changes/breaking_changes_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func TestComputeBreakingChanges(t *testing.T) {
298298
},
299299
wantViolations: []BreakingChange{
300300
{
301-
Message: "Field `field-a.sub-field-1` MinItems went from 100 to 25 on `google-x`",
301+
Message: "Field `field-a.sub-field-1` MaxItems went from 100 to 25 on `google-x`",
302302
DocumentationReference: "https://googlecloudplatform.github.io/magic-modules/breaking-changes/breaking-changes#field-shrinking-max",
303303
},
304304
},
@@ -343,7 +343,7 @@ func TestComputeBreakingChanges(t *testing.T) {
343343
},
344344
wantViolations: []BreakingChange{
345345
{
346-
Message: "Field `field-a.sub-field-1` MinItems went from 100 to 25 on `google-x`",
346+
Message: "Field `field-a.sub-field-1` MaxItems went from 100 to 25 on `google-x`",
347347
DocumentationReference: "https://googlecloudplatform.github.io/magic-modules/breaking-changes/breaking-changes#field-shrinking-max",
348348
},
349349
},

tools/diff-processor/breaking_changes/field_diff.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func FieldGrowingMinMessages(resource, field string, fieldDiff diff.FieldDiff) [
146146
return nil
147147
}
148148
tmpl := "Field `%s` MinItems went from %s to %s on `%s`"
149-
if fieldDiff.Old.MinItems < fieldDiff.New.MinItems || fieldDiff.Old.MinItems == 0 && fieldDiff.New.MinItems > 0 {
149+
if fieldDiff.Old.MinItems < fieldDiff.New.MinItems {
150150
oldMin := strconv.Itoa(fieldDiff.Old.MinItems)
151151
if fieldDiff.Old.MinItems == 0 {
152152
oldMin = "unset"
@@ -167,13 +167,16 @@ func FieldShrinkingMaxMessages(resource, field string, fieldDiff diff.FieldDiff)
167167
if fieldDiff.Old == nil || fieldDiff.New == nil {
168168
return nil
169169
}
170-
tmpl := "Field `%s` MinItems went from %s to %s on `%s`"
171-
if fieldDiff.Old.MaxItems > fieldDiff.New.MaxItems || fieldDiff.Old.MaxItems == 0 && fieldDiff.New.MaxItems > 0 {
172-
oldMax := strconv.Itoa(fieldDiff.Old.MaxItems)
173-
if fieldDiff.Old.MaxItems == 0 {
174-
oldMax = "unset"
175-
}
176-
newMax := strconv.Itoa(fieldDiff.New.MaxItems)
170+
tmpl := "Field `%s` MaxItems went from %s to %s on `%s`"
171+
if fieldDiff.New.MaxItems == 0 {
172+
return nil
173+
}
174+
newMax := strconv.Itoa(fieldDiff.New.MaxItems)
175+
if fieldDiff.Old.MaxItems == 0 {
176+
return []string{fmt.Sprintf(tmpl, field, "unset", newMax, resource)}
177+
}
178+
oldMax := strconv.Itoa(fieldDiff.Old.MaxItems)
179+
if fieldDiff.Old.MaxItems > fieldDiff.New.MaxItems {
177180
return []string{fmt.Sprintf(tmpl, field, oldMax, newMax, resource)}
178181
}
179182
return nil

tools/diff-processor/breaking_changes/field_diff_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@ var FieldShrinkingMaxTestCases = []fieldTestCase{
558558
},
559559
expectedViolation: true,
560560
},
561+
{
562+
name: "max defined to unset",
563+
oldField: &schema.Schema{
564+
MaxItems: 2,
565+
},
566+
newField: &schema.Schema{},
567+
expectedViolation: false,
568+
},
561569
}
562570

563571
func (tc *fieldTestCase) check(rule FieldDiffRule, t *testing.T) {

0 commit comments

Comments
 (0)