Skip to content

Commit 5bb9ac7

Browse files
authored
Ignore issues order in helper.AssertIssues (#363)
1 parent 2978c9e commit 5bb9ac7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

helper/testing.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func AssertIssues(t *testing.T, want Issues, got Issues) {
5858
opts := []cmp.Option{
5959
// Byte field will be ignored because it's not important in tests such as positions
6060
cmpopts.IgnoreFields(hcl.Pos{}, "Byte"),
61+
// Issues will be sorted and output in the end, so ignore the order.
62+
ignoreIssuesOrder(),
6163
ruleComparer(),
6264
}
6365
if diff := cmp.Diff(want, got, opts...); diff != "" {
@@ -71,6 +73,7 @@ func AssertIssuesWithoutRange(t *testing.T, want Issues, got Issues) {
7173

7274
opts := []cmp.Option{
7375
cmpopts.IgnoreFields(Issue{}, "Range"),
76+
ignoreIssuesOrder(),
7477
ruleComparer(),
7578
}
7679
if diff := cmp.Diff(want, got, opts...); diff != "" {
@@ -98,3 +101,24 @@ func ruleComparer() cmp.Option {
98101
return reflect.TypeOf(x) == reflect.TypeOf(y)
99102
})
100103
}
104+
105+
func ignoreIssuesOrder() cmp.Option {
106+
return cmpopts.SortSlices(func(i, j *Issue) bool {
107+
if i.Range.Filename != j.Range.Filename {
108+
return i.Range.Filename < j.Range.Filename
109+
}
110+
if i.Range.Start.Line != j.Range.Start.Line {
111+
return i.Range.Start.Line < j.Range.Start.Line
112+
}
113+
if i.Range.Start.Column != j.Range.Start.Column {
114+
return i.Range.Start.Column < j.Range.Start.Column
115+
}
116+
if i.Range.End.Line != j.Range.End.Line {
117+
return i.Range.End.Line > j.Range.End.Line
118+
}
119+
if i.Range.End.Column != j.Range.End.Column {
120+
return i.Range.End.Column > j.Range.End.Column
121+
}
122+
return i.Message < j.Message
123+
})
124+
}

0 commit comments

Comments
 (0)