-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
runner_test.go
202 lines (158 loc) · 4.1 KB
/
runner_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package genny
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os/exec"
"regexp"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Runner_Run(t *testing.T) {
r := require.New(t)
g := New()
g.Command(exec.Command("foo", "bar"))
g.File(NewFile("foo.txt", strings.NewReader("Hello mark")))
run := DryRunner(context.Background())
r.NoError(run.With(g))
r.NoError(run.Run())
res := run.Results()
r.Len(res.Commands, 1)
r.Len(res.Files, 1)
c := res.Commands[0]
r.Equal("foo bar", strings.Join(c.Args, " "))
f := res.Files[0]
r.Equal("foo.txt", f.Name())
r.Equal("Hello mark", f.String())
}
func Test_Runner_FindFile(t *testing.T) {
r := require.New(t)
g := New()
g.File(NewFile("foo.txt", strings.NewReader("Hello mark")))
g.File(NewFile("foo.txt", strings.NewReader("Hello world")))
run := DryRunner(context.Background())
r.NoError(run.With(g))
r.NoError(run.Run())
res := run.Results()
r.Len(res.Files, 1)
f, err := run.FindFile("foo.txt")
r.NoError(err)
r.Equal(res.Files[0], f)
}
func Test_Runner_FindFile_FromDisk(t *testing.T) {
r := require.New(t)
run := DryRunner(context.Background())
exp, err := ioutil.ReadFile("./internal/testdata/foo.txt")
r.NoError(err)
f, err := run.FindFile("internal/testdata/foo.txt")
r.NoError(err)
act, err := ioutil.ReadAll(f)
r.NoError(err)
r.Equal(string(exp), string(act))
}
func Test_Runner_FindFile_DoesntExist(t *testing.T) {
r := require.New(t)
run := DryRunner(context.Background())
_, err := run.FindFile("idontexist")
r.Error(err)
}
func Test_Runner_Request(t *testing.T) {
table := []struct {
code int
method string
path string
boom bool
}{
{200, "GET", "/a", false},
{200, "POST", "/b", false},
{399, "PATCH", "/c", false},
{401, "GET", "/d", true},
{500, "POST", "/e", true},
}
for _, tt := range table {
t.Run(tt.method+tt.path, func(st *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(tt.code)
}))
defer ts.Close()
r := require.New(st)
p := ts.URL + tt.path
req, err := http.NewRequest(tt.method, p, nil)
r.NoError(err)
run := WetRunner(context.Background())
res, err := run.Request(req)
if tt.boom {
r.Error(err)
} else {
r.NoError(err)
}
results := run.Results()
r.Len(results.Requests, 1)
rr := results.Requests[0]
r.Equal(tt.path, rr.Request.URL.Path)
if res != nil {
r.Equal(tt.code, res.StatusCode)
}
})
}
}
func Test_Runner_FindStep(t *testing.T) {
r := require.New(t)
run := DryRunner(context.Background())
for i := 0; i < 3; i++ {
s, err := NewStep(New(), i)
r.NoError(err)
err = run.WithStep("step "+strconv.Itoa(i+1), s)
r.NoError(err)
}
s, err := run.FindStep("step 2")
r.NoError(err)
r.NotZero(s)
}
func Test_Runner_ReplaceStep(t *testing.T) {
r := require.New(t)
run := DryRunner(context.Background())
for i := 0; i < 3; i++ {
g := New()
g.File(NewFileS(fmt.Sprintf("%d.txt", i), strconv.Itoa(i)))
s, err := NewStep(g, i)
r.NoError(err)
err = run.WithStep("step "+strconv.Itoa(i), s)
r.NoError(err)
}
gx := New()
gx.File(NewFileS("2.txt", "replaced"))
s, err := NewStep(gx, 0)
r.NoError(err)
err = run.ReplaceStep("step 2", s)
r.NoError(err)
r.NoError(run.Run())
res := run.Results()
f, err := res.Find("2.txt")
r.NoError(err)
r.Equal("replaced", f.String())
}
// TODO: This test should be removed when we drop the safeguard for runner functions.
func TestRunnerExec_NeverExecuteNever(t *testing.T) {
r := require.New(t)
// NewRunner returns a most basic Runner so users can customize it.
// This gives us flexibility, so we can extend it with our own way.
runner := NewRunner(context.Background())
pattern := `^danger.*(.*$` // from live user input, configs, whatever.
runner.ExecFn = func(cmd *exec.Cmd) error {
re := regexp.MustCompile(pattern)
if re.MatchString(cmd.Path) {
cmd.Path = "danger_wrapper"
//... do some more
}
return cmd.Run()
}
cmd := exec.Command("maybe")
err := runner.Exec(cmd)
r.Error(err)
r.Contains(err.Error(), "regexp")
}