Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper: Ignore issues order in helper.AssertIssues #363

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
}
Loading