Skip to content

ci: Print go test stats #6855

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

Merged
merged 12 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add golden test for ci-report
  • Loading branch information
mafredri committed Mar 29, 2023
commit d031855c329205b79964d938e1fe808d8e2875bf
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ site/e2e/states/*.json
site/playwright-report/*
site/.swc

# Make target for updating golden files.
cli/testdata/.gen-golden
helm/tests/testdata/.gen-golden
# Make target for updating golden files (any dir).
.gen-golden

# Build
/build/
Expand Down
5 changes: 2 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ site/e2e/states/*.json
site/playwright-report/*
site/.swc

# Make target for updating golden files.
cli/testdata/.gen-golden
helm/tests/testdata/.gen-golden
# Make target for updating golden files (any dir).
.gen-golden

# Build
/build/
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ coderd/apidoc/swagger.json: $(shell find ./scripts/apidocgen $(FIND_EXCLUSIONS)
./scripts/apidocgen/generate.sh
yarn run --cwd=site format:write:only ../docs/api ../docs/manifest.json ../coderd/apidoc/swagger.json

update-golden-files: cli/testdata/.gen-golden helm/tests/testdata/.gen-golden
update-golden-files: cli/testdata/.gen-golden helm/tests/testdata/.gen-golden scripts/ci-report/testdata/.gen-golden
.PHONY: update-golden-files

cli/testdata/.gen-golden: $(wildcard cli/testdata/*.golden) $(wildcard cli/*.tpl) $(GO_SRC_FILES)
Expand All @@ -525,6 +525,10 @@ helm/tests/testdata/.gen-golden: $(wildcard helm/tests/testdata/*.golden) $(GO_S
go test ./helm/tests -run=TestUpdateGoldenFiles -update
touch "$@"

scripts/ci-report/testdata/.gen-golden: $(wildcard scripts/ci-report/testdata/*) $(wildcard scripts/ci-report/*.go)
go test ./scripts/ci-report -run=TestOutputMatchesGoldenFile -update
touch "$@"

# Generate a prettierrc for the site package that uses relative paths for
# overrides. This allows us to share the same prettier config between the
# site and the root of the repo.
Expand Down
46 changes: 36 additions & 10 deletions scripts/ci-report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"golang.org/x/exp/slices"
"golang.org/x/xerrors"
)

func main() {
Expand All @@ -19,11 +20,30 @@ func main() {
}
name := os.Args[1]

f, err := os.Open(name)
goTests, err := parseGoTestJSON(name)
if err != nil {
_, _ = fmt.Printf("error parsing gotestsum report: %v", err)
os.Exit(1)
}

rep, err := parseCIReport(goTests)
if err != nil {
_, _ = fmt.Printf("error opening gotestsum json file: %v", err)
_, _ = fmt.Printf("error parsing ci report: %v", err)
os.Exit(1)
}

err = printCIReport(os.Stdout, rep)
if err != nil {
_, _ = fmt.Printf("error printing report: %v", err)
os.Exit(1)
}
}

func parseGoTestJSON(name string) (GotestsumReport, error) {
f, err := os.Open(name)
if err != nil {
return GotestsumReport{}, xerrors.Errorf("error opening gotestsum json file: %w", err)
}
defer f.Close()

dec := json.NewDecoder(f)
Expand All @@ -35,13 +55,16 @@ func main() {
break
}
if err != nil {
_, _ = fmt.Printf("error decoding json: %v", err)
os.Exit(1)
return GotestsumReport{}, xerrors.Errorf("error decoding json: %w", err)
}
e.Package = strings.TrimPrefix(e.Package, "github.com/coder/coder/")
report = append(report, e)
}

return report, nil
}

func parseCIReport(report GotestsumReport) (CIReport, error) {
packagesSortedByName := []string{}
packageTimes := map[string]float64{}
packageFail := map[string]int{}
Expand Down Expand Up @@ -97,8 +120,7 @@ func main() {
case Pause:

default:
_, _ = fmt.Printf("unknown action: %v in entry %d (%v)", e.Action, i, e)
os.Exit(1)
return CIReport{}, xerrors.Errorf("unknown action: %v in entry %d (%v)", e.Action, i, e)
}
}

Expand Down Expand Up @@ -132,13 +154,17 @@ func main() {
})
}

enc := json.NewEncoder(os.Stdout)
return rep, nil
}

func printCIReport(dst io.Writer, rep CIReport) error {
enc := json.NewEncoder(dst)
enc.SetIndent("", " ")
err = enc.Encode(rep)
err := enc.Encode(rep)
if err != nil {
_, _ = fmt.Printf("error encoding json: %v", err)
os.Exit(1)
return xerrors.Errorf("error encoding json: %w", err)
}
return nil
}

type CIReport struct {
Expand Down
61 changes: 61 additions & 0 deletions scripts/ci-report/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"bytes"
"flag"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

// To update the golden files:
// make update-golden-files
var updateGoldenFiles = flag.Bool("update", false, "update .golden files")

func TestOutputMatchesGoldenFile(t *testing.T) {
t.Parallel()

goTests, err := parseGoTestJSON(filepath.Join("testdata", "gotests.json"))
if err != nil {
t.Fatalf("error parsing gotestsum report: %v", err)
}

rep, err := parseCIReport(goTests)
if err != nil {
t.Fatalf("error parsing ci report: %v", err)
}

var b bytes.Buffer
err = printCIReport(&b, rep)
if err != nil {
t.Fatalf("error printing report: %v", err)
}

goldenFile := filepath.Join("testdata", "ci-report.golden")
got := b.Bytes()
if updateGoldenFile(t, goldenFile, got) {
return
}

want := readGoldenFile(t, goldenFile)
require.Equal(t, string(want), string(got))
}

func readGoldenFile(t *testing.T, name string) []byte {
t.Helper()
b, err := os.ReadFile(name)
require.NoError(t, err, "error reading golden file")
return b
}

func updateGoldenFile(t *testing.T, name string, content []byte) bool {
t.Helper()
if *updateGoldenFiles {
err := os.WriteFile(name, content, 0o644)
require.NoError(t, err, "error updating golden file")
return true
}
return false
}
Loading