@@ -7,12 +7,8 @@ package exec
77import (
88 "bytes"
99 "context"
10- "os/exec"
1110 "testing"
1211
13- "github.com/google/shlex"
14-
15- gdtcontext "github.com/gdt-dev/gdt/context"
1612 "github.com/gdt-dev/gdt/debug"
1713 gdterrors "github.com/gdt-dev/gdt/errors"
1814 "github.com/gdt-dev/gdt/result"
@@ -25,57 +21,39 @@ func (s *Spec) Eval(ctx context.Context, t *testing.T) *result.Result {
2521 outbuf := & bytes.Buffer {}
2622 errbuf := & bytes.Buffer {}
2723
28- var err error
29- var cmd * exec.Cmd
30- var target string
31- var args []string
32- if s .Shell == "" {
33- // Parse time already validated exec string parses into valid shell
34- // args
35- args , _ = shlex .Split (s .Exec )
36- target = args [0 ]
37- args = args [1 :]
38- } else {
39- target = s .Shell
40- args = []string {"-c" , s .Exec }
41- }
42-
43- debug .Println (ctx , t , "exec: %s %s" , target , args )
44- cmd = exec .CommandContext (ctx , target , args ... )
45-
46- outpipe , err := cmd .StdoutPipe ()
47- if err != nil {
48- return result .New (result .WithRuntimeError (ExecRuntimeError (err )))
49- }
50- errpipe , err := cmd .StderrPipe ()
51- if err != nil {
52- return result .New (result .WithRuntimeError (ExecRuntimeError (err )))
53- }
24+ var ec int
5425
55- err = cmd .Start ()
56- if gdtcontext .TimedOut (ctx , err ) {
57- return result .New (result .WithFailures (gdterrors .ErrTimeoutExceeded ))
58- }
59- if err != nil {
26+ if err := s .Do (ctx , t , outbuf , errbuf , & ec ); err != nil {
27+ if err == gdterrors .ErrTimeoutExceeded {
28+ return result .New (result .WithFailures (gdterrors .ErrTimeoutExceeded ))
29+ }
6030 return result .New (result .WithRuntimeError (ExecRuntimeError (err )))
6131 }
62- outbuf .ReadFrom (outpipe )
63- errbuf .ReadFrom (errpipe )
64-
65- err = cmd .Wait ()
66- if gdtcontext .TimedOut (ctx , err ) {
67- return result .New (result .WithFailures (gdterrors .ErrTimeoutExceeded ))
68- }
69- ec := 0
70- if err != nil {
71- eerr , _ := err .(* exec.ExitError )
72- ec = eerr .ExitCode ()
73- }
7432 a := newAssertions (s .Assert , ec , outbuf , errbuf )
7533 if ! a .OK () {
7634 for _ , fail := range a .Failures () {
7735 t .Error (fail )
7836 }
37+ if s .On != nil {
38+ if s .On .Fail != nil {
39+ outbuf .Reset ()
40+ errbuf .Reset ()
41+ err := s .On .Fail .Do (ctx , t , outbuf , errbuf , nil )
42+ if err != nil {
43+ debug .Println (ctx , t , "error in on.fail.exec: %s" , err )
44+ }
45+ if outbuf .Len () > 0 {
46+ debug .Println (
47+ ctx , t , "on.fail.exec: stdout: %s" , outbuf .String (),
48+ )
49+ }
50+ if errbuf .Len () > 0 {
51+ debug .Println (
52+ ctx , t , "on.fail.exec: stderr: %s" , errbuf .String (),
53+ )
54+ }
55+ }
56+ }
7957 }
8058 return result .New (result .WithFailures (a .Failures ()... ))
8159}
0 commit comments