Skip to content

Commit

Permalink
Sort FileResults
Browse files Browse the repository at this point in the history
  • Loading branch information
caitlinelfring committed Sep 3, 2020
1 parent c203ca9 commit 753a421
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"sync"

"github.com/get-woke/woke/pkg/ignore"
Expand Down Expand Up @@ -80,6 +81,7 @@ func (p *Parser) processViolations(paths []string) (fr []result.FileResults, err
}()

for r := range rchan {
sort.Sort(r)
fr = append(fr, *r)
}
return
Expand Down
24 changes: 24 additions & 0 deletions pkg/result/fileresult_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package result

import (
"sort"
"testing"

"github.com/get-woke/woke/pkg/rule"
Expand All @@ -15,5 +16,28 @@ func TestFileResult_String(t *testing.T) {
rs = FindResults(&rule.WhitelistRule, "my/file", "this has no rule violations", 1)
fr = FileResults{Filename: "my/file", Results: rs}
assert.Equal(t, "my/file", fr.String())
}

func TestFileResult_Sort(t *testing.T) {
rs1 := FindResults(&rule.WhitelistRule, "my/file", "this has a few whitelist white-list whitelist", 1)
rs2 := FindResults(&rule.WhitelistRule, "my/file", "this has a few whitelist white-list whitelist", 2)

rs := append(rs2, rs1...)

fr := FileResults{Filename: "my/file", Results: rs}
sort.Sort(fr)

assert.True(t, fr.Results[0].StartPosition.Line == 1)
assert.True(t, fr.Results[0].StartPosition.Column == 15)
assert.True(t, fr.Results[1].StartPosition.Line == 1)
assert.True(t, fr.Results[1].StartPosition.Column == 25)
assert.True(t, fr.Results[2].StartPosition.Line == 1)
assert.True(t, fr.Results[2].StartPosition.Column == 36)

assert.True(t, fr.Results[3].StartPosition.Line == 2)
assert.True(t, fr.Results[3].StartPosition.Column == 15)
assert.True(t, fr.Results[4].StartPosition.Line == 2)
assert.True(t, fr.Results[4].StartPosition.Column == 25)
assert.True(t, fr.Results[5].StartPosition.Line == 2)
assert.True(t, fr.Results[5].StartPosition.Column == 36)
}
18 changes: 18 additions & 0 deletions pkg/result/fileresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ func (fr *FileResults) String() string {
}
return strings.Join(lines, "\n")
}

// Len is part of sort.Interface
func (fr FileResults) Len() int {
return len(fr.Results)
}

// Swap is part of sort.Interface
func (fr FileResults) Swap(i, j int) {
fr.Results[i], fr.Results[j] = fr.Results[j], fr.Results[i]
}

// Less is part of sort.Interface
func (fr FileResults) Less(i, j int) bool {
if fr.Results[i].StartPosition.Line < fr.Results[j].StartPosition.Line {
return true
}
return fr.Results[i].StartPosition.Column < fr.Results[j].StartPosition.Column
}

0 comments on commit 753a421

Please sign in to comment.