| |
| |
| |
|
|
| |
| package scripttest |
|
|
| import ( |
| "bufio" |
| "cmd/internal/pathcache" |
| "cmd/internal/script" |
| "errors" |
| "io" |
| "strings" |
| "testing" |
| ) |
|
|
| |
| |
| |
| |
| |
| func DefaultCmds() map[string]script.Cmd { |
| cmds := script.DefaultCmds() |
| cmds["skip"] = Skip() |
| return cmds |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func DefaultConds() map[string]script.Cond { |
| conds := script.DefaultConds() |
| conds["exec"] = CachedExec() |
| conds["short"] = script.BoolCondition("testing.Short()", testing.Short()) |
| conds["verbose"] = script.BoolCondition("testing.Verbose()", testing.Verbose()) |
| return conds |
| } |
|
|
| |
| |
| func Run(t testing.TB, e *script.Engine, s *script.State, filename string, testScript io.Reader) { |
| t.Helper() |
| err := func() (err error) { |
| log := new(strings.Builder) |
| log.WriteString("\n") |
|
|
| |
| |
| t.Helper() |
| defer func() { |
| t.Helper() |
|
|
| if closeErr := s.CloseAndWait(log); err == nil { |
| err = closeErr |
| } |
|
|
| if log.Len() > 0 { |
| t.Log(strings.TrimSuffix(log.String(), "\n")) |
| } |
| }() |
|
|
| if testing.Verbose() { |
| |
| wait, err := script.Env().Run(s) |
| if err != nil { |
| t.Fatal(err) |
| } |
| if wait != nil { |
| stdout, stderr, err := wait(s) |
| if err != nil { |
| t.Fatalf("env: %v\n%s", err, stderr) |
| } |
| if len(stdout) > 0 { |
| s.Logf("%s\n", stdout) |
| } |
| } |
| } |
|
|
| return e.Execute(s, filename, bufio.NewReader(testScript), log) |
| }() |
|
|
| if skip, ok := errors.AsType[skipError](err); ok { |
| if skip.msg == "" { |
| t.Skip("SKIP") |
| } else { |
| t.Skipf("SKIP: %v", skip.msg) |
| } |
| } |
| if err != nil { |
| t.Errorf("FAIL: %v", err) |
| } |
| } |
|
|
| |
| func Skip() script.Cmd { |
| return script.Command( |
| script.CmdUsage{ |
| Summary: "skip the current test", |
| Args: "[msg]", |
| }, |
| func(_ *script.State, args ...string) (script.WaitFunc, error) { |
| if len(args) > 1 { |
| return nil, script.ErrUsage |
| } |
| if len(args) == 0 { |
| return nil, skipError{""} |
| } |
| return nil, skipError{args[0]} |
| }) |
| } |
|
|
| type skipError struct { |
| msg string |
| } |
|
|
| func (s skipError) Error() string { |
| if s.msg == "" { |
| return "skip" |
| } |
| return s.msg |
| } |
|
|
| |
| |
| |
| func CachedExec() script.Cond { |
| return script.CachedCondition( |
| "<suffix> names an executable in the test binary's PATH", |
| func(name string) (bool, error) { |
| _, err := pathcache.LookPath(name) |
| return err == nil, nil |
| }) |
| } |
|
|