Skip to content

Commit

Permalink
Add flag to show test status as a word next to the icons (#22)
Browse files Browse the repository at this point in the history
* 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.

* Style fix and replace tabs in .tmpl file

* Move status to duration bracket
  • Loading branch information
MartinPetkov authored Dec 31, 2021
1 parent a1f1d9f commit f5f1fee
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .gotestfmt/github/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ we are creating a stylized header for each package.
{{ "\033" }}[0;31m❌
{{- end -}}
{{ " " }}{{- .Name -}}
{{- "\033" -}}[0;37m ({{ .Duration -}}
{{- "\033" -}}[0;37m ({{if $settings.ShowTestStatus}}{{.Result}}; {{end}}{{ .Duration -}}
{{- with .Coverage -}}
, coverage: {{ . }}%
{{- end -}})
Expand All @@ -48,4 +48,4 @@ we are creating a stylized header for each package.
{{- end -}}
{{- end -}}
{{- "\n" -}}
{{- end -}}
{{- end -}}
8 changes: 4 additions & 4 deletions .gotestfmt/gitlab/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand All @@ -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 -}}
{{- end -}}
4 changes: 2 additions & 2 deletions .gotestfmt/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This template contains the format for an individual package.
{{ " \033" }}[0;31m❌
{{- end -}}
{{ " " }}{{- .Name -}}
{{- "\033" -}}[0;37m ({{ .Duration -}}
{{- "\033" -}}[0;37m ({{if $settings.ShowTestStatus}}{{.Result}}; {{end}}{{ .Duration -}}
{{- with .Coverage -}}
, coverage: {{ . }}%
{{- end -}})
Expand All @@ -44,4 +44,4 @@ This template contains the format for an individual package.
{{- end -}}
{{- end -}}
{{- "\n" -}}
{{- end -}}
{{- end -}}
6 changes: 5 additions & 1 deletion .gotestfmt/teamcity/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ 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 -}})
Expand All @@ -40,4 +44,4 @@ we are creating a stylized header for each package.
{{- end -}}
{{- end -}}
##teamcity[blockClosed name='{{ $packageTitle }}']{{- "\n" -}}
{{- end -}}
{{- end -}}
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 (PASS, FAIL, SKIP).

You can use the `-showteststatus` flag to output the words next to the icons.

### 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.
Expand All @@ -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

Expand All @@ -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.
This project is licensed under the [Unlicense](LICENSE.md), you are free to do with it as you please. It has no external dependencies.
10 changes: 10 additions & 0 deletions cmd/gotestfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func main() {
ci := ""
inputFile := "-"
hide := ""
var showTestStatus bool

flag.StringVar(
&ci,
"ci",
Expand All @@ -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 != "" {
Expand All @@ -134,6 +142,8 @@ func main() {
panic(err)
}

cfg.ShowTestStatus = showTestStatus

format, err := gotestfmt.New(
dirs,
)
Expand Down
2 changes: 2 additions & 0 deletions renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f5f1fee

Please sign in to comment.