From 5436f47710c17ff77863f78ad4e1ec47cf670d7b Mon Sep 17 00:00:00 2001 From: Martin Petkov Date: Wed, 29 Dec 2021 15:38:11 -0500 Subject: [PATCH 1/3] Add flag to show test status next to icons It may not be obvious what the icons represent, particularly the one for "SKIP". This adds a bit of documentation and a flag to show the status as a word next to the icon. --- .gotestfmt/github/package.gotpl | 8 ++++---- .gotestfmt/gitlab/package.gotpl | 8 ++++---- .gotestfmt/package.gotpl | 8 ++++---- .gotestfmt/teamcity/package.gotpl | 14 +++++++++----- README.md | 17 ++++++++++++----- cmd/gotestfmt/main.go | 10 ++++++++++ renderer/renderer.go | 2 ++ 7 files changed, 45 insertions(+), 22 deletions(-) diff --git a/.gotestfmt/github/package.gotpl b/.gotestfmt/github/package.gotpl index 37e65c6..1a7896d 100644 --- a/.gotestfmt/github/package.gotpl +++ b/.gotestfmt/github/package.gotpl @@ -24,11 +24,11 @@ we are creating a stylized header for each package. {{- if or (not $settings.HideSuccessfulTests) (ne .Result "PASS") -}} ::group:: {{- if eq .Result "PASS" -}} - {{ "\033" }}[0;32m✅ + {{ "\033" }}[0;32m✅{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- else if eq .Result "SKIP" -}} - {{ "\033" }}[0;33m🚧 + {{ "\033" }}[0;33m🚧{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- else -}} - {{ "\033" }}[0;31m❌ + {{ "\033" }}[0;31m❌{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- end -}} {{ " " }}{{- .Name -}} {{- "\033" -}}[0;37m ({{ .Duration -}} @@ -48,4 +48,4 @@ we are creating a stylized header for each package. {{- end -}} {{- end -}} {{- "\n" -}} -{{- end -}} \ No newline at end of file +{{- end -}} diff --git a/.gotestfmt/gitlab/package.gotpl b/.gotestfmt/gitlab/package.gotpl index b48dfc3..e46e74c 100644 --- a/.gotestfmt/gitlab/package.gotpl +++ b/.gotestfmt/gitlab/package.gotpl @@ -25,11 +25,11 @@ we are creating a stylized header for each package. {{- if or (not $settings.HideSuccessfulTests) (ne .Result "PASS") -}} {{- "\033[0K" }}section_start:{{ with .StartTime }}{{ .Unix }}{{ else }}0{{ end }}:{{ .ID }}[collapsed=true]{{- "\r\033[0K" -}} {{- if eq .Result "PASS" -}} - {{- "\033[0;32m " }}✅ + {{- "\033[0;32m " }}✅{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- else if eq .Result "SKIP" -}} - {{- "\033[0;33m " }}🚧 + {{- "\033[0;33m " }}🚧{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- else -}} - {{- "\033[0;31m " }}❌ + {{- "\033[0;31m " }}❌{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- end -}} {{- " " }}{{- .Name -}} {{- with .Coverage -}}{{- " \033" -}}[0;37m (coverage: {{ . }}%){{- end -}} @@ -46,4 +46,4 @@ we are creating a stylized header for each package. {{- end -}} {{- end -}} {{- "\033[0K" }}section_end:{{ with .EndTime }}{{ .Unix }}{{ else }}0{{end}}:{{ .ID }}{{ "\r\033[0K" }}{{- "\n" -}} -{{- end -}} \ No newline at end of file +{{- end -}} diff --git a/.gotestfmt/package.gotpl b/.gotestfmt/package.gotpl index e4631ee..c6fce73 100644 --- a/.gotestfmt/package.gotpl +++ b/.gotestfmt/package.gotpl @@ -22,11 +22,11 @@ This template contains the format for an individual package. {{- range . -}} {{- if or (not $settings.HideSuccessfulTests) (ne .Result "PASS") -}} {{- if eq .Result "PASS" -}} - {{ " \033" }}[0;32m✅ + {{ " \033" }}[0;32m✅{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- else if eq .Result "SKIP" -}} - {{ " \033" }}[0;33m🚧 + {{ " \033" }}[0;33m🚧{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- else -}} - {{ " \033" }}[0;31m❌ + {{ " \033" }}[0;31m❌{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} {{- end -}} {{ " " }}{{- .Name -}} {{- "\033" -}}[0;37m ({{ .Duration -}} @@ -44,4 +44,4 @@ This template contains the format for an individual package. {{- end -}} {{- end -}} {{- "\n" -}} -{{- end -}} \ No newline at end of file +{{- end -}} diff --git a/.gotestfmt/teamcity/package.gotpl b/.gotestfmt/teamcity/package.gotpl index 988e73d..6e9f693 100644 --- a/.gotestfmt/teamcity/package.gotpl +++ b/.gotestfmt/teamcity/package.gotpl @@ -21,15 +21,19 @@ we are creating a stylized header for each package. {{- $title = print ", coverage: " . "%" -}} {{- end -}}) {{- $title = print $title ")" -}} + {{- $icon = "" -}} {{- if eq .Result "PASS" -}} - {{- $title = print "✅ " $title -}} + {{- $icon = "✅" -}} {{- else if eq .Result "SKIP" -}} - {{- $title = print "🚧 " $title -}} + {{- $icon = "🚧" -}} {{- else -}} - {{- $title = print "❌ " $title -}} + {{- $icon = "❌" -}} {{- end -}} + {{- if $settings.ShowTestStatus -}} + {{- $icon = print $icon " (" .Result ")" -}} + {{- end -}} + {{- $title = print $icon " " $title -}} {{- "\n" -}} - ##teamcity[blockOpened name='{{- $title -}}']{{- "\n" -}} {{- with .Output -}} {{- . -}} @@ -40,4 +44,4 @@ we are creating a stylized header for each package. {{- end -}} {{- end -}} ##teamcity[blockClosed name='{{ $packageTitle }}']{{- "\n" -}} -{{- end -}} \ No newline at end of file +{{- end -}} diff --git a/README.md b/README.md index 0704e40..0c0881b 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ jobs: with: # Optional: pass GITHUB_TOKEN to avoid rate limiting. token: ${{ secrets.GITHUB_TOKEN }} - + # Alternatively, install using go install - name: Set up gotestfmt run: go install github.com/haveyoudebuggedit/gotestfmt/v2/cmd/gotestfmt@latest @@ -137,7 +137,7 @@ FROM golang COPY --from gotestfmt /gotestfmt /usr/local/bin/ ``` -You can then run the tests within this image with the following command: +You can then run the tests within this image with the following command: ```bash go test -json -v ./... | /usr/local/bin/gotestfmt @@ -269,6 +269,7 @@ Render settings are available in all templates. They have the following fields: | `.HideSuccessfulPackages` | `bool` | Hide all packages that have only successful tests from the output. | | `.HideEmptyPackages` | `bool` | Hide the packages from the output that have no test cases. | | `.HideSuccessfulTests` | `bool` | Hide all tests from the output that are successful. | +| `.ShowTestStatus` | `bool` | Show the test status next to the icons (PASS, FAIL, SKIP). | ## FAQ @@ -284,6 +285,12 @@ By default, `gotestfmt` will output all tests and their logs. However, you can u ⚠️ This feature depends on the template you use. If you customized your template please make sure to check the [Render settings](#render-settings) object in your code. +### How do I know what the icons mean in the output? + +The icons are based on the output of `go test -json`. They map to the values from the `[test2json](https://pkg.go.dev/cmd/test2json) package which are PASS, FAIL and SKIP. + +You can use the `-showteststatus` flag to output the words next to the icons in the output. + ### Can I use gotestfmt without `-json`? When running `go test` without `-json` the output does not properly contain the package names for each line. This is not a problem if you are running tests only on a single package, but lines become mixed up when running tests on multiple packages. Version 1 of `gotestfmt` supported the raw output, but version 2 dropped support for it because it results in a lot of unmaintainable code based on an undocumented format. @@ -309,9 +316,9 @@ There are more awesome tools out there: - [ContainerSSH](https://github.com/containerssh/libcontainerssh) - [go-ovirt-client](https://github.com/ovirt/go-ovirt-client) - [go-ovirt-client-log-klog](https://github.com/ovirt/go-ovirt-client-log-klog) -- [...and more!](https://github.com/search?q=gotestfmt&type=code) +- [...and more!](https://github.com/search?q=gotestfmt&type=code) -*To add your name here simply click the pen icon on top of this box. Please use alphabetic order.* +*To add your name here simply click the pen icon on top of this box. Please use alphabetic order.* ## Architecture @@ -331,4 +338,4 @@ If you wish to build `gotestfmt` for yourself you'll need at least Go 1.16. You ## License -This project is licensed under the [Unlicense](LICENSE.md), you are free to do with it as you please. It has no external dependencies. \ No newline at end of file +This project is licensed under the [Unlicense](LICENSE.md), you are free to do with it as you please. It has no external dependencies. diff --git a/cmd/gotestfmt/main.go b/cmd/gotestfmt/main.go index 2e1fe26..e3513d8 100644 --- a/cmd/gotestfmt/main.go +++ b/cmd/gotestfmt/main.go @@ -96,6 +96,8 @@ func main() { ci := "" inputFile := "-" hide := "" + var showTestStatus bool + flag.StringVar( &ci, "ci", @@ -114,6 +116,12 @@ func main() { hide, hideDescription(), ) + flag.BoolVar( + &showTestStatus, + "showteststatus", + showTestStatus, + "Show the test status next to the icons (PASS, FAIL, SKIP).", + ) flag.Parse() if ci != "" { @@ -134,6 +142,8 @@ func main() { panic(err) } + cfg.ShowTestStatus = showTestStatus + format, err := gotestfmt.New( dirs, ) diff --git a/renderer/renderer.go b/renderer/renderer.go index 734bc5b..833ef8b 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -116,4 +116,6 @@ type RenderSettings struct { HideEmptyPackages bool // HideSuccessfulTests hides all tests from the output that are successful. HideSuccessfulTests bool + // ShowTestStatus adds words to indicate the test status next to the icons (PASS, FAIl, SKIP). + ShowTestStatus bool } From c3145188147894e5f8fcb86e5da7442bd426da81 Mon Sep 17 00:00:00 2001 From: Martin Petkov Date: Wed, 29 Dec 2021 15:46:23 -0500 Subject: [PATCH 2/3] Style fix and replace tabs in .tmpl file --- .gotestfmt/teamcity/package.gotpl | 2 +- README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gotestfmt/teamcity/package.gotpl b/.gotestfmt/teamcity/package.gotpl index 6e9f693..b0377b1 100644 --- a/.gotestfmt/teamcity/package.gotpl +++ b/.gotestfmt/teamcity/package.gotpl @@ -32,7 +32,7 @@ we are creating a stylized header for each package. {{- if $settings.ShowTestStatus -}} {{- $icon = print $icon " (" .Result ")" -}} {{- end -}} - {{- $title = print $icon " " $title -}} + {{- $title = print $icon " " $title -}} {{- "\n" -}} ##teamcity[blockOpened name='{{- $title -}}']{{- "\n" -}} {{- with .Output -}} diff --git a/README.md b/README.md index 0c0881b..cdd8a28 100644 --- a/README.md +++ b/README.md @@ -287,9 +287,9 @@ By default, `gotestfmt` will output all tests and their logs. However, you can u ### How do I know what the icons mean in the output? -The icons are based on the output of `go test -json`. They map to the values from the `[test2json](https://pkg.go.dev/cmd/test2json) package which are PASS, FAIL and SKIP. +The icons are based on the output of `go test -json`. They map to the values from the [`test2json`](https://pkg.go.dev/cmd/test2json) package (PASS, FAIL, SKIP). -You can use the `-showteststatus` flag to output the words next to the icons in the output. +You can use the `-showteststatus` flag to output the words next to the icons. ### Can I use gotestfmt without `-json`? From 42f0df221f017201fe0fecd4865242fcfdb14aee Mon Sep 17 00:00:00 2001 From: Martin Petkov Date: Thu, 30 Dec 2021 22:52:17 -0500 Subject: [PATCH 3/3] Move status to duration bracket --- .gotestfmt/github/package.gotpl | 8 ++++---- .gotestfmt/package.gotpl | 8 ++++---- .gotestfmt/teamcity/package.gotpl | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.gotestfmt/github/package.gotpl b/.gotestfmt/github/package.gotpl index 1a7896d..37685e9 100644 --- a/.gotestfmt/github/package.gotpl +++ b/.gotestfmt/github/package.gotpl @@ -24,14 +24,14 @@ we are creating a stylized header for each package. {{- if or (not $settings.HideSuccessfulTests) (ne .Result "PASS") -}} ::group:: {{- if eq .Result "PASS" -}} - {{ "\033" }}[0;32m✅{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} + {{ "\033" }}[0;32m✅ {{- else if eq .Result "SKIP" -}} - {{ "\033" }}[0;33m🚧{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} + {{ "\033" }}[0;33m🚧 {{- else -}} - {{ "\033" }}[0;31m❌{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} + {{ "\033" }}[0;31m❌ {{- end -}} {{ " " }}{{- .Name -}} - {{- "\033" -}}[0;37m ({{ .Duration -}} + {{- "\033" -}}[0;37m ({{if $settings.ShowTestStatus}}{{.Result}}; {{end}}{{ .Duration -}} {{- with .Coverage -}} , coverage: {{ . }}% {{- end -}}) diff --git a/.gotestfmt/package.gotpl b/.gotestfmt/package.gotpl index c6fce73..40ab024 100644 --- a/.gotestfmt/package.gotpl +++ b/.gotestfmt/package.gotpl @@ -22,14 +22,14 @@ This template contains the format for an individual package. {{- range . -}} {{- if or (not $settings.HideSuccessfulTests) (ne .Result "PASS") -}} {{- if eq .Result "PASS" -}} - {{ " \033" }}[0;32m✅{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} + {{ " \033" }}[0;32m✅ {{- else if eq .Result "SKIP" -}} - {{ " \033" }}[0;33m🚧{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} + {{ " \033" }}[0;33m🚧 {{- else -}} - {{ " \033" }}[0;31m❌{{if $settings.ShowTestStatus}} ({{.Result}}){{end}} + {{ " \033" }}[0;31m❌ {{- end -}} {{ " " }}{{- .Name -}} - {{- "\033" -}}[0;37m ({{ .Duration -}} + {{- "\033" -}}[0;37m ({{if $settings.ShowTestStatus}}{{.Result}}; {{end}}{{ .Duration -}} {{- with .Coverage -}} , coverage: {{ . }}% {{- end -}}) diff --git a/.gotestfmt/teamcity/package.gotpl b/.gotestfmt/teamcity/package.gotpl index b0377b1..6f86b42 100644 --- a/.gotestfmt/teamcity/package.gotpl +++ b/.gotestfmt/teamcity/package.gotpl @@ -16,24 +16,24 @@ we are creating a stylized header for each package. {{- with .TestCases -}} {{- range . -}} {{- if or (not $settings.HideSuccessfulTests) (ne .Result "PASS") -}} + {{- if $settings.ShowTestStatus -}} + {{- $title := print .Name " (" .Result "; " .Duration -}} + {{- else -}} {{- $title := print .Name " (" .Duration -}} + {{- end -}} {{- with .Coverage -}} {{- $title = print ", coverage: " . "%" -}} {{- end -}}) {{- $title = print $title ")" -}} - {{- $icon = "" -}} {{- if eq .Result "PASS" -}} - {{- $icon = "✅" -}} + {{- $title = print "✅ " $title -}} {{- else if eq .Result "SKIP" -}} - {{- $icon = "🚧" -}} + {{- $title = print "🚧 " $title -}} {{- else -}} - {{- $icon = "❌" -}} - {{- end -}} - {{- if $settings.ShowTestStatus -}} - {{- $icon = print $icon " (" .Result ")" -}} + {{- $title = print "❌ " $title -}} {{- end -}} - {{- $title = print $icon " " $title -}} {{- "\n" -}} + ##teamcity[blockOpened name='{{- $title -}}']{{- "\n" -}} {{- with .Output -}} {{- . -}}