-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
debugger_test.go
116 lines (105 loc) · 2.61 KB
/
debugger_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package runn
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/k1LoW/runn/testutil"
"github.com/tenntenn/golden"
)
var testDebuggerHostRe = regexp.MustCompile(`(?s)Host:[^\r\n]+\r\n`)
var testDebuggerDateRe = regexp.MustCompile(`(?s)Date:[^\r\n]+\r\n`)
func TestDebugger(t *testing.T) {
tests := []struct {
book string
}{
{"testdata/book/http.yml"},
{"testdata/book/grpc.yml"},
{"testdata/book/cdp.yml"},
{"testdata/book/db.yml"},
{"testdata/book/exec.yml"},
}
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.book, func(t *testing.T) {
if strings.Contains(tt.book, "cdp") && testutil.SkipCDPTest(t) {
t.Skip("chrome not found")
}
out := new(bytes.Buffer)
hs := testutil.HTTPServer(t)
gs := testutil.GRPCServer(t, false, false)
db, _ := testutil.SQLite(t)
opts := []Option{
Book(tt.book),
HTTPRunner("req", hs.URL, hs.Client(), MultipartBoundary(testutil.MultipartBoundary)),
GrpcRunner("greq", gs.Conn()),
DBRunner("db", db),
Capture(NewDebugger(out)),
Var("url", hs.URL),
Scopes(ScopeAllowRunExec),
}
o, err := New(opts...)
if err != nil {
t.Fatal(err)
}
if err := o.Run(ctx); err != nil {
t.Error(err)
}
got := out.String()
if strings.Contains(tt.book, "http.yml") {
got = testDebuggerHostRe.ReplaceAllString(got, "Host: replace.example.com\r\n")
got = testDebuggerDateRe.ReplaceAllString(got, "Date: Wed, 07 Sep 2022 06:28:20 GMT\r\n")
}
if strings.Contains(tt.book, "cdp.yml") {
got = strings.ReplaceAll(got, hs.URL, "http://replace.example.com")
}
f := fmt.Sprintf("%s.debugger", filepath.Base(tt.book))
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, "testdata", f, got)
return
}
if diff := golden.Diff(t, "testdata", f, got); diff != "" {
t.Error(diff)
}
})
}
}
func TestDebuggerWithStderr(t *testing.T) {
noColor(t)
tests := []struct {
book string
}{
{"testdata/book/step_by_step_desc.yml"},
}
ctx := context.Background()
for _, tt := range tests {
t.Run(tt.book, func(t *testing.T) {
out := new(bytes.Buffer)
opts := []Option{
Book(tt.book),
Stderr(out),
Scopes(ScopeAllowRunExec),
}
o, err := New(opts...)
if err != nil {
t.Fatal(err)
}
if err := o.Run(ctx); err != nil {
t.Error(err)
}
got := out.String()
f := fmt.Sprintf("%s.debugger", filepath.Base(tt.book))
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, "testdata", f, got)
return
}
if diff := golden.Diff(t, "testdata", f, got); diff != "" {
t.Error(diff)
}
})
}
}