added more tests to cmdrunner (reproduce another ?? cmdrunner bug...)
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package cmdext
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/syncext"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrExitCode = errors.New("process exited with an unexpected exitcode")
|
||||
var ErrTimeout = errors.New("process did not exit after the specified timeout")
|
||||
|
||||
type CommandResult struct {
|
||||
StdOut string
|
||||
StdErr string
|
||||
@@ -81,21 +86,29 @@ func run(opt CommandRunner) (CommandResult, error) {
|
||||
if fallback, ok := syncext.ReadChannelWithTimeout(outputChan, mathext.Min(32*time.Millisecond, *opt.timeout)); ok {
|
||||
// most of the time the cmd.Process.Kill() should also ahve finished the pipereader
|
||||
// and we can at least return the already collected stdout, stderr, etc
|
||||
return CommandResult{
|
||||
res := CommandResult{
|
||||
StdOut: fallback.stdout,
|
||||
StdErr: fallback.stderr,
|
||||
StdCombined: fallback.stdcombined,
|
||||
ExitCode: -1,
|
||||
CommandTimedOut: true,
|
||||
}, nil
|
||||
}
|
||||
if opt.enforceNoTimeout {
|
||||
return res, ErrTimeout
|
||||
}
|
||||
return res, nil
|
||||
} else {
|
||||
return CommandResult{
|
||||
res := CommandResult{
|
||||
StdOut: "",
|
||||
StdErr: "",
|
||||
StdCombined: "",
|
||||
ExitCode: -1,
|
||||
CommandTimedOut: true,
|
||||
}, nil
|
||||
}
|
||||
if opt.enforceNoTimeout {
|
||||
return res, ErrTimeout
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
case outobj := <-outputChan:
|
||||
@@ -104,26 +117,34 @@ func run(opt CommandRunner) (CommandResult, error) {
|
||||
for _, lstr := range opt.listener {
|
||||
lstr.Finished(excode)
|
||||
}
|
||||
return CommandResult{
|
||||
res := CommandResult{
|
||||
StdOut: outobj.stdout,
|
||||
StdErr: outobj.stderr,
|
||||
StdCombined: outobj.stdcombined,
|
||||
ExitCode: excode,
|
||||
CommandTimedOut: false,
|
||||
}, nil
|
||||
}
|
||||
if opt.enforceExitCodes != nil && !langext.InArray(excode, *opt.enforceExitCodes) {
|
||||
return res, ErrExitCode
|
||||
}
|
||||
return res, nil
|
||||
} else if err != nil {
|
||||
return CommandResult{}, err
|
||||
} else {
|
||||
for _, lstr := range opt.listener {
|
||||
lstr.Finished(0)
|
||||
}
|
||||
return CommandResult{
|
||||
res := CommandResult{
|
||||
StdOut: outobj.stdout,
|
||||
StdErr: outobj.stderr,
|
||||
StdCombined: outobj.stdcombined,
|
||||
ExitCode: 0,
|
||||
CommandTimedOut: false,
|
||||
}, nil
|
||||
}
|
||||
if opt.enforceExitCodes != nil && !langext.InArray(0, *opt.enforceExitCodes) {
|
||||
return res, ErrExitCode
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user