-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
207 lines (186 loc) · 5.93 KB
/
Copy pathcommand_test.go
File metadata and controls
207 lines (186 loc) · 5.93 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
package processkit
import (
"context"
"errors"
"strings"
"testing"
"time"
)
// fakeRunner is a hermetic ProcessRunner double: it records the invocation and
// returns a canned result, so verb semantics are tested with no subprocess.
type fakeRunner struct {
res *Result
err error
inv Invocation
}
func (f *fakeRunner) Output(_ context.Context, inv Invocation) (*Result, error) {
f.inv = inv
return f.res, f.err
}
func cmdWith(f *fakeRunner) *Cmd { return Command("p").WithRunner(f) }
func TestOutput_NonZeroExitIsData(t *testing.T) {
f := &fakeRunner{res: &Result{program: "p", outcome: exited(3), stdout: []byte("hi")}}
res, err := cmdWith(f).Output(context.Background())
if err != nil {
t.Fatalf("Output must not error on a non-zero exit: %v", err)
}
if c, ok := res.Code(); !ok || c != 3 {
t.Fatalf("Code() = (%d, %v), want (3, true)", c, ok)
}
if res.Success() {
t.Fatal("exit 3 should not be a success")
}
}
func TestRun_TrimsAndRequiresSuccess(t *testing.T) {
f := &fakeRunner{res: &Result{program: "p", outcome: exited(0), stdout: []byte(" hello \n")}}
out, err := cmdWith(f).Run(context.Background())
if err != nil {
t.Fatalf("Run: %v", err)
}
if out != " hello" {
t.Fatalf("Run trimmed = %q, want %q", out, " hello")
}
f2 := &fakeRunner{res: &Result{program: "p", outcome: exited(1), stderr: "boom"}}
_, err = cmdWith(f2).Run(context.Background())
var ee *ExitError
if !errors.As(err, &ee) {
t.Fatalf("Run on a failure: want *ExitError, got %v", err)
}
}
func TestExitCode(t *testing.T) {
f := &fakeRunner{res: &Result{outcome: exited(7)}}
code, err := cmdWith(f).ExitCode(context.Background())
if err != nil || code != 7 {
t.Fatalf("ExitCode = (%d, %v), want (7, nil)", code, err)
}
f2 := &fakeRunner{res: &Result{outcome: timedOut()}}
if _, err := cmdWith(f2).ExitCode(context.Background()); !errors.Is(err, ErrTimeout) {
t.Fatalf("timed-out ExitCode: want ErrTimeout, got %v", err)
}
}
func TestProbe(t *testing.T) {
cases := []struct {
code int
want bool
wantErr bool
}{{0, true, false}, {1, false, false}, {2, false, true}}
for _, tc := range cases {
f := &fakeRunner{res: &Result{outcome: exited(tc.code)}}
ok, err := cmdWith(f).Probe(context.Background())
if (err != nil) != tc.wantErr || ok != tc.want {
t.Fatalf("Probe(exit %d) = (%v, %v), want (%v, err=%v)", tc.code, ok, err, tc.want, tc.wantErr)
}
}
}
func TestOkCodes(t *testing.T) {
// Plumbing: WithOkCodes reaches the invocation.
f := &fakeRunner{res: &Result{outcome: exited(0)}}
_, _ = Command("p").WithRunner(f).WithOkCodes(2, 3).Output(context.Background())
if len(f.inv.OkCodes) != 2 || f.inv.OkCodes[0] != 2 {
t.Fatalf("inv.OkCodes = %v, want [2 3]", f.inv.OkCodes)
}
// Semantics: an Ok code is a success.
r := &Result{outcome: exited(2), okCodes: []int{2}}
if !r.Success() {
t.Fatal("exit 2 with OkCodes{2} should be a success")
}
if r.Err() != nil {
t.Fatalf("Err on an OK code should be nil: %v", r.Err())
}
}
func TestExitErrorMatchesErrTimeout(t *testing.T) {
f := &fakeRunner{res: &Result{program: "p", outcome: timedOut(), stdout: []byte("partial")}}
_, err := cmdWith(f).Run(context.Background())
if !errors.Is(err, ErrTimeout) {
t.Fatalf("want errors.Is ErrTimeout, got %v", err)
}
var ee *ExitError
if !errors.As(err, &ee) || !ee.Outcome.TimedOut() {
t.Fatalf("want a timed-out *ExitError, got %v", err)
}
}
func TestInvocationPlumbing(t *testing.T) {
f := &fakeRunner{res: &Result{outcome: exited(0)}}
_, _ = Command("git", "status").
WithRunner(f).
WithArgs("--porcelain").
WithDir("/repo").
WithEnv("A=1", "B=2").
WithTimeout(5 * time.Second).
Output(context.Background())
inv := f.inv
if inv.Program != "git" {
t.Fatalf("Program = %q", inv.Program)
}
if strings.Join(inv.Args, " ") != "status --porcelain" {
t.Fatalf("Args = %v", inv.Args)
}
if inv.Dir != "/repo" {
t.Fatalf("Dir = %q", inv.Dir)
}
if strings.Join(inv.Env, ",") != "A=1,B=2" {
t.Fatalf("Env = %v", inv.Env)
}
if inv.Timeout != 5*time.Second {
t.Fatalf("Timeout = %v", inv.Timeout)
}
}
func TestExitErrorSanitizesControlChars(t *testing.T) {
ee := &ExitError{Program: "p", Outcome: exited(1), Stderr: "bad\x1b[31mred\x1b[0m output"}
msg := ee.Error()
if strings.ContainsRune(msg, 0x1b) {
t.Fatalf("ANSI escape leaked into error string: %q", msg)
}
if !strings.Contains(msg, "exited with code 1") {
t.Fatalf("error string missing exit info: %q", msg)
}
}
// TestCmd_WithIsCopyOnWrite guards the documented copy-on-write contract: a
// partly-configured Cmd can be branched without the branches interfering.
func TestCmd_WithIsCopyOnWrite(t *testing.T) {
base := Command("git").WithDir("/repo")
a := base.WithArgs("status")
b := base.WithArgs("log")
f := &fakeRunner{res: &Result{outcome: exited(0)}}
_, _ = a.WithRunner(f).Output(context.Background())
if got := strings.Join(f.inv.Args, " "); got != "status" {
t.Fatalf("a args = %q, want %q (b leaked into a)", got, "status")
}
_, _ = b.WithRunner(f).Output(context.Background())
if got := strings.Join(f.inv.Args, " "); got != "log" {
t.Fatalf("b args = %q, want %q", got, "log")
}
}
// TestResult_AccessorsReturnCopies guards that Args/StdoutBytes hand out copies,
// so a caller mutating the returned slice can't corrupt the Result.
func TestResult_AccessorsReturnCopies(t *testing.T) {
r := &Result{args: []string{"a"}, stdout: []byte("xy")}
got := r.Args()
got[0] = "MUT"
if r.Args()[0] != "a" {
t.Fatal("Args() must return a copy")
}
b := r.StdoutBytes()
b[0] = 'Z'
if r.StdoutBytes()[0] != 'x' {
t.Fatal("StdoutBytes() must return a copy")
}
}
func TestOutcomeString(t *testing.T) {
sig := 9
cases := []struct {
got Outcome
want string
}{
{exited(0), "exited(0)"},
{exited(3), "exited(3)"},
{signalled(&sig), "signalled(9)"},
{signalled(nil), "signalled"},
{timedOut(), "timedOut"},
}
for _, tc := range cases {
if got := tc.got.String(); got != tc.want {
t.Errorf("Outcome.String() = %q, want %q", got, tc.want)
}
}
}