forked from bufbuild/buf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appcmdtesting.go
207 lines (195 loc) · 6.25 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
// Copyright 2020-2023 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
import (
"bytes"
"context"
"io"
"os"
"testing"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/stretchr/testify/assert"
"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()))
}
// 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()))
}
// 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()
for _, expectedPartial := range expectedStderrPartials {
assert.Contains(t, allStderr, expectedPartial)
}
}
// 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()))
require.Equal(t, stringutil.TrimLines(expectedStderr), stringutil.TrimLines(stderr.String()))
}
// 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,
) {
// 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),
),
)
require.Equal(
t,
expectedExitCode,
exitCode,
stringutil.TrimLines(stdoutCopy.String())+"\n"+stringutil.TrimLines(stderrCopy.String()),
)
}