Skip to content

Commit

Permalink
Render stderr when execution failed
Browse files Browse the repository at this point in the history
  • Loading branch information
travelist committed May 26, 2020
1 parent 6c007bb commit 178f9c3
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ var testCommand = func(command *cobra.Command, args []string) {
allSuccess := true

for index, _ := range testIns {
fmt.Printf("# in_%d.txt ... ", index+1)

if e := executeBeforeEach(); e != nil {
color.Set(color.FgRed)
fmt.Printf("%v\n", e)
Expand All @@ -69,6 +71,7 @@ var testCommand = func(command *cobra.Command, args []string) {
// TODO TLE check
out, e := executeCommand(strings.Split(c, " "), testIns[index])
if e != nil {
fmt.Println()
color.Set(color.FgRed)
fmt.Printf("%v\n", e)
color.Unset()
Expand All @@ -84,10 +87,10 @@ var testCommand = func(command *cobra.Command, args []string) {
}

if success {
fmt.Printf("# in_%d.txt ... %s\n", index+1, color.GreenString("AC"))
fmt.Printf("%s\n", color.GreenString("AC"))
} else {
allSuccess = false
fmt.Printf("# in_%d.txt ... %s\n", index+1, color.RedString("WA"))
fmt.Printf("%s\n", color.RedString("WA"))

fmt.Printf("%s\n", color.MagentaString("[Input]"))
in, _ := getFileBody(testIns[index])
Expand Down Expand Up @@ -201,12 +204,14 @@ func executeCommand(command []string, inputFilePath string) ([]byte, error) {
io.Copy(stdin, in)

b, e := c.Output()
if e != nil {
color.Set(color.FgRed)
fmt.Printf("%v\n", stderr.String())
color.Unset()
return b, e
if e != nil || stderr.Len() > 0 {
return b, &ExecError{
err: e,
stdout: b,
stderr: stderr,
}
}

return b, e
}

Expand Down Expand Up @@ -335,3 +340,21 @@ func executeAfterAll() (e error) {

return nil
}

type ExecError struct {
err error
stdout []byte
stderr bytes.Buffer
}

func (e ExecError) Error() string {
if e.stderr.Len() > 0 {
return e.stderr.String()
}

if e.err != nil {
return e.err.Error()
}

return "Command execution error"
}

0 comments on commit 178f9c3

Please sign in to comment.