Skip to content

Commit

Permalink
Fixes #8: Support for JSON test output (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic authored Dec 5, 2021
1 parent 4a8fab7 commit 6c886cf
Show file tree
Hide file tree
Showing 51 changed files with 1,049 additions and 721 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
- checkout
- run:
name: Self-test
command: go test ./... 2>&1 | go run cmd/gotestfmt/main.go
command: go test -json ./... 2>&1 | go run cmd/gotestfmt/main.go
- run:
name: Self-test (verbose)
command: go test -v ./... 2>&1 | tee /tmp/gotest.log | go run cmd/gotestfmt/main.go
command: go test -json -v ./... 2>&1 | tee /tmp/gotest.log | go run cmd/gotestfmt/main.go
- store_artifacts:
path: /tmp/gotest.log
destination: gotest.log
Expand All @@ -23,7 +23,7 @@ jobs:
version: 19.03.13
- run:
name: Container test
command: go test -v ./... 2>&1 | docker run -i ghcr.io/haveyoudebuggedit/gotestfmt:latest
command: go test -json -v ./... 2>&1 | docker run -i ghcr.io/haveyoudebuggedit/gotestfmt:latest
workflows:
version: 2
build-workflow:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ jobs:
- name: Run self-test
run: |
set -euo pipefail
go test ./... 2>&1 | tee /tmp/gotest.log | go run ./cmd/gotestfmt
go test -json ./... 2>&1 | tee /tmp/gotest.log | go run ./cmd/gotestfmt
- name: Run self-test (verbose)
run: |
set -euo pipefail
go test -v ./... 2>&1 | tee /tmp/gotest-verbose.log | go run ./cmd/gotestfmt
go test -json -v ./... 2>&1 | tee /tmp/gotest-verbose.log | go run ./cmd/gotestfmt
- name: Upload test log
uses: actions/upload-artifact@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ docker-build:
--name gotestfmt-$$ \
-e GITLAB_CI=${GITLAB_CI} \
gotestfmt \
/bin/sh -c "cd /source; go test -v ./... 2>&1 | tee /tmp/gotest.log | go run cmd/gotestfmt/main.go"
/bin/sh -c "cd /source; go test -json -v ./... 2>&1 | tee /tmp/gotest.log | go run cmd/gotestfmt/main.go"
docker cp gotestfmt-$$:/tmp/gotest.log gotest.log
docker rm --force gotestfmt-$$
artifacts:
Expand Down
1 change: 0 additions & 1 deletion .gotestfmt/package.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ This template contains the format for an individual package.
{{- "\n" -}}
{{- end -}}

{{- "\n" -}}
{{- end -}}
{{- end -}}
{{- "\n" -}}
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ Then this is the tool for you. Run it locally, or in any CI system with the foll

```bash
set -euo pipefail
go test -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
```

Tadam, your tests will now show up in a beautifully formatted fashion. Plug it into your CI, and you're done.

**Note:** Please always save the original log. You will need it if you have to file a bug report for gotestfmt.

**⚠️ With version 2.0 gotestfmt switched to supporting only JSON output. Please add the `-json` flag to your `go test` command line!**

## Setting it up in your CI system

We have support for several CI systems, and you can also customize the output to match your system. Gotestfmt detects the CI system based on environment variables. If it can't detect the CI system it will try to create a generic colored test output. You can force the CI output with the `-ci github|gitlab|...` option.
Expand Down Expand Up @@ -55,7 +57,7 @@ jobs:
- name: Run tests
run: |
set -euo pipefail
go test -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
# Upload the original go test log as an artifact for later review.
- name: Upload test log
Expand Down Expand Up @@ -88,7 +90,7 @@ COPY --from gotestfmt /gotestfmt /usr/local/bin/
You can then run the tests within this image with the following command:

```bash
go test -v ./... | /usr/local/bin/gotestfmt
go test -json -v ./... | /usr/local/bin/gotestfmt
```

To put it all together, you can use the following `.gitlab-ci.yaml`:
Expand All @@ -109,7 +111,7 @@ docker-build:
-v /tmp:/tmp |
-e GITLAB_CI=${GITLAB_CI} \
gotestfmt \
/bin/sh -c "cd /source; go test -v ./... 2>&1 | tee /tmp/gotest.log | /usr/local/bin/gotestfmt"
/bin/sh -c "cd /source; go test -json -v ./... 2>&1 | tee /tmp/gotest.log | /usr/local/bin/gotestfmt"
artifacts:
paths:
- /tmp/gotest.log
Expand Down Expand Up @@ -140,7 +142,7 @@ jobs:
version: 19.03.13
- run:
name: Run tests
command: go test -v ./... 2>&1 | tee /tmp/gotest.log | docker run -i ghcr.io/haveyoudebuggedit/gotestfmt:latest
command: go test -json -v ./... 2>&1 | tee /tmp/gotest.log | docker run -i ghcr.io/haveyoudebuggedit/gotestfmt:latest
- store_artifacts:
path: /tmp/gotest.log
destination: gotest.log
Expand Down
5 changes: 5 additions & 0 deletions _testsource/coverage/code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example

func Hello() string {
return "Hello world!"
}
13 changes: 13 additions & 0 deletions _testsource/coverage/code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package example_test

import (
"testing"

"github.com/haveyoudebuggedit/example"
)

func TestHello(t *testing.T) {
if example.Hello() != "Hello world!" {
t.Fail()
}
}
4 changes: 4 additions & 0 deletions _testsource/coverage/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module github.com/haveyoudebuggedit/example

go 1.16

22 changes: 21 additions & 1 deletion cmd/gotestfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ func main() {
"./.gotestfmt",
}
ci := ""
inputFile := "-"
flag.StringVar(
&ci,
"ci",
ci,
"Which subdirectory to use within the .gotestfmt folder. Defaults to detecting the CI from environment variables.",
)
flag.StringVar(
&inputFile,
"input",
inputFile,
"Read build log from file. Defaults to standard input.",
)
flag.Parse()

if ci != "" {
Expand All @@ -57,5 +64,18 @@ func main() {
if err != nil {
panic(err)
}
format.Format(os.Stdin, os.Stdout)

input := os.Stdin
if inputFile != "-" {
fh, err := os.Open(inputFile)
if err != nil {
panic(err)
}
defer func() {
_ = fh.Close()
}()
input = fh
}

format.Format(input, os.Stdout)
}
10 changes: 8 additions & 2 deletions parser/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type TestCase struct {
Coverage *float64
// Output is the log output of this test case.
Output string
// Cached indicates that the test results are cached and the tests have not actually been run.
Cached bool
}

// ID returns the Name of the test case without slashes
Expand Down Expand Up @@ -62,8 +64,12 @@ type Package struct {
Output string
// TestCases is a list of test cases run in this package. Subtests are included as separate test cases.
TestCases []*TestCase
// TestCasesByName holds the test cases mapped by name.
TestCasesByName map[string]*TestCase
// Reason is a description of why the Result happened. Empty in most cases.
Reason string
// Cached indicates that the results came from the go test cache.
Cached bool
}

func (p *Package) EndTime() *time.Time {
Expand Down Expand Up @@ -95,7 +101,7 @@ type Download struct {

// Downloads is the context for TemplatePackageDownloads.
type Downloads struct {
// Packages is a list of packages
// Packages is a list of packagesByName
Packages []*Download `json:"packages"`
// Failed indicates that one or more package downloads failed.
Failed bool `json:"failed"`
Expand All @@ -105,7 +111,7 @@ type Downloads struct {
EndTime *time.Time `json:"-"`
}

// ParseResult is an overall structure for parser results, containing the prefix text, downloads and packages.
// ParseResult is an overall structure for parser results, containing the prefix text, downloads and packagesByName.
type ParseResult struct {
Prefix []string `json:"prefix"`
Downloads Downloads `json:"downloads"`
Expand Down
Loading

0 comments on commit 6c886cf

Please sign in to comment.