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

Add format-icons flag for nerdfonts instead of unicode #366

Merged
merged 7 commits into from
Sep 22, 2023
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ The `--format` flag or `GOTESTSUM_FORMAT` environment variable set the format th
is used to print the test names, and possibly test output, as the tests run. Most
outputs use color to highlight pass, fail, or skip.

The `--format-hivis` flag changes the icons used by `pkgname` formats to higher
visiblity unicode characters.
The `--format-icons` flag changes the icons used by `pkgname` and `testdox` formats.
You can set the `GOTESTSUM_FORMAT_ICONS` environment variable, instead of the flag.
The nerdfonts icons requires a font from [Nerd Fonts](https://www.nerdfonts.com/).

Commonly used formats (see `--help` for a full list):

Expand Down
1 change: 1 addition & 0 deletions cmd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestPostRunHook(t *testing.T) {
}

env.Patch(t, "GOTESTSUM_FORMAT", "short")
env.Patch(t, "GOTESTSUM_FORMAT_ICONS", "default")

exec := newExecFromTestData(t)
err = postRunHook(opts, exec)
Expand Down
12 changes: 12 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func setupFlags(name string) (*pflag.FlagSet, *options) {
false, "do not print empty packages in compact formats")
flags.BoolVar(&opts.formatOptions.UseHiVisibilityIcons, "format-hivis",
false, "use high visibility characters in some formats")
_ = flags.MarkHidden("format-hivis")
flags.StringVar(&opts.formatOptions.Icons, "format-icons",
lookEnvWithDefault("GOTESTSUM_FORMAT_ICONS", ""),
"use different icons, see help for options")
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 @@ -145,6 +149,14 @@ Formats:
standard-quiet standard go test format
standard-verbose standard go test -v format

Format icons:
default the original unicode (✓, ∅, ✖)
hivis higher visibility unicode (✅, ➖, ❌)
text simple text characters (PASS, SKIP, FAIL)
codicons requires a font from https://www.nerdfonts.com/ (  )
octicons requires a font from https://www.nerdfonts.com/ (  )
emoticons requires a font from https://www.nerdfonts.com/ (󰇵 󰇶 󰇸)

Commands:
%[1]s tool slowest find or skip the slowest tests
%[1]s help print this help next
Expand Down
10 changes: 9 additions & 1 deletion cmd/testdata/gotestsum-help-text
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Flags:
--debug enabled debug logging
-f, --format string print format of test input (default "pkgname")
--format-hide-empty-pkg do not print empty packages in compact formats
--format-hivis use high visibility characters in some formats
--format-icons string use different icons, see help for options
--hide-summary summary hide sections of the summary: skipped,failed,errors,output (default none)
--jsonfile string write all TestEvents to file
--jsonfile-timing-events string write only the pass, skip, and fail TestEvents to the file
Expand Down Expand Up @@ -41,6 +41,14 @@ Formats:
standard-quiet standard go test format
standard-verbose standard go test -v format

Format icons:
default the original unicode (✓, ∅, ✖)
hivis higher visibility unicode (✅, ➖, ❌)
text simple text characters (PASS, SKIP, FAIL)
codicons requires a font from https://www.nerdfonts.com/ (  )
octicons requires a font from https://www.nerdfonts.com/ (  )
emoticons requires a font from https://www.nerdfonts.com/ (󰇵 󰇶 󰇸)

Commands:
gotestsum tool slowest find or skip the slowest tests
gotestsum help print this help next
1 change: 1 addition & 0 deletions cmd/testdata/post-run-hook-expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GOTESTSUM_ELAPSED=0.000s
GOTESTSUM_FORMAT=short
GOTESTSUM_FORMAT_ICONS=default
GOTESTSUM_JSONFILE=events.json
GOTESTSUM_JSONFILE_TIMING_EVENTS=timing.json
GOTESTSUM_JUNITFILE=junit.xml
Expand Down
107 changes: 78 additions & 29 deletions testjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ func testDoxFormat(out io.Writer, opts FormatOptions) EventFormatter {
Event TestEvent
Sentence string
}
getIcon := icon
if opts.UseHiVisibilityIcons {
getIcon = iconHiVis
}

getIcon := getIconFunc(opts)
results := map[string][]Result{}
return eventFormatterFunc(func(event TestEvent, exec *Execution) error {
switch {
Expand Down Expand Up @@ -244,40 +242,90 @@ func pkgNameFormat(out io.Writer, opts FormatOptions) eventFormatterFunc {
}
}

func icon(action Action) string {
switch action {
case ActionPass:
return color.GreenString("✓")
case ActionSkip:
return color.YellowString("∅")
case ActionFail:
return color.RedString("✖")
default:
return ""
type icons struct {
pass string
fail string
skip string
color bool
}

func (i icons) forAction(action Action) string {
if i.color {
switch action {
case ActionPass:
return color.GreenString(i.pass)
case ActionSkip:
return color.YellowString(i.skip)
case ActionFail:
return color.RedString(i.fail)
default:
return " "
}
} else {
switch action {
case ActionPass:
return i.pass
case ActionSkip:
return i.skip
case ActionFail:
return i.fail
default:
return " "
}
}
}

func iconHiVis(action Action) string {
switch action {
case ActionPass:
return "✅"
case ActionSkip:
return "➖"
case ActionFail:
return "❌"
func getIconFunc(opts FormatOptions) func(Action) string {
switch {
case opts.UseHiVisibilityIcons || opts.Icons == "hivis":
return icons{
pass: "✅", // WHITE HEAVY CHECK MARK
skip: "➖", // HEAVY MINUS SIGN
fail: "❌", // CROSS MARK
color: false,
}.forAction
case opts.Icons == "text":
return icons{
pass: "PASS",
skip: "SKIP",
fail: "FAIL",
color: true,
}.forAction
case opts.Icons == "codicons":
return icons{
pass: "\ueba4", // cod-pass
skip: "\ueabd", // cod-circle_slash
fail: "\uea87", // cod-error
color: true,
}.forAction
case opts.Icons == "octicons":
return icons{
pass: "\uf49e", // oct-check_circle
skip: "\uf517", // oct-skip
fail: "\uf52f", // oct-x_circle
color: true,
}.forAction
case opts.Icons == "emoticons":
return icons{
pass: "\U000f01f5", // md-emoticon_happy_outline
skip: "\U000f01f6", // md-emoticon_neutral_outline
fail: "\U000f01f8", // md-emoticon_sad_outline
color: true,
}.forAction
default:
return ""
return icons{
pass: "✓", // CHECK MARK
skip: "∅", // EMPTY SET
fail: "✖", // HEAVY MULTIPLICATION X
color: true,
}.forAction
}
}

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

getIcon := icon
if opts.UseHiVisibilityIcons {
getIcon = iconHiVis
}

getIcon := getIconFunc(opts)
fmtEvent := func(action string) string {
return action + " " + packageLine(event, exec.Package(event.Package))
}
Expand Down Expand Up @@ -367,7 +415,8 @@ func (e eventFormatterFunc) Format(event TestEvent, output *Execution) error {

type FormatOptions struct {
HideEmptyPackages bool
UseHiVisibilityIcons bool
UseHiVisibilityIcons bool // Deprecated
Icons string
}

// NewEventFormatter returns a formatter for printing events.
Expand Down
30 changes: 29 additions & 1 deletion testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,38 @@ func TestFormats_DefaultGoTestJson(t *testing.T) {
{
name: "pkgname with hivis",
format: func(out io.Writer) EventFormatter {
return pkgNameFormat(out, FormatOptions{UseHiVisibilityIcons: true})
return pkgNameFormat(out, FormatOptions{Icons: "hivis"})
},
expectedOut: "format/pkgname-hivis.out",
},
{
name: "pkgname with text",
format: func(out io.Writer) EventFormatter {
return pkgNameFormat(out, FormatOptions{Icons: "text"})
},
expectedOut: "format/pkgname-text.out",
},
{
name: "pkgname with codicons",
format: func(out io.Writer) EventFormatter {
return pkgNameFormat(out, FormatOptions{Icons: "codicons"})
},
expectedOut: "format/pkgname-codicons.out",
},
{
name: "pkgname with octicons",
format: func(out io.Writer) EventFormatter {
return pkgNameFormat(out, FormatOptions{Icons: "octicons"})
},
expectedOut: "format/pkgname-octicons.out",
},
{
name: "pkgname with emoticons",
format: func(out io.Writer) EventFormatter {
return pkgNameFormat(out, FormatOptions{Icons: "emoticons"})
},
expectedOut: "format/pkgname-emoticons.out",
},
{
name: "pkgname with hide-empty",
format: func(out io.Writer) EventFormatter {
Expand Down
5 changes: 5 additions & 0 deletions testjson/testdata/format/pkgname-codicons.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
 testjson/internal/badmain (1ms)
 testjson/internal/empty (cached)
 testjson/internal/good (cached)
 testjson/internal/parallelfails (20ms)
 testjson/internal/withfails (20ms)
5 changes: 5 additions & 0 deletions testjson/testdata/format/pkgname-emoticons.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
󰇸 testjson/internal/badmain (1ms)
󰇶 testjson/internal/empty (cached)
󰇵 testjson/internal/good (cached)
󰇸 testjson/internal/parallelfails (20ms)
󰇸 testjson/internal/withfails (20ms)
5 changes: 5 additions & 0 deletions testjson/testdata/format/pkgname-octicons.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
 testjson/internal/badmain (1ms)
 testjson/internal/empty (cached)
 testjson/internal/good (cached)
 testjson/internal/parallelfails (20ms)
 testjson/internal/withfails (20ms)
5 changes: 5 additions & 0 deletions testjson/testdata/format/pkgname-text.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FAIL testjson/internal/badmain (1ms)
SKIP testjson/internal/empty (cached)
PASS testjson/internal/good (cached)
FAIL testjson/internal/parallelfails (20ms)
FAIL testjson/internal/withfails (20ms)