Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests, and cleanup formats #262

Merged
merged 11 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestPostRunHook(t *testing.T) {

func newExecFromTestData(t *testing.T) *testjson.Execution {
t.Helper()
f, err := os.Open("../testjson/testdata/go-test-json.out")
f, err := os.Open("../testjson/testdata/input/go-test-json.out")
assert.NilError(t, err)
defer f.Close() // nolint: errcheck

Expand All @@ -61,7 +61,7 @@ func TestEventHandler_Event_WithMissingActionFail(t *testing.T) {
errBuf := new(bytes.Buffer)
format := testjson.NewEventFormatter(errBuf, "testname")

source := golden.Get(t, "../../testjson/testdata/go-test-json-missing-test-fail.out")
source := golden.Get(t, "../../testjson/testdata/input/go-test-json-missing-test-fail.out")
cfg := testjson.ScanConfig{
Stdout: bytes.NewReader(source),
Handler: &eventHandler{jsonFile: buf, formatter: format},
Expand All @@ -78,7 +78,7 @@ func TestEventHandler_Event_WithMissingActionFail(t *testing.T) {
func TestEventHandler_Event_MaxFails(t *testing.T) {
format := testjson.NewEventFormatter(ioutil.Discard, "testname")

source := golden.Get(t, "../../testjson/testdata/go-test-json.out")
source := golden.Get(t, "../../testjson/testdata/input/go-test-json.out")
cfg := testjson.ScanConfig{
Stdout: bytes.NewReader(source),
Handler: &eventHandler{formatter: format, maxFails: 2},
Expand Down
6 changes: 3 additions & 3 deletions cmd/testdata/post-run-hook-expected
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ GOTESTSUM_FORMAT=short
GOTESTSUM_JSONFILE=events.json
GOTESTSUM_JUNITFILE=junit.xml
TESTS_ERRORS=0
TESTS_FAILED=5
TESTS_SKIPPED=4
TESTS_TOTAL=46
TESTS_FAILED=13
TESTS_SKIPPED=5
TESTS_TOTAL=59
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/fsnotify/fsnotify v1.4.9
github.com/google/go-cmp v0.5.5
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/jonboulle/clockwork v0.2.2
github.com/pkg/errors v0.9.1
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
Expand Down
2 changes: 1 addition & 1 deletion internal/junitxml/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func createExecution(t *testing.T) *testjson.Execution {
}

func readTestData(t *testing.T, stream string) io.Reader {
raw, err := ioutil.ReadFile("../../testjson/testdata/go-test-json." + stream)
raw, err := ioutil.ReadFile("../../testjson/testdata/input/go-test-json." + stream)
assert.NilError(t, err)
return bytes.NewReader(raw)
}
Expand Down
162 changes: 99 additions & 63 deletions internal/junitxml/testdata/junitxml-report.golden

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions testjson/dotformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
"gotest.tools/gotestsum/log"
)

func dotsFormatV1(event TestEvent, exec *Execution) (string, error) {
func dotsFormatV1(event TestEvent, exec *Execution) string {
pkg := exec.Package(event.Package)
switch {
case event.PackageEvent():
return "", nil
return ""
case event.Action == ActionRun && pkg.Total == 1:
return "[" + RelativePackagePath(event.Package) + "]", nil
return "[" + RelativePackagePath(event.Package) + "]"
}
return fmtDot(event), nil
return fmtDot(event)
}

func fmtDot(event TestEvent) string {
Expand Down
18 changes: 4 additions & 14 deletions testjson/dotformat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,19 @@ import (
func TestScanTestOutput_WithDotsFormatter(t *testing.T) {
skip.If(t, runtime.GOOS == "windows")

defer patchPkgPathPrefix("github.com/gotestyourself/gotestyourself")()

out := new(bytes.Buffer)
dotfmt := &dotFormatter{
pkgs: make(map[string]*dotLine),
writer: dotwriter.New(out),
termWidth: 80,
}
shim := newFakeHandler(dotfmt, "go-test-json")
exec, err := ScanTestOutput(shim.Config(t))
shim := newFakeHandler(dotfmt, "input/go-test-json")
_, err := ScanTestOutput(shim.Config(t))
assert.NilError(t, err)

actual := text.ProcessLines(t, out, text.OpRemoveSummaryLineElapsedTime)
golden.Assert(t, actual, outFile("dots-format"))
golden.Assert(t, shim.err.String(), "dots-format.err")
assert.DeepEqual(t, exec, expectedExecution, cmpExecutionShallow)
}

func outFile(name string) string {
if runtime.GOOS == "windows" {
return name + "-windows.out"
}
return name + ".out"
golden.Assert(t, actual, "format/dots-v2.out")
golden.Assert(t, shim.err.String(), "input/go-test-json.err")
}

func TestFmtDotElapsed(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sync"
"time"

"github.com/jonboulle/clockwork"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"gotest.tools/gotestsum/log"
Expand Down Expand Up @@ -470,11 +469,11 @@ func (e *Execution) Packages() []string {
return sortedKeys(e.packages)
}

var clock = clockwork.NewRealClock()
var timeNow = time.Now

// Elapsed returns the time elapsed since the execution started.
func (e *Execution) Elapsed() time.Duration {
return clock.Now().Sub(e.started)
return timeNow().Sub(e.started)
}

// Failed returns a list of all the failed test cases.
Expand Down Expand Up @@ -579,7 +578,7 @@ func (e *Execution) Started() time.Time {
// time the test execution started.
func newExecution() *Execution {
return &Execution{
started: clock.Now(),
started: timeNow(),
packages: make(map[string]*Package),
}
}
Expand Down
6 changes: 3 additions & 3 deletions testjson/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ var cmpPackage = cmp.Options{
}

func TestScanTestOutput_MinimalConfig(t *testing.T) {
in := bytes.NewReader(golden.Get(t, "go-test-json.out"))
in := bytes.NewReader(golden.Get(t, "input/go-test-json.out"))
exec, err := ScanTestOutput(ScanConfig{Stdout: in})
assert.NilError(t, err)
// a weak check to show that all the stdout was scanned
assert.Equal(t, exec.Total(), 46)
assert.Equal(t, exec.Total(), 59)
}

func TestScanTestOutput_CallsStopOnError(t *testing.T) {
Expand All @@ -50,7 +50,7 @@ func TestScanTestOutput_CallsStopOnError(t *testing.T) {
called = true
}
cfg := ScanConfig{
Stdout: bytes.NewReader(golden.Get(t, "go-test-json.out")),
Stdout: bytes.NewReader(golden.Get(t, "input/go-test-json.out")),
Handler: &handlerFails{},
Stop: stop,
}
Expand Down
57 changes: 27 additions & 30 deletions testjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ import (
"github.com/fatih/color"
)

func debugFormat(event TestEvent, _ *Execution) (string, error) {
func debugFormat(event TestEvent, _ *Execution) string {
return fmt.Sprintf("%s %s %s (%.3f) [%d] %s\n",
event.Package,
event.Test,
event.Action,
event.Elapsed,
event.Time.Unix(),
event.Output), nil
event.Output)
}

// go test -v
func standardVerboseFormat(event TestEvent, _ *Execution) (string, error) {
func standardVerboseFormat(event TestEvent, _ *Execution) string {
if event.Action == ActionOutput {
return event.Output, nil
return event.Output
}
return "", nil
return ""
}

// go test
func standardQuietFormat(event TestEvent, _ *Execution) (string, error) {
func standardQuietFormat(event TestEvent, _ *Execution) string {
if !event.PackageEvent() {
return "", nil
return ""
}
if event.Output == "PASS\n" || isCoverageOutput(event.Output) {
return "", nil
return ""
}
if isWarningNoTestsToRunOutput(event.Output) {
return "", nil
return ""
}

return event.Output, nil
return event.Output
}

func testNameFormat(event TestEvent, exec *Execution) (string, error) {
func testNameFormat(event TestEvent, exec *Execution) string {
result := colorEvent(event)(strings.ToUpper(string(event.Action)))
formatTest := func() string {
pkgPath := RelativePackagePath(event.Package)
Expand All @@ -55,29 +55,29 @@ func testNameFormat(event TestEvent, exec *Execution) (string, error) {

switch {
case isPkgFailureOutput(event):
return event.Output, nil
return event.Output

case event.PackageEvent():
if !event.Action.IsTerminal() {
return "", nil
return ""
}
pkg := exec.Package(event.Package)
if event.Action == ActionSkip || (event.Action == ActionPass && pkg.Total == 0) {
result = colorEvent(event)("EMPTY")
}

event.Elapsed = 0 // hide elapsed for now, for backwards compat
return result + " " + packageLine(event, exec), nil
return result + " " + packageLine(event, exec)

case event.Action == ActionFail:
pkg := exec.Package(event.Package)
tc := pkg.LastFailedByName(event.Test)
return pkg.Output(tc.ID) + formatTest(), nil
return pkg.Output(tc.ID) + formatTest()

case event.Action == ActionPass:
return formatTest(), nil
return formatTest()
}
return "", nil
return ""
}

// joinPkgToTestName for formatting.
Expand Down Expand Up @@ -128,11 +128,11 @@ func all(cond ...bool) bool {
return true
}

func pkgNameFormat(event TestEvent, exec *Execution) (string, error) {
func pkgNameFormat(event TestEvent, exec *Execution) string {
if !event.PackageEvent() {
return "", nil
return ""
}
return shortFormatPackageEvent(event, exec), nil
return shortFormatPackageEvent(event, exec)
}

func shortFormatPackageEvent(event TestEvent, exec *Execution) string {
Expand Down Expand Up @@ -181,16 +181,16 @@ func packageLine(event TestEvent, exec *Execution) string {
return buf.String()
}

func pkgNameWithFailuresFormat(event TestEvent, exec *Execution) (string, error) {
func pkgNameWithFailuresFormat(event TestEvent, exec *Execution) string {
if !event.PackageEvent() {
if event.Action == ActionFail {
pkg := exec.Package(event.Package)
tc := pkg.LastFailedByName(event.Test)
return pkg.Output(tc.ID), nil
return pkg.Output(tc.ID)
}
return "", nil
return ""
}
return shortFormatPackageEvent(event, exec), nil
return shortFormatPackageEvent(event, exec)
}

func colorEvent(event TestEvent) func(format string, a ...interface{}) string {
Expand Down Expand Up @@ -237,14 +237,11 @@ func NewEventFormatter(out io.Writer, format string) EventFormatter {

type formatAdapter struct {
out io.Writer
format func(TestEvent, *Execution) (string, error)
format func(TestEvent, *Execution) string
}

func (f *formatAdapter) Format(event TestEvent, exec *Execution) error {
o, err := f.format(event, exec)
if err != nil {
return err
}
_, err = f.out.Write([]byte(o))
o := f.format(event, exec)
_, err := f.out.Write([]byte(o))
return err
}
Loading