Skip to content

Commit

Permalink
Ignore issues order in helper.AssertIssues (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Jan 6, 2025
1 parent 2978c9e commit 5bb9ac7
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions helper/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func AssertIssues(t *testing.T, want Issues, got Issues) {
opts := []cmp.Option{
// Byte field will be ignored because it's not important in tests such as positions
cmpopts.IgnoreFields(hcl.Pos{}, "Byte"),
// Issues will be sorted and output in the end, so ignore the order.
ignoreIssuesOrder(),
ruleComparer(),
}
if diff := cmp.Diff(want, got, opts...); diff != "" {
Expand All @@ -71,6 +73,7 @@ func AssertIssuesWithoutRange(t *testing.T, want Issues, got Issues) {

opts := []cmp.Option{
cmpopts.IgnoreFields(Issue{}, "Range"),
ignoreIssuesOrder(),
ruleComparer(),
}
if diff := cmp.Diff(want, got, opts...); diff != "" {
Expand Down Expand Up @@ -98,3 +101,24 @@ func ruleComparer() cmp.Option {
return reflect.TypeOf(x) == reflect.TypeOf(y)
})
}

func ignoreIssuesOrder() cmp.Option {
return cmpopts.SortSlices(func(i, j *Issue) bool {
if i.Range.Filename != j.Range.Filename {
return i.Range.Filename < j.Range.Filename
}
if i.Range.Start.Line != j.Range.Start.Line {
return i.Range.Start.Line < j.Range.Start.Line
}
if i.Range.Start.Column != j.Range.Start.Column {
return i.Range.Start.Column < j.Range.Start.Column
}
if i.Range.End.Line != j.Range.End.Line {
return i.Range.End.Line > j.Range.End.Line
}
if i.Range.End.Column != j.Range.End.Column {
return i.Range.End.Column > j.Range.End.Column
}
return i.Message < j.Message
})
}

0 comments on commit 5bb9ac7

Please # to comment.