-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_test.go
222 lines (206 loc) · 5.33 KB
/
command_test.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
package main
import (
"bytes"
"reflect"
"regexp"
"testing"
)
func Test_Command_LogModes(test *testing.T) {
cases := []struct {
note string
*Cmd
stderr *regexp.Regexp
isErr bool
}{
// DEFAULT MODE (LogOnError)
{
note: "default mode, success => empty stderr",
Cmd: Command("go", "list", "--", "net/http"),
stderr: regexp.MustCompile(`^$`), // empty
isErr: false,
},
{
note: "default mode, error => cmd + output in stderr",
Cmd: Command("go", "list", "--", "net/http", "foobarNOTEXISTING"),
stderr: regexp.MustCompile(`^# go list -- net/http foobarNOTEXISTING
can't load package: package foobarNOTEXISTING: cannot find package "foobarNOTEXISTING" in any of:
[\s\S]*
net/http
?$`),
isErr: true,
},
{
note: "changed env, error => cmd + env diff + output in stderr",
Cmd: Command("go", "list", "--", "foobarNOTEXISTING").
Setenv("FOOBAX=sOmEtHiNg STuppid", "GOPATH="),
stderr: regexp.MustCompile(`^# FOOBAX=sOmEtHiNg STuppid GOPATH= go list -- foobarNOTEXISTING
can't load package: package foobarNOTEXISTING: cannot find package "foobarNOTEXISTING" in any of:
[\s\S]*$`),
isErr: true,
},
// MODE LogAlways
{
note: "LogAlways, success => cmd in stderr, but no output",
Cmd: Command("go", "list", "--", "net/http").
LogAlways(),
stderr: regexp.MustCompile(`^# go list -- net/http[\s]*$`),
isErr: false,
},
{
note: "LogAlways, error => cmd + output in stderr",
Cmd: Command("go", "list", "--", "net/http", "foobarNOTEXISTING").
LogAlways(),
stderr: regexp.MustCompile(`^# go list -- net/http foobarNOTEXISTING
can't load package: package foobarNOTEXISTING: cannot find package "foobarNOTEXISTING" in any of:
[\s\S]*
net/http
?$`),
isErr: true,
},
// MODE LogNever
{
note: "LogNever, success => empty stderr",
Cmd: Command("go", "list", "--", "net/http").
LogNever(),
stderr: regexp.MustCompile(`^$`),
isErr: false,
},
{
note: "LogNever, changed env, error => empty stderr",
Cmd: Command("go", "list", "--", "net/http", "foobarNOTEXISTING").
LogNever().
Setenv("FOOBAX=sOmEtHiNg STuppid", "GOPATH="),
stderr: regexp.MustCompile(`^$`),
isErr: true,
},
}
for _, c := range cases {
buf := bytes.NewBuffer(nil)
stderr = buf
err := c.Cmd.DiscardOutput()
if c.isErr != (err != nil) {
test.Errorf("case %q [%s] expected isErr=%v, got err=%v",
c.note, c.Cmd.Cmd.Args, c.isErr, err)
}
if !c.stderr.Match(buf.Bytes()) {
test.Errorf("case %q [%s] expected stderr matching %q, got:\n%s",
c.note, c.Cmd.Cmd.Args, c.stderr, buf.String())
}
}
}
func Test_Command_Setenv(test *testing.T) {
cases := []struct {
env []string
query string
expected []string
}{
{
env: []string{"GOOS=windows"},
query: "GOOS",
expected: []string{"windows"},
},
{
env: []string{"GOOS=linux"},
query: "GOOS",
expected: []string{"linux"},
},
{
env: []string{"GOARCH=arm", "GOOS=windows"},
query: "GOOS",
expected: []string{"windows"},
},
{
env: []string{"GOARCH=arm", "GOOS=linux"},
query: "GOOS",
expected: []string{"linux"},
},
{
env: []string{"GOPATH="},
query: "GOPATH",
expected: nil,
},
}
for _, c := range cases {
lines, err := Command("go", "env", c.query).Setenv(c.env...).OutputLines()
if err != nil {
test.Errorf("case %v error: %v", err)
}
if !reflect.DeepEqual(lines, c.expected) {
test.Errorf("case %v expected: %v, got: %v", c, c.expected, lines)
}
}
}
func Test_Command_OutputOneLine(test *testing.T) {
// 0 lines -> error
// 1 line -> ok
// 2,3 lines -> error
cases := []struct {
note string
*Cmd
expected string
isErr bool
}{
{
note: "0 lines -> error",
Cmd: Command("go", "list", "-f", "", "--", "net/http"),
isErr: true,
},
{
note: "1 line -> ok",
Cmd: Command("go", "list", "--", "net/http"),
expected: "net/http",
isErr: false,
},
{
note: "1 line with trailing empty line -> still ok, still '1 line'",
Cmd: Command("go", "env", "GOOS", "GOPATH").
Setenv("GOOS=linux", "GOPATH="),
expected: "linux",
isErr: false,
},
{
note: "2 lines -> error",
Cmd: Command("go", "list", "--", "net", "net/http"),
isErr: true,
},
{
note: "3 lines -> error",
Cmd: Command("go", "list", "--", "net", "net/http", "os"),
isErr: true,
},
}
for _, c := range cases {
line, err := c.Cmd.OutputOneLine()
if c.isErr != (err != nil) {
test.Errorf("case %q [%s] expected isErr=%v, got err=%v",
c.note, c.Cmd.Cmd.Args, c.isErr, err)
}
if c.isErr {
continue
}
if c.expected != line {
test.Errorf("case %q [%s] expected: %q, got: %q",
c.note, c.Cmd.Cmd.Args, c.expected, line)
}
}
}
func Test_Command_OutputLines_Nil(test *testing.T) {
lines, err := Command("go", "env", "GOPATH").Setenv("GOPATH=").OutputLines()
if err != nil {
test.Errorf("unexpected err=%v", err)
}
if lines != nil {
test.Errorf("expected lines==nil, got: %q", lines)
}
}
func Test_Command_OutputLines_Many(test *testing.T) {
lines, err := Command("go", "list", "--", "net", "net/http", "os").OutputLines()
if err != nil {
test.Errorf("unexpected err=%v", err)
}
expected := []string{"net", "net/http", "os"}
if !reflect.DeepEqual(lines, expected) {
test.Errorf("OutputLines() expected:\n%q\ngot:\n%q",
expected, lines)
}
}