From 1a8ea1bee9051ffd21632b4dc4a0b1be5551660d Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Wed, 2 Dec 2026 21:49:35 -0500 Subject: [PATCH] ci: print how many Go and Android tests ran (#8372) The web lane's Vitest summary already says how many tests ran; the go, integration and android lanes said nothing, so a before-and-after had to count tests from source. - ci/testcount reads `go test -json` and prints what plain go test would (one line per package, the whole output of a failed test), then the pass/fail/skip counts for tests and subtests. Both Go lanes pipe through it and exit with go test's own status, kept in a file because the shell has no pipefail: the reader never decides the gate. - The android lane sums Gradle's JUnit reports after the unit tests, failure or not. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/release.yml | 33 +++++++- ci/testcount/main.go | 152 +++++++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 ci/testcount/main.go diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 00632083..1bb6b67e 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -166,8 +166,14 @@ jobs: - name: golangci-lint run: golangci-lint run ./... + # ci/testcount prints what plain go test would, plus how many tests + # ran. The step exits with go test's own status, kept aside in a file + # because the shell has no pipefail. - name: go test (short, race) - run: go test -short -race ./... + run: | + go build -o /tmp/testcount ./ci/testcount + { go test -short -race -json ./...; echo $? > /tmp/go-test-rc; } | /tmp/testcount + exit "$(cat /tmp/go-test-rc)" # Full `go test -race` against an ephemeral Postgres. # @@ -303,7 +309,11 @@ jobs: PKGS=$(sh ci/db-test-packages.sh) echo "database test packages: $(echo "$PKGS" | wc -l)" go test -race -run '^$' $PKGS - go test -p 1 -race -timeout 20m $PKGS + # ci/testcount prints the usual go test output plus how many tests + # ran; the step exits with go test's own status (no pipefail). + go build -o /tmp/testcount ./ci/testcount + { go test -p 1 -race -timeout 20m -json $PKGS; echo $? > /tmp/go-test-rc; } | /tmp/testcount + exit "$(cat /tmp/go-test-rc)" web: runs-on: go-ci @@ -380,6 +390,25 @@ jobs: - name: Unit tests run: ./gradlew testDebugUnitTest + # How many tests ran, summed from Gradle's JUnit reports. Runs after a + # failure too, so a red run still says how much of the suite it reached. + - name: Test count + if: always() + run: | + set -- app/build/test-results/testDebugUnitTest/*.xml + if [ ! -e "$1" ]; then echo "no unit test reports"; exit 0; fi + awk '/ /tmp/go-test-rc; } | testcount +// exit "$(cat /tmp/go-test-rc)" +package main + +import ( + "bufio" + "encoding/json" + "fmt" + "io" + "os" + "strings" +) + +type event struct { + Action string + Package string + Test string + Output string +} + +type tally struct{ passed, failed, skipped int } + +func (t *tally) add(action string) { + switch action { + case "pass": + t.passed++ + case "fail": + t.failed++ + case "skip": + t.skipped++ + } +} + +func (t tally) String() string { + return fmt.Sprintf("%d passed, %d failed, %d skipped", t.passed, t.failed, t.skipped) +} + +// pkgState holds one package's output until the package ends, keyed by test +// name ("" for the package itself), in the order each test first spoke. +type pkgState struct { + order []string + out map[string][]string +} + +func main() { + if err := run(os.Stdin, os.Stdout); err != nil { + fmt.Fprintln(os.Stderr, "testcount:", err) + os.Exit(1) + } +} + +// printer keeps the first write error, so run can report a closed stdout +// once instead of checking every line. +type printer struct { + w io.Writer + err error +} + +func (p *printer) print(s string) { + if p.err == nil { + _, p.err = io.WriteString(p.w, s) + } +} + +// lines writes a test's output without the run/pause/continue markers that +// -json adds and plain go test leaves out. +func (p *printer) lines(lines []string) { + for _, line := range lines { + if !strings.HasPrefix(strings.TrimLeft(line, " "), "=== ") { + p.print(line) + } + } +} + +func run(r io.Reader, w io.Writer) error { + out := &printer{w: w} + var tests, subtests tally + pkgs := map[string]*pkgState{} + state := func(pkg string) *pkgState { + p := pkgs[pkg] + if p == nil { + p = &pkgState{out: map[string][]string{}} + pkgs[pkg] = p + } + return p + } + + sc := bufio.NewScanner(r) + sc.Buffer(make([]byte, 64*1024), 64*1024*1024) + for sc.Scan() { + var e event + if err := json.Unmarshal(sc.Bytes(), &e); err != nil || e.Action == "" { + // Not an event (the go command's own complaints): pass it on. + out.print(sc.Text() + "\n") + continue + } + switch e.Action { + case "build-output": + out.print(e.Output) + case "output": + p := state(e.Package) + if _, seen := p.out[e.Test]; !seen { + p.order = append(p.order, e.Test) + } + p.out[e.Test] = append(p.out[e.Test], e.Output) + case "pass", "fail", "skip": + p := state(e.Package) + if e.Test != "" { + if strings.Contains(e.Test, "/") { + subtests.add(e.Action) + } else { + tests.add(e.Action) + } + if e.Action == "fail" { + out.lines(p.out[e.Test]) + } + delete(p.out, e.Test) + continue + } + if e.Action == "fail" { + // A test still holding output here never finished: the + // binary panicked or timed out while it ran. + for _, name := range p.order { + if name != "" { + out.lines(p.out[name]) + } + } + out.lines(p.out[""]) + } else { + for _, line := range p.out[""] { + if strings.HasPrefix(line, "ok ") || strings.HasPrefix(line, "?") { + out.print(line) + } + } + } + delete(pkgs, e.Package) + } + } + if err := sc.Err(); err != nil { + return err + } + out.print(fmt.Sprintf("\ntests: %s\nsubtests: %s\n", tests, subtests)) + return out.err +}