Skip to content

Commit

Permalink
Merge pull request #193 from diurnalist/b/loop-capture-var
Browse files Browse the repository at this point in the history
fix loop var bug in fixable results
  • Loading branch information
rgeyer authored Sep 19, 2024
2 parents eb2bc3b + 81af297 commit d2a0ebe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions lint/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (f DashboardRuleFunc) Lint(d Dashboard, s *ResultSet) {
}
rr := make([]FixableResult, len(dashboardResults))
for i, r := range dashboardResults {
r := r // capture loop variable
var fix func(*Dashboard)
if r.Fix != nil {
fix = func(dashboard *Dashboard) {
Expand Down
32 changes: 32 additions & 0 deletions lint/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,35 @@ func TestCustomRules(t *testing.T) {
})
}
}

func TestFixableRules(t *testing.T) {
sampleDashboard, err := os.ReadFile("testdata/dashboard.json")
assert.NoError(t, err)

rule := lint.NewDashboardRuleFunc(
"test-fixable-rule", "Test fixable rule",
func(d lint.Dashboard) lint.DashboardRuleResults {
rr := lint.DashboardRuleResults{}
rr.AddFixableError(d, "fixing first issue", func(d *lint.Dashboard) {
d.Title += " fixed-once"
})
rr.AddFixableError(d, "fixing second issue", func(d *lint.Dashboard) {
d.Title += " fixed-twice"
})
return rr
},
)

rules := lint.RuleSet{}
rules.Add(rule)

dashboard, err := lint.NewDashboard(sampleDashboard)
assert.NoError(t, err)

results, err := rules.Lint([]lint.Dashboard{dashboard})
assert.NoError(t, err)

results.AutoFix(&dashboard)

assert.Equal(t, "Sample dashboard fixed-once fixed-twice", dashboard.Title)
}

0 comments on commit d2a0ebe

Please sign in to comment.