-
Notifications
You must be signed in to change notification settings - Fork 256
/
appcmdtesting.go
301 lines (286 loc) · 8.57 KB
/
appcmdtesting.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2020-2024 Buf Technologies, Inc.
//
// 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 appcmdtesting contains test utilities for appcmd.
package appcmdtesting
import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/slicesext"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/stretchr/testify/require"
)
// RunCommandSuccessStdout runs the command and makes sure it was successful, and compares the stdout output.
func RunCommandSuccessStdout(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedStdout string,
newEnv func(use string) map[string]string,
stdin io.Reader,
args ...string,
) {
RunCommandExitCodeStdout(t, newCommand, 0, expectedStdout, newEnv, stdin, args...)
}
// RunCommandExitCodeStdout runs the command and compares the exit code and stdout output.
func RunCommandExitCodeStdout(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCode int,
expectedStdout string,
newEnv func(use string) map[string]string,
stdin io.Reader,
args ...string,
) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
RunCommandExitCode(t, newCommand, expectedExitCode, newEnv, stdin, stdout, stderr, args...)
require.Equal(
t,
stringutil.TrimLines(expectedStdout),
stringutil.TrimLines(stdout.String()),
requireErrorMessage(args, stdout, stderr),
)
}
// RunCommandExitCodeStdoutFile runs the command and compares the exit code and stdout output.
func RunCommandExitCodeStdoutFile(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCode int,
expectedStdout string,
newEnv func(use string) map[string]string,
stdin io.Reader,
args ...string,
) {
file, err := os.Open(expectedStdout)
require.NoError(t, err)
expectedstdoutConts, err := io.ReadAll(file)
require.NoError(t, err)
RunCommandExitCodeStdout(t, newCommand, expectedExitCode, string(expectedstdoutConts), newEnv, stdin, args...)
}
// RunCommandExitCodeStdoutStdinFile runs the command and allows a stdinFile to be opened and piped into the command.
func RunCommandExitCodeStdoutStdinFile(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCode int,
expectedStdout string,
newEnv func(use string) map[string]string,
stdinFile string,
args ...string,
) {
stdin, err := os.Open(stdinFile)
require.NoError(t, err)
RunCommandExitCodeStdout(t, newCommand, expectedExitCode, expectedStdout, newEnv, stdin, args...)
}
// RunCommandExitCodeStderr runs the command and compares the exit code and stderr output.
func RunCommandExitCodeStderr(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCode int,
expectedStderr string,
newEnv func(use string) map[string]string,
stdin io.Reader,
args ...string,
) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
RunCommandExitCode(t, newCommand, expectedExitCode, newEnv, stdin, stdout, stderr, args...)
require.Equal(
t,
stringutil.TrimLines(expectedStderr),
stringutil.TrimLines(stderr.String()),
requireErrorMessage(args, stdout, stderr),
)
}
// RunCommandExitCodesStderr runs the command and compares the exit codes and stderr output.
func RunCommandExitCodesStderr(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCodes []int,
expectedStderr string,
newEnv func(use string) map[string]string,
stdin io.Reader,
args ...string,
) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
RunCommandExitCodes(t, newCommand, expectedExitCodes, newEnv, stdin, stdout, stderr, args...)
require.Equal(
t,
stringutil.TrimLines(expectedStderr),
stringutil.TrimLines(stderr.String()),
requireErrorMessage(args, stdout, stderr),
)
}
// RunCommandExitCodeStderrContains runs the command and compares the exit code and stderr output
// with the passed partial messages.
func RunCommandExitCodeStderrContains(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCode int,
expectedStderrPartials []string,
newEnv func(use string) map[string]string,
stdin io.Reader,
args ...string,
) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
RunCommandExitCode(t, newCommand, expectedExitCode, newEnv, stdin, stdout, stderr, args...)
allStderr := stderr.String()
if len(expectedStderrPartials) == 0 {
require.Empty(t, allStderr, "stderr was not empty:\n"+requireErrorMessage(args, stdout, stderr))
}
for _, expectedPartial := range expectedStderrPartials {
require.Contains(t, allStderr, expectedPartial, "stderr expected to contain %q:\n:%s", expectedPartial, requireErrorMessage(args, stdout, stderr))
}
}
// RunCommandExitCodeStdoutStderr runs the command and compares the exit code, stdout, and stderr output.
func RunCommandExitCodeStdoutStderr(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCode int,
expectedStdout string,
expectedStderr string,
newEnv func(use string) map[string]string,
stdin io.Reader,
args ...string,
) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
RunCommandExitCode(t, newCommand, expectedExitCode, newEnv, stdin, stdout, stderr, args...)
require.Equal(
t,
stringutil.TrimLines(expectedStdout),
stringutil.TrimLines(stdout.String()),
requireErrorMessage(args, stdout, stderr),
)
require.Equal(
t,
stringutil.TrimLines(expectedStderr),
stringutil.TrimLines(stderr.String()),
requireErrorMessage(args, stdout, stderr),
)
}
// RunCommandSuccess runs the command and makes sure it was successful.
func RunCommandSuccess(
t *testing.T,
newCommand func(use string) *appcmd.Command,
newEnv func(use string) map[string]string,
stdin io.Reader,
stdout io.Writer,
args ...string,
) {
stderr := bytes.NewBuffer(nil)
RunCommandExitCode(t, newCommand, 0, newEnv, stdin, stdout, stderr, args...)
}
// RunCommandExitCode runs the command and compares the exit code.
func RunCommandExitCode(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCode int,
newEnv func(use string) map[string]string,
stdin io.Reader,
stdout io.Writer,
stderr io.Writer,
args ...string,
) {
RunCommandExitCodes(t, newCommand, []int{expectedExitCode}, newEnv, stdin, stdout, stderr, args...)
}
// RunCommandExitCodes runs the command and compares the exit code to the expected
// exit codes.
//
// It would be nice if we could do:
//
// type IntOrInts interface {
// int | []int
// }
//
// func RunCommandExitCode[I IntOrInts](expectedExitCode I)
//
// However we can't: https://github.com/golang/go/issues/49206
func RunCommandExitCodes(
t *testing.T,
newCommand func(use string) *appcmd.Command,
expectedExitCodes []int,
newEnv func(use string) map[string]string,
stdin io.Reader,
stdout io.Writer,
stderr io.Writer,
args ...string,
) {
// make the use something different than the actual command
// to make sure that all code is binary-name-agnostic.
use := "test"
stdoutCopy := bytes.NewBuffer(nil)
if stdout == nil {
stdout = stdoutCopy
} else {
stdout = io.MultiWriter(stdout, stdoutCopy)
}
stderrCopy := bytes.NewBuffer(nil)
if stderr == nil {
stderr = stderrCopy
} else {
stderr = io.MultiWriter(stderr, stderrCopy)
}
var env map[string]string
if newEnv != nil {
env = newEnv(use)
}
exitCode := app.GetExitCode(
appcmd.Run(
context.Background(),
app.NewContainer(
env,
stdin,
stdout,
stderr,
append([]string{"test"}, args...)...,
),
newCommand(use),
),
)
if slicesext.Count(expectedExitCodes, func(i int) bool { return exitCode == i }) == 0 {
require.True(
t,
false,
"expected exit code %d to be one of %v\n:%s",
exitCode,
expectedExitCodes,
requireErrorMessage(args, stdoutCopy, stderrCopy),
)
}
}
func requireErrorMessage(args []string, stdout *bytes.Buffer, stderr *bytes.Buffer) string {
return fmt.Sprintf(
"args: %s\nstdout: %s\nstderr: %s",
strings.Join(
slicesext.Map(
args,
// To make the args copy-pastable.
func(arg string) string {
return `'` + arg + `'`
},
),
" ",
),
stringutil.TrimLines(stdout.String()),
stringutil.TrimLines(stderr.String()),
)
}