Skip to content

Commit

Permalink
Test that execution stops if ScanTestOutput errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Nov 1, 2020
1 parent 876c2f5 commit 7e74fa1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
20 changes: 10 additions & 10 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,18 +562,10 @@ func ScanTestOutput(config ScanConfig) (*Execution, error) {

var group errgroup.Group
group.Go(func() error {
if err := readStdout(config, execution); err != nil {
config.Stop()
return err
}
return nil
return stopOnError(config.Stop, readStdout(config, execution))
})
group.Go(func() error {
if err := readStderr(config, execution); err != nil {
config.Stop()
return err
}
return nil
return stopOnError(config.Stop, readStderr(config, execution))
})

err := group.Wait()
Expand All @@ -585,6 +577,14 @@ func ScanTestOutput(config ScanConfig) (*Execution, error) {
return execution, err
}

func stopOnError(stop func(), err error) error {
if err != nil {
stop()
return err
}
return nil
}

func readStdout(config ScanConfig, execution *Execution) error {
scanner := bufio.NewScanner(config.Stdout)
for scanner.Scan() {
Expand Down
32 changes: 32 additions & 0 deletions testjson/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testjson

import (
"bytes"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -58,3 +59,34 @@ func TestScanTestOutput_MinimalConfig(t *testing.T) {
// a weak check to show that all the stdout was scanned
assert.Equal(t, exec.Total(), 46)
}

func TestScanTestOutput_CallsStopOnError(t *testing.T) {
var called bool
stop := func() {
called = true
}
cfg := ScanConfig{
Stdout: bytes.NewReader(golden.Get(t, "go-test-json.out")),
Handler: &handlerFails{},
Stop: stop,
}
_, err := ScanTestOutput(cfg)
assert.Error(t, err, "something failed")
assert.Assert(t, called)
}

type handlerFails struct {
count int
}

func (s *handlerFails) Event(_ TestEvent, _ *Execution) error {
if s.count > 1 {
return fmt.Errorf("something failed")
}
s.count++
return nil
}

func (s *handlerFails) Err(_ string) error {
return nil
}

0 comments on commit 7e74fa1

Please sign in to comment.