forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance_test.go
421 lines (340 loc) · 9.8 KB
/
acceptance_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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//go:build acceptance
package acceptance_test
import (
"fmt"
"os"
"path"
"strconv"
"strings"
"testing"
"time"
"math/rand"
"github.com/cli/cli/v2/internal/ghcmd"
"github.com/cli/go-internal/testscript"
)
func ghMain() int {
return int(ghcmd.Main())
}
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"gh": ghMain,
}))
}
func TestAPI(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "api"))
}
func TestAuth(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "auth"))
}
func TestGPGKeys(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "gpg-key"))
}
func TestExtensions(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "extension"))
}
func TestIssues(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "pr"))
}
func TestLabels(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "label"))
}
func TestOrg(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "org"))
}
func TestProject(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "project"))
}
func TestPullRequests(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "pr"))
}
func TestReleases(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "release"))
}
func TestRepo(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "repo"))
}
func TestRulesets(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "ruleset"))
}
func TestSearches(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "search"))
}
func TestSecrets(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "secret"))
}
func TestSSHKeys(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "ssh-key"))
}
func TestVariables(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "variable"))
}
func TestWorkflows(t *testing.T) {
var tsEnv testScriptEnv
if err := tsEnv.fromEnv(); err != nil {
t.Fatal(err)
}
testscript.Run(t, testScriptParamsFor(tsEnv, "workflow"))
}
func testScriptParamsFor(tsEnv testScriptEnv, command string) testscript.Params {
var files []string
if tsEnv.script != "" {
files = []string{path.Join("testdata", command, tsEnv.script)}
}
var dir string
if len(files) == 0 {
dir = path.Join("testdata", command)
}
return testscript.Params{
Dir: dir,
Files: files,
Setup: sharedSetup(tsEnv),
Cmds: sharedCmds(tsEnv),
RequireExplicitExec: true,
RequireUniqueNames: true,
TestWork: tsEnv.preserveWorkDir,
}
}
var keyT struct{}
func sharedSetup(tsEnv testScriptEnv) func(ts *testscript.Env) error {
return func(ts *testscript.Env) error {
scriptName, ok := extractScriptName(ts.Vars)
if !ok {
ts.T().Fatal("script name not found")
}
// When using script name to uniquely identify where test data comes from,
// some places like GitHub Actions secret names don't accept hyphens.
// Replace them with underscores until such a time this becomes a problem.
ts.Setenv("SCRIPT_NAME", strings.ReplaceAll(scriptName, "-", "_"))
ts.Setenv("HOME", ts.Cd)
ts.Setenv("GH_CONFIG_DIR", ts.Cd)
ts.Setenv("GH_HOST", tsEnv.host)
ts.Setenv("ORG", tsEnv.org)
ts.Setenv("GH_TOKEN", tsEnv.token)
ts.Setenv("RANDOM_STRING", randomString(10))
ts.Values[keyT] = ts.T()
return nil
}
}
// sharedCmds defines a collection of custom testscript commands for our use.
func sharedCmds(tsEnv testScriptEnv) map[string]func(ts *testscript.TestScript, neg bool, args []string) {
return map[string]func(ts *testscript.TestScript, neg bool, args []string){
"defer": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! defer")
}
if tsEnv.skipDefer {
return
}
tt, ok := ts.Value(keyT).(testscript.T)
if !ok {
ts.Fatalf("%v is not a testscript.T", ts.Value(keyT))
}
ts.Defer(func() {
// If you're wondering why we're not using ts.Check here, it's because it raises a panic, and testscript
// only catches the panics directly from commands, not from the deferred functions. So what we do
// instead is grab the `t` in the setup function and store it as a value. It's important that we use
// `t` from the setup function because it represents the subtest created for each individual script,
// rather than each top-level test.
// See: https://github.com/rogpeppe/go-internal/issues/276
if err := ts.Exec(args[0], args[1:]...); err != nil {
tt.FailNow()
}
})
},
"env2upper": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! env2upper")
}
if len(args) == 0 {
ts.Fatalf("usage: env2upper name=value ...")
}
for _, env := range args {
i := strings.Index(env, "=")
if i < 0 {
ts.Fatalf("env2upper: argument does not match name=value")
}
ts.Setenv(env[:i], strings.ToUpper(env[i+1:]))
}
},
"replace": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! replace")
}
if len(args) < 2 {
ts.Fatalf("usage: replace file env...")
}
src := ts.MkAbs(args[0])
ts.Logf("replace src: %s", src)
// Preserve the existing file mode while replacing the contents similar to native cp behavior
info, err := os.Stat(src)
ts.Check(err)
mode := info.Mode() & 0o777
data, err := os.ReadFile(src)
ts.Check(err)
for _, arg := range args[1:] {
i := strings.Index(arg, "=")
if i < 0 {
ts.Fatalf("replace: %s argument does not match name=value", arg)
}
name := fmt.Sprintf("$%s", arg[:i])
value := arg[i+1:]
ts.Logf("replace %s: %s", name, value)
// `replace` was originally built similar to `cp` and `cmpenv`, expanding environment variables within a file.
// However files with content that looks like environments variable such as GitHub Actions workflows
// were being modified unexpectedly. Thus `replace` has been designed to using string replacement
// looking for `$KEY` specifically.
data = []byte(strings.ReplaceAll(string(data), name, value))
}
ts.Check(os.WriteFile(src, data, mode))
},
"stdout2env": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! stdout2env")
}
if len(args) != 1 {
ts.Fatalf("usage: stdout2env name")
}
ts.Setenv(args[0], strings.TrimRight(ts.ReadFile("stdout"), "\n"))
},
"sleep": func(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! sleep")
}
if len(args) != 1 {
ts.Fatalf("usage: sleep seconds")
}
// sleep for the given number of seconds
seconds, err := strconv.Atoi(args[0])
if err != nil {
ts.Fatalf("invalid number of seconds: %v", err)
}
d := time.Duration(seconds) * time.Second
time.Sleep(d)
},
}
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func extractScriptName(vars []string) (string, bool) {
for _, kv := range vars {
if strings.HasPrefix(kv, "WORK=") {
v := strings.Split(kv, "=")[1]
return strings.CutPrefix(path.Base(v), "script-")
}
}
return "", false
}
type missingEnvError struct {
missingEnvs []string
}
func (e missingEnvError) Error() string {
return fmt.Sprintf("environment variable(s) %s must be set and non-empty", strings.Join(e.missingEnvs, ", "))
}
type testScriptEnv struct {
host string
org string
token string
script string
skipDefer bool
preserveWorkDir bool
}
func (e *testScriptEnv) fromEnv() error {
envMap := map[string]string{}
requiredEnvVars := []string{
"GH_ACCEPTANCE_HOST",
"GH_ACCEPTANCE_ORG",
"GH_ACCEPTANCE_TOKEN",
}
var missingEnvs []string
for _, key := range requiredEnvVars {
val, ok := os.LookupEnv(key)
if val == "" || !ok {
missingEnvs = append(missingEnvs, key)
continue
}
envMap[key] = val
}
if len(missingEnvs) > 0 {
return missingEnvError{missingEnvs: missingEnvs}
}
if envMap["GH_ACCEPTANCE_ORG"] == "github" || envMap["GH_ACCEPTANCE_ORG"] == "cli" {
return fmt.Errorf("GH_ACCEPTANCE_ORG cannot be 'github' or 'cli'")
}
e.host = envMap["GH_ACCEPTANCE_HOST"]
e.org = envMap["GH_ACCEPTANCE_ORG"]
e.token = envMap["GH_ACCEPTANCE_TOKEN"]
e.script = os.Getenv("GH_ACCEPTANCE_SCRIPT")
e.preserveWorkDir = os.Getenv("GH_ACCEPTANCE_PRESERVE_WORK_DIR") == "true"
e.skipDefer = os.Getenv("GH_ACCEPTANCE_SKIP_DEFER") == "true"
return nil
}