Skip to content

Commit 2775c04

Browse files
committed
Add sort determinism tests for vulns exposure and praise commands
1 parent db60209 commit 2775c04

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

cmd/vulns_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"path/filepath"
8+
"sort"
89
"strings"
910
"sync/atomic"
1011
"testing"
@@ -371,3 +372,48 @@ func TestBuildVersRange(t *testing.T) {
371372
})
372373
}
373374
}
375+
376+
func TestExposureSortDeterministic(t *testing.T) {
377+
entries := []VulnExposureEntry{
378+
{VulnID: "GHSA-0003", ExposureDays: 10},
379+
{VulnID: "GHSA-0001", ExposureDays: 10},
380+
{VulnID: "GHSA-0002", ExposureDays: 10},
381+
{VulnID: "GHSA-0004", ExposureDays: 30},
382+
}
383+
384+
sort.Slice(entries, func(i, j int) bool {
385+
if entries[i].ExposureDays != entries[j].ExposureDays {
386+
return entries[i].ExposureDays > entries[j].ExposureDays
387+
}
388+
return entries[i].VulnID < entries[j].VulnID
389+
})
390+
391+
want := []string{"GHSA-0004", "GHSA-0001", "GHSA-0002", "GHSA-0003"}
392+
for i, id := range want {
393+
if entries[i].VulnID != id {
394+
t.Errorf("entries[%d].VulnID = %q, want %q", i, entries[i].VulnID, id)
395+
}
396+
}
397+
}
398+
399+
func TestPraiseSortDeterministic(t *testing.T) {
400+
authors := []PraiseAuthorSummary{
401+
{Author: "charlie", TotalFixes: 5},
402+
{Author: "alice", TotalFixes: 5},
403+
{Author: "bob", TotalFixes: 10},
404+
}
405+
406+
sort.Slice(authors, func(i, j int) bool {
407+
if authors[i].TotalFixes != authors[j].TotalFixes {
408+
return authors[i].TotalFixes > authors[j].TotalFixes
409+
}
410+
return authors[i].Author < authors[j].Author
411+
})
412+
413+
want := []string{"bob", "alice", "charlie"}
414+
for i, name := range want {
415+
if authors[i].Author != name {
416+
t.Errorf("authors[%d].Author = %q, want %q", i, authors[i].Author, name)
417+
}
418+
}
419+
}

0 commit comments

Comments
 (0)