-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
profile_test.go
90 lines (86 loc) · 1.67 KB
/
profile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package runn
import (
"bytes"
"context"
"fmt"
"os"
"testing"
"github.com/goccy/go-json"
"github.com/k1LoW/stopw"
)
func TestProfile(t *testing.T) {
tests := []struct {
book string
profile bool
wantErr bool
depth int
}{
{"testdata/book/db.yml", true, false, 4},
{"testdata/book/only_if_included.yml", true, false, 1},
{"testdata/book/if.yml", true, false, 2},
{"testdata/book/include_main.yml", true, false, 5},
{"testdata/book/db.yml", false, true, 0},
}
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.book, func(t *testing.T) {
db, err := os.CreateTemp("", "tmp")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
os.Remove(db.Name())
})
opts := []Option{
T(t),
Book(tt.book),
Profile(tt.profile),
Runner("db", fmt.Sprintf("sqlite://%s", db.Name())),
Scopes(ScopeAllowRunExec),
}
o, err := New(opts...)
if err != nil {
t.Fatal(err)
}
if err := o.Run(ctx); err != nil {
t.Error(err)
}
buf := new(bytes.Buffer)
if err := o.DumpProfile(buf); err != nil {
if !tt.wantErr {
t.Errorf("got %v", err)
}
return
}
if buf.Len() == 0 {
t.Error("invalid profile")
}
var s *stopw.Span
if err := json.Unmarshal(buf.Bytes(), &s); err != nil {
t.Error(err)
}
if tt.wantErr {
t.Error("want error")
}
got := calcDepth(o.sw.Result())
if got != tt.depth {
t.Errorf("got %v\nwant %v", got, tt.depth)
}
})
}
}
func calcDepth(s *stopw.Span) int {
d := 0
if len(s.Breakdown) > 0 {
d += 1
most := 0
for _, b := range s.Breakdown {
d := calcDepth(b)
if most < d {
most = d
}
}
d += most
}
return d
}