Skip to content

Commit e9f6761

Browse files
engine/tracker: add CI apply and compare detection tests
Covers change application and baseline comparison helpers. Made-with: Cursor
1 parent 069c9de commit e9f6761

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

engine/tracker/ci_apply_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tracker
2+
3+
import (
4+
"testing"
5+
6+
"github.com/AlexsanderHamir/prof/internal"
7+
)
8+
9+
func TestApplyCommandLineThresholdsNoFail(t *testing.T) {
10+
r := &ProfileChangeReport{
11+
FunctionChanges: []*FunctionChangeResult{
12+
{FunctionName: "f", FlatChangePercent: 50, ChangeType: internal.REGRESSION},
13+
},
14+
}
15+
sel := &Selections{UseThreshold: false}
16+
if err := applyCommandLineThresholds(r, sel); err != nil {
17+
t.Fatal(err)
18+
}
19+
}
20+
21+
func TestApplyCommandLineThresholdsTriggers(t *testing.T) {
22+
r := &ProfileChangeReport{
23+
FunctionChanges: []*FunctionChangeResult{
24+
{FunctionName: "f", FlatChangePercent: 99, ChangeType: internal.REGRESSION},
25+
},
26+
}
27+
sel := &Selections{UseThreshold: true, RegressionThreshold: 10}
28+
if err := applyCommandLineThresholds(r, sel); err == nil {
29+
t.Fatal("expected regression failure")
30+
}
31+
}
32+
33+
func TestShouldIgnoreFunctionByConfig(t *testing.T) {
34+
cfg := &internal.CITrackingConfig{
35+
IgnoreFunctions: []string{"x"},
36+
IgnorePrefixes: []string{"runtime."},
37+
}
38+
if !shouldIgnoreFunctionByConfig(cfg, "x") {
39+
t.Fatal()
40+
}
41+
if !shouldIgnoreFunctionByConfig(cfg, "runtime.gc") {
42+
t.Fatal()
43+
}
44+
if shouldIgnoreFunctionByConfig(cfg, "other") {
45+
t.Fatal()
46+
}
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tracker
2+
3+
import (
4+
"testing"
5+
6+
"github.com/AlexsanderHamir/prof/internal"
7+
"github.com/AlexsanderHamir/prof/parser"
8+
)
9+
10+
func TestDetectChangeBetweenTwoObjectsRegression(t *testing.T) {
11+
base := &parser.LineObj{FnName: "f", Flat: 10, Cum: 10}
12+
cur := &parser.LineObj{FnName: "f", Flat: 20, Cum: 20}
13+
r, err := detectChangeBetweenTwoObjects(base, cur)
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
if r.ChangeType != internal.REGRESSION {
18+
t.Fatalf("got %s", r.ChangeType)
19+
}
20+
}
21+
22+
func TestDetectChangeBetweenTwoObjectsImprovement(t *testing.T) {
23+
base := &parser.LineObj{FnName: "f", Flat: 20, Cum: 20}
24+
cur := &parser.LineObj{FnName: "f", Flat: 10, Cum: 10}
25+
r, err := detectChangeBetweenTwoObjects(base, cur)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
if r.ChangeType != internal.IMPROVEMENT {
30+
t.Fatalf("got %s", r.ChangeType)
31+
}
32+
}
33+
34+
func TestDetectChangeNilBaseline(t *testing.T) {
35+
cur := &parser.LineObj{FnName: "f", Flat: 1, Cum: 1}
36+
_, err := detectChangeBetweenTwoObjects(nil, cur)
37+
if err == nil {
38+
t.Fatal("expected error")
39+
}
40+
}
41+
42+
func TestLineObjByShortName(t *testing.T) {
43+
objs := []*parser.LineObj{{FnName: "a", Flat: 1}, {FnName: "b", Flat: 2}}
44+
m := lineObjByShortName(objs)
45+
if len(m) != 2 || m["a"].Flat != 1 {
46+
t.Fatal(m)
47+
}
48+
}

0 commit comments

Comments
 (0)