-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.go
More file actions
362 lines (283 loc) · 8.82 KB
/
binary.go
File metadata and controls
362 lines (283 loc) · 8.82 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package testastic
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
const defaultRunTimeout = 30 * time.Second
var errBuildBinaryRequiresImportPath = errors.New("BuildBinary requires importPath")
// Binary is a coverage-instrumented Go binary that can be reused across tests.
//
// Binaries built with [BuildBinaryMain] create a temporary directory that
// outlives individual tests. Call [Binary.Cleanup] after [testing.M.Run] to
// remove it. Binaries built with [BuildBinary] use [testing.TB.TempDir] and
// are cleaned up automatically.
type Binary struct {
path string
workDir string
tempDir string // non-empty only for BuildBinaryMain; removed by Cleanup
}
// RunResult contains the captured stdout, stderr, and exit code from a CLI run.
type RunResult struct {
Stdout string
Stderr string
ExitCode int
}
// RunOption configures optional behavior for a single [Binary.Run] invocation.
// Use the provided option constructors (such as [WithRunEnv] and
// [WithRunTimeout]) to create options.
type RunOption interface {
applyRun(cfg *runConfig)
}
type runConfig struct {
env []string
stdin io.Reader
timeout time.Duration
workDir string
}
// BuildBinary builds a coverage-instrumented Go binary during a regular test
// and returns a reusable handle.
func BuildBinary(tb testing.TB, importPath string, opts ...BuildOption) *Binary {
tb.Helper()
cfg := newBuildConfig(opts)
cfg.importPath = importPath
if cfg.workDir == "" {
cfg.workDir = defaultWorkDir()
}
validateBuildConfig(tb, cfg)
outputPath := filepath.Join(tb.TempDir(), binaryOutputName("binary"))
output, err := runGoBuild(testingContext(tb), outputPath, cfg)
if err != nil {
tb.Fatalf("testastic: go build failed:\n%s", output)
}
return &Binary{path: outputPath, workDir: cfg.workDir}
}
// BuildBinaryMain builds a coverage-instrumented Go binary in TestMain and
// returns a reusable handle. Build failures print to stderr and exit the test
// process, because TestMain does not have a testing.TB.
func BuildBinaryMain(m *testing.M, importPath string, opts ...BuildOption) *Binary {
cfg := newBuildConfig(opts)
cfg.importPath = importPath
if cfg.workDir == "" {
cfg.workDir = defaultWorkDir()
}
err := validateBuildConfigError(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "testastic: %v\n", err)
os.Exit(1)
}
buildDir, err := os.MkdirTemp("", "testastic-binary-*")
if err != nil {
fmt.Fprintf(os.Stderr, "testastic: create binary temp dir: %v\n", err)
os.Exit(1)
}
outputPath := filepath.Join(buildDir, binaryOutputName("binary"))
output, err := runGoBuild(context.Background(), outputPath, cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "testastic: go build failed:\n%s", output)
os.Exit(1)
}
return &Binary{path: outputPath, workDir: cfg.workDir, tempDir: buildDir}
}
// NewBinary returns a reusable handle for a pre-built binary path.
func NewBinary(binaryPath string) *Binary {
return &Binary{path: binaryPath, workDir: defaultWorkDir()}
}
// Path returns the filesystem path to the binary.
func (b *Binary) Path() string {
return b.path
}
// Cleanup removes the temporary build directory created by [BuildBinaryMain].
// It is a no-op for binaries built with [BuildBinary] or wrapped with
// [NewBinary].
//
// Call Cleanup after [testing.M.Run] returns:
//
// func TestMain(m *testing.M) {
// bin := testastic.BuildBinaryMain(m, "./cmd/api")
// code := m.Run()
// bin.Cleanup()
// os.Exit(code)
// }
func (b *Binary) Cleanup() {
if b.tempDir != "" {
_ = os.RemoveAll(b.tempDir)
}
}
// Start launches the binary as a long-running process and waits for readiness.
func (b *Binary) Start(
ctx context.Context, tb testing.TB,
readyCheck ReadyChecker, opts ...ProcessOption,
) *Process {
tb.Helper()
if b == nil || b.path == "" {
tb.Fatalf("testastic: binary path is empty")
}
cfg := newProcessConfig(opts)
cfg.binaryPath = b.path
cfg.readyCheck = readyCheck
if cfg.workDir == "" {
cfg.workDir = b.workDir
}
if cfg.workDir == "" {
cfg.workDir = defaultWorkDir()
}
return startProcess(ctx, tb, cfg)
}
// Run executes the binary with the provided args and default run options.
func (b *Binary) Run(tb testing.TB, args ...string) *RunResult {
tb.Helper()
return b.RunWithOptions(tb, args)
}
// RunWithOptions executes the binary with the provided args and options,
// capturing stdout, stderr, and exit code.
func (b *Binary) RunWithOptions(tb testing.TB, args []string, opts ...RunOption) *RunResult {
tb.Helper()
if b == nil || b.path == "" {
tb.Fatalf("testastic: binary path is empty")
}
cfg := newRunConfig(b, opts)
validateRunConfig(tb, cfg)
baseCtx := testingContext(tb)
ctx, cancel := context.WithTimeout(baseCtx, cfg.timeout)
defer cancel()
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
coverDir := setupCoverDir(tb, "")
cmd := exec.CommandContext(ctx, b.path, args...) //nolint:gosec // args are from test config
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Stdin = cfg.stdin
cmd.Env = append(append(os.Environ(), cfg.env...), "GOCOVERDIR="+coverDir)
cmd.Dir = cfg.workDir
err := cmd.Run()
result := &RunResult{
Stdout: stdout.String(),
Stderr: stderr.String(),
}
if cmd.ProcessState != nil {
result.ExitCode = cmd.ProcessState.ExitCode()
}
if err == nil {
return result
}
ctxErr := ctx.Err()
if ctxErr != nil {
if errors.Is(ctxErr, context.DeadlineExceeded) {
tb.Fatalf(
"testastic: binary run timed out after %v\nstdout:\n%s\nstderr:\n%s",
cfg.timeout, result.Stdout, result.Stderr,
)
}
tb.Fatalf("testastic: binary run failed: %v", ctxErr)
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return result
}
tb.Fatalf("testastic: failed to run binary: %v", err)
return nil
}
type runEnvOption struct{ env []string }
func (o runEnvOption) applyRun(c *runConfig) { c.env = o.env }
type stdinOption struct{ reader io.Reader }
func (o stdinOption) applyRun(c *runConfig) { c.stdin = o.reader }
type runTimeoutOption struct{ duration time.Duration }
func (o runTimeoutOption) applyRun(c *runConfig) { c.timeout = o.duration }
type runWorkDirOption struct{ dir string }
func (o runWorkDirOption) applyRun(c *runConfig) { c.workDir = o.dir }
// WithRunEnv sets additional environment variables in KEY=VALUE format for a
// single CLI run. When called multiple times, only the last value is used.
func WithRunEnv(env ...string) RunOption {
return runEnvOption{env: env}
}
// WithStdin sets stdin for a single CLI run.
func WithStdin(r io.Reader) RunOption {
return stdinOption{reader: r}
}
// WithRunTimeout sets the maximum duration for a single CLI run.
func WithRunTimeout(d time.Duration) RunOption {
return runTimeoutOption{duration: d}
}
// WithRunWorkDir sets the working directory for a single CLI run.
func WithRunWorkDir(dir string) RunOption {
return runWorkDirOption{dir: dir}
}
func newRunConfig(binary *Binary, opts []RunOption) *runConfig {
cfg := &runConfig{timeout: defaultRunTimeout, workDir: binary.workDir}
for _, opt := range opts {
opt.applyRun(cfg)
}
if cfg.timeout == 0 {
cfg.timeout = defaultRunTimeout
}
return cfg
}
func validateBuildConfig(tb testing.TB, cfg *buildConfig) {
tb.Helper()
err := validateBuildConfigError(cfg)
if err != nil {
tb.Fatalf("testastic: %v", err)
}
}
func validateBuildConfigError(cfg *buildConfig) error {
if cfg.importPath == "" {
return errBuildBinaryRequiresImportPath
}
return nil
}
func validateRunConfig(tb testing.TB, cfg *runConfig) {
tb.Helper()
for _, e := range cfg.env {
if strings.HasPrefix(e, "GOCOVERDIR=") {
tb.Fatalf("testastic: WithRunEnv must not include GOCOVERDIR")
}
}
if cfg.timeout < 0 {
tb.Fatalf("testastic: WithRunTimeout must not be negative")
}
}
// testingContext extracts the test context from tb. A type assertion is
// required because [testing.TB] does not include Context() in its interface,
// even though the concrete types [testing.T], [testing.B], and [testing.F]
// all implement it.
func testingContext(tb testing.TB) context.Context {
tb.Helper()
provider, ok := any(tb).(interface{ Context() context.Context })
if !ok {
return context.Background()
}
ctx := provider.Context()
if ctx == nil {
return context.Background()
}
return ctx
}
func binaryOutputName(base string) string {
if runtime.GOOS == "windows" {
return base + ".exe"
}
return base
}
func runGoBuild(ctx context.Context, outputPath string, cfg *buildConfig) ([]byte, error) {
args := make([]string, 0, 4+len(cfg.buildArgs)+1)
args = append(args, "build", "-cover", "-o", outputPath)
args = append(args, cfg.buildArgs...)
args = append(args, cfg.importPath)
cmd := exec.CommandContext(ctx, "go", args...) //nolint:gosec // args are from test config
cmd.Dir = cfg.workDir
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("go build: %w", err)
}
return output, nil
}