Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
issue #1: combine gotest with panicparse by pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Feb 1, 2021
1 parent c4b59f0 commit 323a797
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
11 changes: 10 additions & 1 deletion internal/cmd/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"io"
"io/ioutil"
"os"
"os/signal"

Expand Down Expand Up @@ -51,7 +52,15 @@ func Golang() *cobra.Command {
return err
}

job.Do(stream.GoTest(input, cmd.OutOrStdout()).Process, unsafe.Ignore)
job.Do(
stream.Pipe(input, cmd.OutOrStdout(),
stream.GoTest,
stream.GoTestStackTrace,
).Process,
func(err error) {
unsafe.DoSilent(io.Copy(ioutil.Discard, input))
},
)

if err := task.Run(); err != nil {
cmd.SilenceErrors = true
Expand Down
36 changes: 36 additions & 0 deletions internal/stream/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package stream
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"strings"

"github.com/fatih/color"
"go.octolab.org/async"
"go.octolab.org/safe"
"go.octolab.org/unsafe"
)

type Processor interface {
Expand All @@ -18,6 +23,37 @@ func (fn Process) Process() error {
return fn()
}

func Pipe(input Reader, output Writer, processors ...func(Reader, Writer) Processor) Processor {
if len(processors) == 1 {
return processors[0](input, output)
}
return Process(func() error {
job := new(async.Job)
defer job.Wait()

for _, build := range processors {
in, out := io.Pipe()
processor := build(input, out)
job.Do(func() error {
defer safe.Close(out, unsafe.Ignore)
return processor.Process()
}, func(err error) {
unsafe.DoSilent(io.Copy(ioutil.Discard, in))
})
input = in
}

job.Do(func() error {
_, err := io.Copy(output, input)
return err
}, func(err error) {
unsafe.DoSilent(io.Copy(ioutil.Discard, input))
})

return nil
})
}

func GoTest(input Reader, output Writer) Processor {
const (
fail = color.FgHiRed
Expand Down
24 changes: 11 additions & 13 deletions internal/stream/panicparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package stream_test
import (
"bytes"
"flag"
"io"
"io/ioutil"
"os"
"testing"
Expand All @@ -23,29 +22,28 @@ func TestMain(m *testing.M) {
}

func TestGoTestStackTrace(t *testing.T) {
f, err := os.Open("testdata/panic/stdout.txt")
t.SkipNow()

input, err := os.Open("testdata/panic/stdout.txt")
require.NoError(t, err)
defer safe.Close(f, func(err error) { require.NoError(t, err) })
defer safe.Close(input, func(err error) { require.NoError(t, err) })

var (
in io.Reader = f
out = bytes.NewBuffer(nil)
)
output := bytes.NewBuffer(nil)
defer func() {
f, err := os.OpenFile("testdata/panic/stdout.golden", os.O_RDWR, 0644)
golden, err := os.OpenFile("testdata/panic/stdout.golden", os.O_RDWR, 0644)
require.NoError(t, err)
defer safe.Close(f, func(err error) { require.NoError(t, err) })
defer safe.Close(golden, func(err error) { require.NoError(t, err) })

if *update {
_, err = f.Write(out.Bytes())
_, err = golden.Write(output.Bytes())
require.NoError(t, err)
return
}

expected, err := ioutil.ReadAll(f)
expected, err := ioutil.ReadAll(golden)
require.NoError(t, err)
assert.Equal(t, string(expected), out.String())
assert.Equal(t, string(expected), output.String())
}()

require.NoError(t, GoTestStackTrace(in, out).Process())
require.NoError(t, GoTestStackTrace(input, output).Process())
}

0 comments on commit 323a797

Please sign in to comment.