sharedCmds defines a collection of custom testscript commands for our use.
(tsEnv testScriptEnv)
| 278 | |
| 279 | // sharedCmds defines a collection of custom testscript commands for our use. |
| 280 | func sharedCmds(tsEnv testScriptEnv) map[string]func(ts *testscript.TestScript, neg bool, args []string) { |
| 281 | return map[string]func(ts *testscript.TestScript, neg bool, args []string){ |
| 282 | "defer": func(ts *testscript.TestScript, neg bool, args []string) { |
| 283 | if neg { |
| 284 | ts.Fatalf("unsupported: ! defer") |
| 285 | } |
| 286 | |
| 287 | if tsEnv.skipDefer { |
| 288 | return |
| 289 | } |
| 290 | |
| 291 | tt, ok := ts.Value(keyT).(testscript.T) |
| 292 | if !ok { |
| 293 | ts.Fatalf("%v is not a testscript.T", ts.Value(keyT)) |
| 294 | } |
| 295 | |
| 296 | ts.Defer(func() { |
| 297 | // If you're wondering why we're not using ts.Check here, it's because it raises a panic, and testscript |
| 298 | // only catches the panics directly from commands, not from the deferred functions. So what we do |
| 299 | // instead is grab the `t` in the setup function and store it as a value. It's important that we use |
| 300 | // `t` from the setup function because it represents the subtest created for each individual script, |
| 301 | // rather than each top-level test. |
| 302 | // See: https://github.com/rogpeppe/go-internal/issues/276 |
| 303 | if err := ts.Exec(args[0], args[1:]...); err != nil { |
| 304 | tt.FailNow() |
| 305 | } |
| 306 | }) |
| 307 | }, |
| 308 | "env2upper": func(ts *testscript.TestScript, neg bool, args []string) { |
| 309 | if neg { |
| 310 | ts.Fatalf("unsupported: ! env2upper") |
| 311 | } |
| 312 | if len(args) == 0 { |
| 313 | ts.Fatalf("usage: env2upper name=value ...") |
| 314 | } |
| 315 | for _, env := range args { |
| 316 | i := strings.Index(env, "=") |
| 317 | |
| 318 | if i < 0 { |
| 319 | ts.Fatalf("env2upper: argument does not match name=value") |
| 320 | } |
| 321 | |
| 322 | ts.Setenv(env[:i], strings.ToUpper(env[i+1:])) |
| 323 | } |
| 324 | }, |
| 325 | "replace": func(ts *testscript.TestScript, neg bool, args []string) { |
| 326 | if neg { |
| 327 | ts.Fatalf("unsupported: ! replace") |
| 328 | } |
| 329 | if len(args) < 2 { |
| 330 | ts.Fatalf("usage: replace file env...") |
| 331 | } |
| 332 | |
| 333 | src := ts.MkAbs(args[0]) |
| 334 | ts.Logf("replace src: %s", src) |
| 335 | |
| 336 | // Preserve the existing file mode while replacing the contents similar to native cp behavior |
| 337 | info, err := os.Stat(src) |
no test coverage detected