sharedCmds defines a collection of custom testscript commands for our use.
(tsEnv testScriptEnv)
| 355 | |
| 356 | // sharedCmds defines a collection of custom testscript commands for our use. |
| 357 | func sharedCmds(tsEnv testScriptEnv) map[string]func(ts *testscript.TestScript, neg bool, args []string) { |
| 358 | return map[string]func(ts *testscript.TestScript, neg bool, args []string){ |
| 359 | "defer": func(ts *testscript.TestScript, neg bool, args []string) { |
| 360 | if neg { |
| 361 | ts.Fatalf("unsupported: ! defer") |
| 362 | } |
| 363 | |
| 364 | if tsEnv.skipDefer { |
| 365 | return |
| 366 | } |
| 367 | |
| 368 | tt, ok := ts.Value(keyT).(testscript.T) |
| 369 | if !ok { |
| 370 | ts.Fatalf("%v is not a testscript.T", ts.Value(keyT)) |
| 371 | } |
| 372 | |
| 373 | ts.Defer(func() { |
| 374 | // If you're wondering why we're not using ts.Check here, it's because it raises a panic, and testscript |
| 375 | // only catches the panics directly from commands, not from the deferred functions. So what we do |
| 376 | // instead is grab the `t` in the setup function and store it as a value. It's important that we use |
| 377 | // `t` from the setup function because it represents the subtest created for each individual script, |
| 378 | // rather than each top-level test. |
| 379 | // See: https://github.com/rogpeppe/go-internal/issues/276 |
| 380 | if err := ts.Exec(args[0], args[1:]...); err != nil { |
| 381 | tt.FailNow() |
| 382 | } |
| 383 | }) |
| 384 | }, |
| 385 | "env2upper": func(ts *testscript.TestScript, neg bool, args []string) { |
| 386 | if neg { |
| 387 | ts.Fatalf("unsupported: ! env2upper") |
| 388 | } |
| 389 | if len(args) == 0 { |
| 390 | ts.Fatalf("usage: env2upper name=value ...") |
| 391 | } |
| 392 | for _, env := range args { |
| 393 | i := strings.Index(env, "=") |
| 394 | |
| 395 | if i < 0 { |
| 396 | ts.Fatalf("env2upper: argument does not match name=value") |
| 397 | } |
| 398 | |
| 399 | ts.Setenv(env[:i], strings.ToUpper(env[i+1:])) |
| 400 | } |
| 401 | }, |
| 402 | "generate-ssh-key": func(ts *testscript.TestScript, neg bool, args []string) { |
| 403 | if neg { |
| 404 | ts.Fatalf("unsupported: ! generate-ssh-key") |
| 405 | } |
| 406 | if len(args) < 1 || len(args) > 2 { |
| 407 | ts.Fatalf("usage: generate-ssh-key file [comment]") |
| 408 | } |
| 409 | |
| 410 | comment := "" |
| 411 | if len(args) == 2 { |
| 412 | comment = args[1] |
| 413 | } |
| 414 | publicKey, err := generateSSHPublicKey(comment) |
no test coverage detected