Skip to content

Commit da0e250

Browse files
MaineK00nclaude
andauthored
fix(detector): sort previous JSON dirs in descending order for diff (#2555)
* fix(detector): sort previous JSON dirs in descending order for diff ListValidJSONDirs in detector was switched from a descending sort.Slice to slices.Sort in #2415, which sorts ascending. The doc comment and loadPrevious both assume descending order — loadPrevious takes dirs[1:] expecting the second-newest dir as the previous scan, but with the ascending sort it picks the second-oldest, so report --diff-plus compares against an ancient scan instead of the latest prior one. Match reporter/util.go's ListValidJSONDirs by sorting descending. Reported in #2536 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test(detector): regression test for ListValidJSONDirs sort order Reproduces the scenario from discussion #2536 — when many timestamped result dirs exist, the newest must come first so loadPrevious picks the second-newest as the previous scan. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 1b97518 commit da0e250

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

detector/util.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package detector
44

55
import (
6+
"cmp"
67
"encoding/json"
78
"fmt"
89
"maps"
@@ -235,7 +236,9 @@ func ListValidJSONDirs(resultsDir string) (dirs []string, err error) {
235236
}
236237
}
237238
}
238-
slices.Sort(dirs)
239+
slices.SortFunc(dirs, func(a, b string) int {
240+
return -cmp.Compare(a, b)
241+
})
239242
return
240243
}
241244

detector/util_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//go:build !scanner
2+
3+
package detector
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
)
10+
11+
func TestListValidJSONDirs_SortedDescending(t *testing.T) {
12+
root := t.TempDir()
13+
14+
names := []string{
15+
"2026-04-24T08-22-31+0000",
16+
"2026-04-25T08-27-25+0000",
17+
"2026-04-25T17-52-14+0000",
18+
"2026-04-25T18-20-55+0000",
19+
"2026-04-25T19-48-34+0000",
20+
"2026-04-27T13-02-05+0000",
21+
"2026-04-28T12-56-45+0000",
22+
"2026-04-29T12-57-26+0000",
23+
"2026-04-30T12-57-38+0000",
24+
"2026-05-01T12-57-29+0000",
25+
"2026-05-01T15-17-13+0000",
26+
"2026-05-01T15-33-21+0000",
27+
"not-a-timestamp",
28+
}
29+
for _, n := range names {
30+
if err := os.Mkdir(filepath.Join(root, n), 0o755); err != nil {
31+
t.Fatalf("mkdir %s: %v", n, err)
32+
}
33+
}
34+
35+
got, err := ListValidJSONDirs(root)
36+
if err != nil {
37+
t.Fatalf("ListValidJSONDirs: %v", err)
38+
}
39+
40+
want := []string{
41+
filepath.Join(root, "2026-05-01T15-33-21+0000"),
42+
filepath.Join(root, "2026-05-01T15-17-13+0000"),
43+
filepath.Join(root, "2026-05-01T12-57-29+0000"),
44+
filepath.Join(root, "2026-04-30T12-57-38+0000"),
45+
filepath.Join(root, "2026-04-29T12-57-26+0000"),
46+
filepath.Join(root, "2026-04-28T12-56-45+0000"),
47+
filepath.Join(root, "2026-04-27T13-02-05+0000"),
48+
filepath.Join(root, "2026-04-25T19-48-34+0000"),
49+
filepath.Join(root, "2026-04-25T18-20-55+0000"),
50+
filepath.Join(root, "2026-04-25T17-52-14+0000"),
51+
filepath.Join(root, "2026-04-25T08-27-25+0000"),
52+
filepath.Join(root, "2026-04-24T08-22-31+0000"),
53+
}
54+
55+
if len(got) != len(want) {
56+
t.Fatalf("len(got)=%d want=%d\ngot=%v", len(got), len(want), got)
57+
}
58+
for i := range want {
59+
if got[i] != want[i] {
60+
t.Errorf("dirs[%d] = %s, want %s", i, got[i], want[i])
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)