-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcmd_helper.go
285 lines (231 loc) · 6.18 KB
/
cmd_helper.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testutil
import (
"context"
"errors"
"io/ioutil"
"os/exec"
"strings"
"testing"
)
type FakeCmd struct {
t *testing.T
runs []run
timesCalled int
}
type run struct {
command string
input []byte
output []byte
env []string
dir *string
err error
pipeOutput bool
}
func newFakeCmd() *FakeCmd {
return &FakeCmd{}
}
func (c *FakeCmd) addRun(r run) *FakeCmd {
c.runs = append(c.runs, r)
return c
}
func (c *FakeCmd) popRun() (*run, error) {
if len(c.runs) == 0 {
return nil, errors.New("no more run is expected")
}
run := c.runs[0]
c.runs = c.runs[1:]
return &run, nil
}
func (c *FakeCmd) ForTest(t *testing.T) {
if c != nil {
c.t = t
}
}
func CmdRun(command string) *FakeCmd {
return newFakeCmd().AndRun(command)
}
func CmdRunInput(command, input string) *FakeCmd {
return newFakeCmd().AndRunInput(command, input)
}
func CmdRunErr(command string, err error) *FakeCmd {
return newFakeCmd().AndRunErr(command, err)
}
func CmdRunOut(command string, output string) *FakeCmd {
return newFakeCmd().AndRunOut(command, output)
}
func CmdRunDirOut(command string, dir string, output string) *FakeCmd {
return newFakeCmd().AndRunDirOut(command, dir, output)
}
func CmdRunOutErr(command string, output string, err error) *FakeCmd {
return newFakeCmd().AndRunOutErr(command, output, err)
}
func CmdRunEnv(command string, env []string) *FakeCmd {
return newFakeCmd().AndRunEnv(command, env)
}
// CmdRunWithOutput programs the fake runner with a command and expected output
func CmdRunWithOutput(command, output string) *FakeCmd {
return newFakeCmd().AndRunWithOutput(command, output)
}
func (c *FakeCmd) AndRun(command string) *FakeCmd {
return c.addRun(run{
command: command,
})
}
func (c *FakeCmd) AndRunInput(command, input string) *FakeCmd {
return c.addRun(run{
command: command,
input: []byte(input),
})
}
func (c *FakeCmd) AndRunErr(command string, err error) *FakeCmd {
return c.addRun(run{
command: command,
err: err,
})
}
// AndRunWithOutput takes a command and an expected output.
// It expected to match up with a call to RunCmd, and pipes
// the provided output to RunCmd's exec.Cmd's stdout.
func (c *FakeCmd) AndRunWithOutput(command, output string) *FakeCmd {
return c.addRun(run{
command: command,
output: []byte(output),
pipeOutput: true,
})
}
func (c *FakeCmd) AndRunInputOut(command string, input string, output string) *FakeCmd {
return c.addRun(run{
command: command,
input: []byte(input),
output: []byte(output),
})
}
func (c *FakeCmd) AndRunOut(command string, output string) *FakeCmd {
return c.addRun(run{
command: command,
output: []byte(output),
})
}
func (c *FakeCmd) AndRunDirOut(command string, dir string, output string) *FakeCmd {
return c.addRun(run{
command: command,
dir: &dir,
output: []byte(output),
})
}
func (c *FakeCmd) AndRunOutErr(command string, output string, err error) *FakeCmd {
return c.addRun(run{
command: command,
output: []byte(output),
err: err,
})
}
func (c *FakeCmd) AndRunEnv(command string, env []string) *FakeCmd {
return c.addRun(run{
command: command,
env: env,
})
}
func (c *FakeCmd) RunCmdOut(ctx context.Context, cmd *exec.Cmd) ([]byte, error) {
c.timesCalled++
command := strings.Join(cmd.Args, " ")
r, err := c.popRun()
if err != nil {
c.t.Fatalf("unable to run RunCmdOut() with command %q: %v", command, err)
}
if r.command != command {
c.t.Errorf("expected: %s. Got: %s", r.command, command)
}
c.assertCmdEnv(r.env, cmd.Env)
c.assertCmdDir(r.dir, cmd.Dir)
if err := c.assertInput(cmd, r, command); err != nil {
return nil, err
}
if r.output == nil {
c.t.Errorf("expected RunCmd(%s) to be called. Got RunCmdOut(%s)", r.command, command)
}
return r.output, r.err
}
func (c *FakeCmd) RunCmd(ctx context.Context, cmd *exec.Cmd) error {
c.timesCalled++
command := strings.Join(cmd.Args, " ")
r, err := c.popRun()
if err != nil {
c.t.Fatalf("unable to run RunCmd() with command %q", command)
}
if r.command != command {
c.t.Errorf("\nwanted: %s\n\n got: %s", r.command, command)
}
if r.output != nil {
if !r.pipeOutput {
c.t.Errorf("expected RunCmdOut(%s) to be called. Got RunCmd(%s)", r.command, command)
} else {
cmd.Stdout.Write(r.output)
}
}
c.assertCmdEnv(r.env, cmd.Env)
if err := c.assertInput(cmd, r, command); err != nil {
return err
}
return r.err
}
func (c *FakeCmd) assertInput(cmd *exec.Cmd, r *run, command string) error {
if r.input == nil {
return nil
}
if cmd.Stdin == nil {
c.t.Error("expected to run the command with a custom stdin", command)
return nil
}
buf, err := ioutil.ReadAll(cmd.Stdin)
if err != nil {
return err
}
actualInput := string(buf)
expectedInput := string(r.input)
if actualInput != expectedInput {
c.t.Errorf("wrong input. Expected: %s. Got %s", expectedInput, actualInput)
}
return nil
}
// assertCmdEnv ensures that actualEnv contains all values from requiredEnv
func (c *FakeCmd) assertCmdEnv(requiredEnv, actualEnv []string) {
if requiredEnv == nil {
return
}
c.t.Helper()
envs := make(map[string]struct{}, len(actualEnv))
for _, e := range actualEnv {
envs[e] = struct{}{}
}
for _, e := range requiredEnv {
if _, ok := envs[e]; !ok {
c.t.Errorf("expected env variable with value %q", e)
}
}
}
// assertCmdDir ensures that actualDir contains matches requiredDir
func (c *FakeCmd) assertCmdDir(requiredDir *string, actualDir string) {
if requiredDir == nil {
return
}
c.t.Helper()
if *requiredDir != actualDir {
c.t.Errorf("expected: %s. Got: %s", *requiredDir, actualDir)
}
}
func (c *FakeCmd) TimesCalled() int {
return c.timesCalled
}