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

Hide empty packages in compact formats #283

Merged
merged 2 commits into from
Nov 26, 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
2 changes: 1 addition & 1 deletion cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *eventHandler) Close() error {
var _ testjson.EventHandler = &eventHandler{}

func newEventHandler(opts *options) (*eventHandler, error) {
formatter := testjson.NewEventFormatter(opts.stdout, opts.format)
formatter := testjson.NewEventFormatter(opts.stdout, opts.format, opts.formatOptions)
if formatter == nil {
return nil, fmt.Errorf("unknown format %s", opts.format)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (bufferCloser) Close() error { return nil }
func TestEventHandler_Event_WithMissingActionFail(t *testing.T) {
buf := new(bufferCloser)
errBuf := new(bytes.Buffer)
format := testjson.NewEventFormatter(errBuf, "testname")
format := testjson.NewEventFormatter(errBuf, "testname", testjson.FormatOptions{})

source := golden.Get(t, "../../testjson/testdata/input/go-test-json-missing-test-fail.out")
cfg := testjson.ScanConfig{
Expand All @@ -76,7 +76,7 @@ func TestEventHandler_Event_WithMissingActionFail(t *testing.T) {
}

func TestEventHandler_Event_MaxFails(t *testing.T) {
format := testjson.NewEventFormatter(ioutil.Discard, "testname")
format := testjson.NewEventFormatter(ioutil.Discard, "testname", testjson.FormatOptions{})

source := golden.Get(t, "../../testjson/testdata/input/go-test-json.out")
cfg := testjson.ScanConfig{
Expand Down
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func setupFlags(name string) (*pflag.FlagSet, *options) {
flags.StringVarP(&opts.format, "format", "f",
lookEnvWithDefault("GOTESTSUM_FORMAT", "short"),
"print format of test input")
flags.BoolVar(&opts.formatOptions.HideEmptyPackages, "format-hide-empty-pkg",
false, "do not print empty packages in compact formats")
flags.BoolVar(&opts.rawCommand, "raw-command", false,
"don't prepend 'go test -json' to the 'go test' command")
flags.BoolVar(&opts.ignoreNonJSONOutputLines, "ignore-non-json-output-lines", false,
Expand Down Expand Up @@ -146,6 +148,7 @@ func lookEnvWithDefault(key, defValue string) string {
type options struct {
args []string
format string
formatOptions testjson.FormatOptions
debug bool
rawCommand bool
ignoreNonJSONOutputLines bool
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ See https://pkg.go.dev/gotest.tools/gotestsum#section-readme for detailed docume
Flags:
--debug enabled debug logging
-f, --format string print format of test input (default "short")
--format-hide-empty-pkg do not print empty packages in compact formats
--hide-summary summary hide sections of the summary: skipped,failed,errors,output (default none)
--jsonfile string write all TestEvents to file
--junitfile string write a JUnit XML file
Expand Down
8 changes: 7 additions & 1 deletion testjson/dotformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type dotFormatter struct {
pkgs map[string]*dotLine
order []string
writer *dotwriter.Writer
opts FormatOptions
termWidth int
}

Expand All @@ -67,7 +68,7 @@ func (l *dotLine) checkWidth(prefix, terminal int) {
}
}

func newDotFormatter(out io.Writer) EventFormatter {
func newDotFormatter(out io.Writer, opts FormatOptions) EventFormatter {
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil || w == 0 {
log.Warnf("Failed to detect terminal width for dots format, error: %v", err)
Expand All @@ -77,6 +78,7 @@ func newDotFormatter(out io.Writer) EventFormatter {
pkgs: make(map[string]*dotLine),
writer: dotwriter.New(out),
termWidth: w,
opts: opts,
}
}

Expand All @@ -101,6 +103,10 @@ func (d *dotFormatter) Format(event TestEvent, exec *Execution) error {

sort.Slice(d.order, d.orderByLastUpdated)
for _, pkg := range d.order {
if d.opts.HideEmptyPackages && exec.Package(pkg).Total == 0 {
continue
}

line := d.pkgs[pkg]
pkgname := RelativePackagePath(pkg) + " "
prefix := fmtDotElapsed(exec.Package(pkg))
Expand Down
2 changes: 1 addition & 1 deletion testjson/dotformat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestFmtDotElapsed_RuneCountProperty(t *testing.T) {

func TestNewDotFormatter(t *testing.T) {
buf := new(bytes.Buffer)
ef := newDotFormatter(buf)
ef := newDotFormatter(buf, FormatOptions{})

d, ok := ef.(*dotFormatter)
skip.If(t, !ok, "no terminal width")
Expand Down
48 changes: 31 additions & 17 deletions testjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,16 @@ func all(cond ...bool) bool {
return true
}

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

func shortFormatPackageEvent(event TestEvent, exec *Execution) string {
func shortFormatPackageEvent(opts FormatOptions, event TestEvent, exec *Execution) string {
pkg := exec.Package(event.Package)

fmtEvent := func(action string) string {
Expand All @@ -144,9 +146,15 @@ func shortFormatPackageEvent(event TestEvent, exec *Execution) string {
withColor := colorEvent(event)
switch event.Action {
case ActionSkip:
if opts.HideEmptyPackages {
return ""
}
return fmtEvent(withColor("∅"))
case ActionPass:
if pkg.Total == 0 {
if opts.HideEmptyPackages {
return ""
}
return fmtEvent(withColor("∅"))
}
return fmtEvent(withColor("✓"))
Expand Down Expand Up @@ -181,16 +189,18 @@ func packageLine(event TestEvent, exec *Execution) string {
return buf.String()
}

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)
func pkgNameWithFailuresFormat(opts FormatOptions) func(event TestEvent, exec *Execution) string {
return func(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)
}
return ""
}
return ""
return shortFormatPackageEvent(opts, event, exec)
}
return shortFormatPackageEvent(event, exec)
}

func colorEvent(event TestEvent) func(format string, a ...interface{}) string {
Expand All @@ -211,8 +221,12 @@ type EventFormatter interface {
Format(event TestEvent, output *Execution) error
}

type FormatOptions struct {
HideEmptyPackages bool
}

// NewEventFormatter returns a formatter for printing events.
func NewEventFormatter(out io.Writer, format string) EventFormatter {
func NewEventFormatter(out io.Writer, format string, formatOpts FormatOptions) EventFormatter {
switch format {
case "debug":
return &formatAdapter{out, debugFormat}
Expand All @@ -223,13 +237,13 @@ func NewEventFormatter(out io.Writer, format string) EventFormatter {
case "dots", "dots-v1":
return &formatAdapter{out, dotsFormatV1}
case "dots-v2":
return newDotFormatter(out)
return newDotFormatter(out, formatOpts)
case "testname", "short-verbose":
return &formatAdapter{out, testNameFormat}
case "pkgname", "short":
return &formatAdapter{out, pkgNameFormat}
return &formatAdapter{out, pkgNameFormat(formatOpts)}
case "pkgname-and-test-fails", "short-with-failures":
return &formatAdapter{out, pkgNameWithFailuresFormat}
return &formatAdapter{out, pkgNameWithFailuresFormat(formatOpts)}
default:
return nil
}
Expand Down
11 changes: 8 additions & 3 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,14 @@ func TestFormats_DefaultGoTestJson(t *testing.T) {
},
{
name: "pkgname",
format: pkgNameFormat,
format: pkgNameFormat(FormatOptions{}),
expectedOut: "format/pkgname.out",
},
{
name: "pkgname",
format: pkgNameFormat(FormatOptions{HideEmptyPackages: true}),
expectedOut: "format/pkgname-hide-empty.out",
},
{
name: "standard-verbose",
format: standardVerboseFormat,
Expand Down Expand Up @@ -163,7 +168,7 @@ func TestFormats_Coverage(t *testing.T) {
},
{
name: "pkgname",
format: pkgNameFormat,
format: pkgNameFormat(FormatOptions{}),
expectedOut: "format/pkgname-coverage.out",
},
{
Expand Down Expand Up @@ -214,7 +219,7 @@ func TestFormats_Shuffle(t *testing.T) {
},
{
name: "pkgname",
format: pkgNameFormat,
format: pkgNameFormat(FormatOptions{}),
expectedOut: "format/pkgname-shuffle.out",
},
{
Expand Down
4 changes: 4 additions & 0 deletions testjson/testdata/format/pkgname-hide-empty.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
✖ testjson/internal/badmain (1ms)
✓ testjson/internal/good (cached)
✖ testjson/internal/parallelfails (20ms)
✖ testjson/internal/withfails (20ms)