Skip to content

Commit

Permalink
Fixes #23: Code coverage not shown with default templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic committed Dec 30, 2021
1 parent a1f1d9f commit f4b6395
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 57 deletions.
6 changes: 5 additions & 1 deletion .gotestfmt/github/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ we are creating a stylized header for each package.
{{- else -}}
{{ "\033" }}[0;31m
{{- end -}}
📦 {{ .Name }}{{ "\033" }}[0m{{- "\n" -}}
📦 {{ .Name }}{{- "\033" }}[0m
{{- with .Coverage -}}
{{- "\033" -}}[0;37m ({{ . }}% coverage){{- "\033" -}}[0m
{{- end -}}
{{- "\n" -}}
{{- with .Reason -}}
{{- " " -}}🛑 {{ . -}}{{- "\n" -}}
{{- end -}}
Expand Down
6 changes: 5 additions & 1 deletion .gotestfmt/gitlab/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ we are creating a stylized header for each package.
{{- else -}}
{{- "\033" }}[0;31m
{{- end -}}
📦 {{ .Name }}{{ "\033" }}[0m{{- "\n" -}}
📦 {{ .Name }}{{- "\033" }}[0m
{{- with .Coverage -}}
{{- "\033" -}}[0;37m ({{ . }}% coverage){{- "\033" -}}[0m
{{- end -}}
{{- "\n" -}}
{{- with .Reason -}}
{{- " " -}}🛑 {{ . -}}{{- "\n" -}}
{{- end -}}
Expand Down
14 changes: 6 additions & 8 deletions .gotestfmt/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ This template contains the format for an individual package.
{{- else -}}
{{ "\033" }}[0;31m
{{- end -}}
📦 {{ .Name }}{{ "\033" }}[0m{{- "\n" -}}
📦 {{ .Name -}}{{- "\033" }}[0m
{{- with .Coverage -}}
{{- "\033" -}}[0;37m ({{ . }}% coverage){{- "\033" -}}[0m
{{- end -}}
{{- "\n" -}}
{{- with .Reason -}}
{{- " " -}}🛑 {{ . -}}{{- "\n" -}}
{{- end -}}
Expand All @@ -29,13 +33,7 @@ This template contains the format for an individual package.
{{ " \033" }}[0;31m❌
{{- end -}}
{{ " " }}{{- .Name -}}
{{- "\033" -}}[0;37m ({{ .Duration -}}
{{- with .Coverage -}}
, coverage: {{ . }}%
{{- end -}})
{{- "\033" -}}[0m
{{- "\n" -}}

{{- "\033" -}}[0;37m ({{ .Duration -}}){{- "\033" -}}[0m{{- "\n" -}}
{{- with .Output -}}
{{- . -}}
{{- "\n" -}}
Expand Down
2 changes: 1 addition & 1 deletion .gotestfmt/teamcity/downloads.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ This template contains the format for a package download.
{{- end -}}
{{- end -}}
{{- end -}}
##teamcity[blockClosed name='{{ $$title }}']
##teamcity[blockClosed name='{{ $title }}']
{{- end -}}
{{- end -}}
10 changes: 3 additions & 7 deletions .gotestfmt/teamcity/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ we are creating a stylized header for each package.
*/ -}}
{{- $settings := .Settings -}}
{{- if and (or (not $settings.HideSuccessfulPackages) (ne .Result "PASS")) (or (not $settings.HideEmptyPackages) (ne .Result "SKIP") (ne (len .TestCases) 0)) -}}
{{- $packageTitle := print "📦 " .Name -}}
##teamcity[blockOpened name='{{ $packageTitle }}']{{- "\n" -}}
##teamcity[blockOpened name='📦 {{ .Name }}{{- with .Coverage }} ({{ . }}% coverage){{- end -}}']
{{- with .Reason -}}
{{- " " -}}🛑 {{ . -}}{{- "\n" -}}
{{- end -}}
Expand All @@ -16,10 +15,7 @@ we are creating a stylized header for each package.
{{- with .TestCases -}}
{{- range . -}}
{{- if or (not $settings.HideSuccessfulTests) (ne .Result "PASS") -}}
{{- $title := print .Name " (" .Duration -}}
{{- with .Coverage -}}
{{- $title = print ", coverage: " . "%" -}}
{{- end -}})
{{- $title := print .Name " (" .Duration ")" -}}
{{- $title = print $title ")" -}}
{{- if eq .Result "PASS" -}}
{{- $title = print "✅ " $title -}}
Expand All @@ -39,5 +35,5 @@ we are creating a stylized header for each package.
{{- end -}}
{{- end -}}
{{- end -}}
##teamcity[blockClosed name='{{ $packageTitle }}']{{- "\n" -}}
##teamcity[blockClosed name='📦 {{ .Name }}{{- with .Coverage }} ({{ . }}% coverage){{- end -}}']{{- "\n" -}}
{{- end -}}
77 changes: 38 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,62 +213,61 @@ When running on a well-known CI system, such as GitHub Actions, gotestfmt will d

This file contains the output fragment showing the package downloads in the Go template format. It has the following variables available:

| Variable | Type | Description |
|----------|------|-------------|
| `.Failed` | `bool` | Indicates an overall failure. |
| `.Packages` | `[]Package` | A list of packages that have been processed. |
| `.StartTime` | `*time.Time` | The time the first download line was seen. May be empty. |
| `.EndTime` | `*time.Time` | The time the last download line was seen. May be empty. |
| `.Settings` | [`RenderSettings`](#render-settings) | The render settings (what to hide, etc, [see below](#render-settings)). |
| Variable | Type | Description |
|--------------|--------------------------------------|-------------------------------------------------------------------------|
| `.Failed` | `bool` | Indicates an overall failure. |
| `.Packages` | `[]Package` | A list of packages that have been processed. |
| `.StartTime` | `*time.Time` | The time the first download line was seen. May be empty. |
| `.EndTime` | `*time.Time` | The time the last download line was seen. May be empty. |
| `.Settings` | [`RenderSettings`](#render-settings) | The render settings (what to hide, etc, [see below](#render-settings)). |

The `Package` items have the following format:

| Variable | Type | Description |
|----------|------|-------------|
| Variable | Type | Description |
|------------|----------|----------------------------------------------------------------------|
| `.Package` | `string` | Name of the package. (e.g. `github.com/haveyoudebuggedit/gotestfmt`) |
| `.Version` | `string` | Version of the package. (e.g. `v1.0.0`) |
| `.Failed` | `bool` | If the package download has failed. |
| `.Reason` | `string` | Text explaining the failure. |
| `.Version` | `string` | Version of the package. (e.g. `v1.0.0`) |
| `.Failed` | `bool` | If the package download has failed. |
| `.Reason` | `string` | Text explaining the failure. |

#### package.tpl

This template is the output format for the results of a single package and the tests in it. If multiple packages are tested, this template is called multiple times in a row. It has the following fields:

| Variable | Type | Description |
|----------|------|-------------|
| `.Name` | `string` | Name of the package under test.
| `.Result` | `string` | Result of all tests in this package. Can be `PASS`, `FAIL`, or `SKIP`. |
| `.Duration` | `time.Duration` | Duration of all test runs in this package. |
| `.Coverage` | `*float64` | If coverage data was provided, this indicates the code coverage percentage. |
| `.Output` | `string` | Additional output from failures. (e.g. syntax error indications) |
| `.TestCases` | `[]TestCase` | A list of test case results. |
| `.Reason` | `string` | Text explaining the failure. Empty in most cases. |
| `.StartTime` | `*time.Time` | A pointer to a time object when the package was first seen in the output. May be nil. |
| `.EndTime` | `*time.Time` | A pointer to the time object when the package was last seen in the output. May be nil. |
| `.Settings` | [`RenderSettings`](#render-settings) | The render settings (what to hide, etc, [see below](#render-settings)). |
| Variable | Type | Description |
|--------------|--------------------------------------|----------------------------------------------------------------------------------------|
| `.Name` | `string` | Name of the package under test. |
| `.Result` | `string` | Result of all tests in this package. Can be `PASS`, `FAIL`, or `SKIP`. |
| `.Duration` | `time.Duration` | Duration of all test runs in this package. |
| `.Coverage` | `*float64` | If coverage data was provided, this indicates the code coverage percentage. |
| `.Output` | `string` | Additional output from failures. (e.g. syntax error indications) |
| `.TestCases` | `[]TestCase` | A list of test case results. |
| `.Reason` | `string` | Text explaining the failure. Empty in most cases. |
| `.StartTime` | `*time.Time` | A pointer to a time object when the package was first seen in the output. May be nil. |
| `.EndTime` | `*time.Time` | A pointer to the time object when the package was last seen in the output. May be nil. |
| `.Settings` | [`RenderSettings`](#render-settings) | The render settings (what to hide, etc, [see below](#render-settings)). |

Test cases have the following format:

| Variable | Type | Description |
|----------|------|-------------|
| `.Name` | `string` | Name of the test case. May contain slashes (`/`) if subtests are run. |
| `.Result` | `string` | Result of the test. Can be `PASS`, `FAIL`, or `SKIP`. |
| `.Duration` | `time.Duration` | Duration of all test runs in this package. |
| `.Coverage` | `float64` | If coverage data was provided, this indicates the code coverage percentage. Contains a negative number if no coverage data is available. |
| `.Output` | `string` | Log output from the test. |
| `.StartTime` | `*time.Time` | A pointer to a time object when the test case was first seen in the output. May be nil. |
| `.EndTime` | `*time.Time` | A pointer to the time object when the test case was last seen in the output. May be nil. |
| Variable | Type | Description |
|--------------|-----------------|------------------------------------------------------------------------------------------|
| `.Name` | `string` | Name of the test case. May contain slashes (`/`) if subtests are run. |
| `.Result` | `string` | Result of the test. Can be `PASS`, `FAIL`, or `SKIP`. |
| `.Duration` | `time.Duration` | Duration of all test runs in this package. |
| `.Output` | `string` | Log output from the test. |
| `.StartTime` | `*time.Time` | A pointer to a time object when the test case was first seen in the output. May be nil. |
| `.EndTime` | `*time.Time` | A pointer to the time object when the test case was last seen in the output. May be nil. |

#### Render settings

Render settings are available in all templates. They have the following fields:

| Variable | Type | Description |
|----------|------|-------------|
| `.HideSuccessfulDownloads` | `bool` | Hide successful package downloads from the output. |
| `.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. |
| Variable | Type | Description |
|----------------------------|--------|--------------------------------------------------------------------|
| `.HideSuccessfulDownloads` | `bool` | Hide successful package downloads from the output. |
| `.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. |

## FAQ

Expand Down
2 changes: 2 additions & 0 deletions parser/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type TestCase struct {
Duration time.Duration
// Coverage is the percentage of code coverage in this test case, or a negative number if no coverage data is
// present.
//
// Deprecated: Coverage is not reported per testcase and should not be used.
Coverage *float64
// Output is the log output of this test case.
Output string
Expand Down

0 comments on commit f4b6395

Please sign in to comment.