|
5 | 5 | "context" |
6 | 6 | "fmt" |
7 | 7 | "path/filepath" |
| 8 | + "sort" |
8 | 9 | "strings" |
9 | 10 | "sync/atomic" |
10 | 11 | "testing" |
@@ -371,3 +372,48 @@ func TestBuildVersRange(t *testing.T) { |
371 | 372 | }) |
372 | 373 | } |
373 | 374 | } |
| 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