|
86 | 86 | assert.Greater(t, len(result.Results), 0) |
87 | 87 | assert.Equal(t, 0, len(result.FixedResults)) |
88 | 88 | } |
| 89 | + |
| 90 | +// TestAutoFixDoesNotAffectNonFixableViolations tests that autofix only affects fixable rules |
| 91 | +func TestAutoFixDoesNotAffectNonFixableViolations(t *testing.T) { |
| 92 | + spec := ` |
| 93 | +openapi: 3.0.0 |
| 94 | +info: |
| 95 | + title: Test API |
| 96 | + version: 1.0.0 |
| 97 | + description: "" |
| 98 | +paths: |
| 99 | + /test: |
| 100 | + get: |
| 101 | + summary: "" |
| 102 | +` |
| 103 | + |
| 104 | + // Fixable rule |
| 105 | + fixableRule := model.Rule{ |
| 106 | + Id: "empty-description-fixable", |
| 107 | + Message: "Empty description found", |
| 108 | + Given: "$.info.description", |
| 109 | + Severity: model.SeverityWarn, |
| 110 | + AutoFixFunction: "fixEmptyDescription", |
| 111 | + Then: &model.RuleAction{Function: "truthy"}, |
| 112 | + } |
| 113 | + |
| 114 | + // Non-fixable rule (no AutoFixFunction) |
| 115 | + nonFixableRule := model.Rule{ |
| 116 | + Id: "empty-summary-not-fixable", |
| 117 | + Message: "Empty summary found", |
| 118 | + Given: "$.paths..summary", |
| 119 | + Severity: model.SeverityError, |
| 120 | + Then: &model.RuleAction{Function: "truthy"}, |
| 121 | + } |
| 122 | + |
| 123 | + emptyDescriptionFix := func(node *yaml.Node, document *yaml.Node, context *model.RuleFunctionContext) (*yaml.Node, error) { |
| 124 | + if node.Value == "" { |
| 125 | + node.Value = "TODO: Add description" |
| 126 | + } |
| 127 | + return node, nil |
| 128 | + } |
| 129 | + |
| 130 | + execution := &RuleSetExecution{ |
| 131 | + RuleSet: &rulesets.RuleSet{Rules: map[string]*model.Rule{ |
| 132 | + "empty-description-fixable": &fixableRule, |
| 133 | + "empty-summary-not-fixable": &nonFixableRule, |
| 134 | + }}, |
| 135 | + Spec: []byte(spec), |
| 136 | + SpecFileName: "test.yaml", |
| 137 | + ApplyAutoFixes: true, |
| 138 | + AutoFixFunctions: map[string]model.AutoFixFunction{"fixEmptyDescription": emptyDescriptionFix}, |
| 139 | + } |
| 140 | + |
| 141 | + result := ApplyRulesToRuleSet(execution) |
| 142 | + |
| 143 | + // Should have both fixed and unfixed results |
| 144 | + assert.Greater(t, len(result.FixedResults), 0, "Should have fixed some violations") |
| 145 | + assert.Greater(t, len(result.Results), 0, "Should have unfixed violations") |
| 146 | + |
| 147 | + // Verify fixed results are only from fixable rule |
| 148 | + for _, r := range result.FixedResults { |
| 149 | + assert.True(t, r.AutoFixed) |
| 150 | + assert.Equal(t, "empty-description-fixable", r.RuleId) |
| 151 | + } |
| 152 | + |
| 153 | + // Verify unfixed results are only from non-fixable rule |
| 154 | + for _, r := range result.Results { |
| 155 | + assert.False(t, r.AutoFixed) |
| 156 | + assert.Equal(t, "empty-summary-not-fixable", r.RuleId) |
| 157 | + } |
| 158 | +} |
0 commit comments