-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathexec.go
More file actions
129 lines (116 loc) · 3.03 KB
/
exec.go
File metadata and controls
129 lines (116 loc) · 3.03 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
package envtest
import (
"bytes"
"context"
"fmt"
"io"
"os"
"time"
"github.com/stretchr/testify/assert"
"github.com/ignite/cli/v29/ignite/pkg/cmdrunner"
"github.com/ignite/cli/v29/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/v29/ignite/pkg/env"
"github.com/ignite/cli/v29/ignite/pkg/errors"
)
type execOptions struct {
ctx context.Context
shouldErr, shouldRetry bool
stdout, stderr io.Writer
}
type ExecOption func(*execOptions)
// ExecShouldError sets the expectations of a command's execution to end with a failure.
func ExecShouldError() ExecOption {
return func(o *execOptions) {
o.shouldErr = true
}
}
// ExecCtx sets cancelation context for the execution.
func ExecCtx(ctx context.Context) ExecOption {
return func(o *execOptions) {
o.ctx = ctx
}
}
// ExecStdout captures stdout of an execution.
func ExecStdout(w io.Writer) ExecOption {
return func(o *execOptions) {
o.stdout = w
}
}
// ExecStderr captures stderr of an execution.
func ExecStderr(w io.Writer) ExecOption {
return func(o *execOptions) {
o.stderr = w
}
}
// ExecRetry retries command until it is successful before context is canceled.
func ExecRetry() ExecOption {
return func(o *execOptions) {
o.shouldRetry = true
}
}
// Exec executes a command step with options where msg describes the expectation from the test.
// unless calling with Must(), Exec() will not exit test runtime on failure.
func (e Env) Exec(msg string, steps step.Steps, options ...ExecOption) (ok bool) {
opts := &execOptions{
ctx: e.ctx,
stdout: io.Discard,
stderr: io.Discard,
}
for _, o := range options {
o(opts)
}
var (
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}
)
copts := []cmdrunner.Option{
cmdrunner.DefaultStdout(io.MultiWriter(stdout, opts.stdout)),
cmdrunner.DefaultStderr(io.MultiWriter(stderr, opts.stderr)),
}
if HasTestVerboseFlag() {
fmt.Printf("Executing %d step(s) for %q\n", len(steps), msg)
copts = append(copts, cmdrunner.EnableDebug())
}
if IsCI {
copts = append(copts, cmdrunner.EndSignal(os.Kill))
}
defaultEnvs := []string{
fmt.Sprintf("%s=true", envDoNotTrack),
}
if e.configDir != "" {
defaultEnvs = append(defaultEnvs, fmt.Sprintf("%s=%s", env.ConfigDirEnvVar, e.configDir))
}
preparedSteps := make(step.Steps, 0, len(steps))
for _, s := range steps {
if s == nil {
preparedSteps = append(preparedSteps, nil)
continue
}
cloned := *s
cloned.Env = append(append([]string{}, defaultEnvs...), s.Env...)
preparedSteps = append(preparedSteps, &cloned)
}
err := cmdrunner.
New(copts...).
Run(opts.ctx, preparedSteps...)
if errors.Is(err, context.Canceled) {
err = nil
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
if opts.shouldRetry && opts.ctx.Err() == nil {
time.Sleep(time.Second)
return e.Exec(msg, steps, options...)
}
}
if err != nil {
msg = fmt.Sprintf("%s\n\nLogs:\n\n%s\n\nError Logs:\n\n%s\n",
msg,
stdout.String(),
stderr.String())
}
if opts.shouldErr {
return assert.Error(e.t, err, msg)
}
return assert.NoError(e.t, err, msg)
}