Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,25 @@ jobs:
git fetch --no-tags --unshallow origin
fi

# Coverage for the CI dashboard: on Linux, for every push to main, and for a PR
# whose head commit message has "with-coverage" (to try a change to it). The
# directories are outside the checkout, so that the check for new files below
# does not see them. The PR's head commit is there after the fetch above.
- name: Decide whether to collect coverage
id: cover
if: ${{ matrix.os.name == 'linux' && (github.event_name == 'push' || github.event_name == 'pull_request') }}
env:
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
if [ "$GITHUB_EVENT_NAME" = "push" ] || git log -1 --format=%B "$HEAD_SHA" | grep -q 'with-coverage'; then
echo "unit=$RUNNER_TEMP/cover-unit" >> "$GITHUB_OUTPUT"
echo "acc=$RUNNER_TEMP/cover-acc" >> "$GITHUB_OUTPUT"
fi

- name: Run tests
env:
CLI_GOCOVERDIR_UNIT: ${{ steps.cover.outputs.unit }}
CLI_GOCOVERDIR: ${{ steps.cover.outputs.acc }}
ENVFILTER: ${{ matrix.deployment.envfilter }}
# On pull requests, run only a subset of acceptance subtests on the slower
# windows/macOS cells so neither lands on the critical path to merge. An
Expand All @@ -181,6 +198,32 @@ jobs:
if-no-files-found: warn
retention-days: 7

# Failed tests count too: their coverage is as real as that of the ones that passed.
- name: Pack coverage
if: ${{ always() && steps.cover.outputs.acc != '' }}
env:
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
PR_NUMBER: ${{ github.event.pull_request.number }}
OS: ${{ matrix.os.name }}
CELL: ${{ matrix.deployment.name }}
UNIT_DIR: ${{ steps.cover.outputs.unit }}
ACC_DIR: ${{ steps.cover.outputs.acc }}
run: |
go -C tools run ./coverpack -root "$GITHUB_WORKSPACE" -unit "$UNIT_DIR" -acc "$ACC_DIR" \
-o "$RUNNER_TEMP/coverage.bin.zst" \
-meta "{\"sha\": \"$SHA\", \"os\": \"$OS\", \"cell\": \"$CELL\", \"pr\": ${PR_NUMBER:-null}}"

- name: Upload coverage
if: ${{ always() && steps.cover.outputs.acc != '' }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: coverage-${{ matrix.os.name }}-${{ matrix.deployment.name }}
path: ${{ runner.temp }}/coverage.bin.zst
if-no-files-found: warn
retention-days: 7
# Already compressed.
compression-level: 0

# `task test` prints a lot of output that GitHub is slow to render, so
# surface just the failed test names on the job summary page (and in this
# step's log) for a quick read of what broke.
Expand Down
9 changes: 8 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,21 @@ tasks:
# The slowest unit package observed across Linux/macOS/Windows is ~83s, so 10m is
# ample headroom while a hung package (e.g. a wedged goroutine) fails in minutes
# instead of burning the full hour. LOCAL_TIMEOUT still overrides when needed.
#
# CLI_GOCOVERDIR_UNIT (an absolute path) turns on coverage of every package by
# every test (tools/coverpack reads it). It is a coverage directory, not
# -coverprofile, because --rerun-fails runs go test again with the same flags and
# that would overwrite the profile with the coverage of the rerun alone.
- '[ -z "$CLI_GOCOVERDIR_UNIT" ] || mkdir -p "$CLI_GOCOVERDIR_UNIT"'
- |
{{.GO_TOOL}} gotestsum \
--format ${GOTESTSUM_FORMAT:-pkgname-and-test-fails} \
--no-summary=skipped \
--jsonfile test-output-unit-root.json \
--rerun-fails \
--packages "{{.TEST_PACKAGES}}" \
-- -timeout=${LOCAL_TIMEOUT:-10m}
-- ${CLI_GOCOVERDIR_UNIT:+-cover -coverpkg=./...} -timeout=${LOCAL_TIMEOUT:-10m} \
${CLI_GOCOVERDIR_UNIT:+-args -test.gocoverdir=$CLI_GOCOVERDIR_UNIT}

test-unit-tools:
desc: Run unit tests in tools/ module
Expand Down
2 changes: 2 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,8 @@ func runTest(t *testing.T,
}
err := os.MkdirAll(coverDir, os.ModePerm)
require.NoError(t, err)
// The directory name does not say which variant it is; tools/coverpack reads the test name from here.
testutil.WriteFile(t, filepath.Join(coverDir, "name"), t.Name())
cmd.Env = append(cmd.Env, "GOCOVERDIR="+coverDir)
}

Expand Down
245 changes: 245 additions & 0 deletions tools/coverpack/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
package main

// The packed file, zstd-compressed. Inside, a uvarint is unsigned LEB128 (encoding/binary's
// uvarint):
//
// "CLICOV1\n"
// uvarint n, n bytes meta, JSON: the -meta object plus "go", "module" and "commit" (the commit that was
// checked out and tested; for a PR, GitHub's merge of it into the base branch)
// uvarint L layouts, sorted by path:
// uvarint n, n bytes path relative to the module ("bundle/deploy/x.go")
// 20 bytes git blob sha of the file as read (zeros if it was not found)
// uvarint line count
// uvarint B blocks, sorted by start, then end; per block:
// uvarint sl - previous sl, uvarint sc, uvarint el - sl, uvarint ec, uvarint statements
// (as in a Go cover profile: lines and byte columns 1-based, the end exclusive)
// L times the distinct bitmaps of each layout, in layout order:
// uvarint P, then P bitmaps of ceil(B / 8) bytes: block i is bit i % 8 of byte i / 8
// uvarint N profiles:
// 1 byte kind 0: all acceptance tests merged, 1: unit tests merged, 2: one acceptance test
// uvarint n, n bytes test name (t.Name()), empty for kinds 0 and 1
// uvarint F files, by layout; per file: uvarint layout - previous layout, uvarint bitmap index
//
// Kinds 0 and 1 list every file of their build, covered or not: they are its inventory. A file
// that a test (kind 2) does not list was not covered by it if the build has the file, else it is
// unknown.

import (
"encoding/binary"
"errors"
"fmt"
"io"
)

const magic = "CLICOV1\n"

const (
kindAll = 0
kindUnit = 1
kindTest = 2
)

type layout struct {
path string // relative to the module
sha [20]byte
lines int
blocks []block
first int // catalog id of blocks[0]; the ids of a layout are consecutive
}

type profile struct {
kind byte
name string
files []int // layouts, ascending
bits bitset
}

type bitset []uint64

func newBitset(n int) bitset { return make(bitset, (n+63)/64) }

func (b bitset) set(i int32) { b[i>>6] |= 1 << (i & 63) }
func (b bitset) has(i int) bool { return b[i>>6]>>(i&63)&1 != 0 }

func (b bitset) or(o bitset) {
for i := range b {
b[i] |= o[i]
}
}

// bitmap is the bits of a layout, in the order of the file.
func (b bitset) bitmap(l *layout) []byte {
out := make([]byte, (len(l.blocks)+7)/8)
for i := range l.blocks {
if b.has(l.first + i) {
out[i/8] |= 1 << (i % 8)
}
}
return out
}

// encode returns the uncompressed file and the number of distinct bitmaps in it.
func encode(meta []byte, layouts []*layout, profiles []profile) ([]byte, int) {
distinct := make([]map[string]int, len(layouts))
order := make([][][]byte, len(layouts))
index := make([][]int, len(profiles))
for p, prof := range profiles {
for _, l := range prof.files {
bm := prof.bits.bitmap(layouts[l])
if distinct[l] == nil {
distinct[l] = map[string]int{}
}
i, ok := distinct[l][string(bm)]
if !ok {
i = len(order[l])
distinct[l][string(bm)] = i
order[l] = append(order[l], bm)
}
index[p] = append(index[p], i)
}
}

out := []byte(magic)
put := func(v int) { out = binary.AppendUvarint(out, uint64(v)) }
putBytes := func(b []byte) { put(len(b)); out = append(out, b...) }

putBytes(meta)
put(len(layouts))
for _, l := range layouts {
putBytes([]byte(l.path))
out = append(out, l.sha[:]...)
put(l.lines)
put(len(l.blocks))
prev := uint32(0)
for _, b := range l.blocks {
put(int(b.sl - prev))
put(int(b.sc))
put(int(b.el - b.sl))
put(int(b.ec))
put(int(b.stmts))
prev = b.sl
}
}
total := 0
for l := range layouts {
put(len(order[l]))
for _, bm := range order[l] {
out = append(out, bm...)
}
total += len(order[l])
}
put(len(profiles))
for p, prof := range profiles {
out = append(out, prof.kind)
putBytes([]byte(prof.name))
put(len(prof.files))
prev := 0
for i, l := range prof.files {
put(l - prev)
put(index[p][i])
prev = l
}
}
return out, total
}

// decoded is a packed file read back.
type decoded struct {
meta []byte
layouts []*layout
bitmaps [][][]byte // per layout
profiles []decodedProfile
}

type decodedProfile struct {
kind byte
name string
files []int // layouts, ascending
index []int // bitmap of each file
}

func decode(data []byte) (*decoded, error) {
c := &cursor{b: data}
if string(c.take(len(magic))) != magic {
return nil, errors.New("not a coverpack file")
}
str := func() []byte { return c.take(c.count()) }
d := &decoded{meta: str()}
d.layouts = make([]*layout, c.count())
for i := range d.layouts {
l := &layout{path: string(str())}
copy(l.sha[:], c.take(20))
l.lines = int(c.uleb128())
l.blocks = make([]block, c.count())
sl := uint32(0)
for j := range l.blocks {
sl += uint32(c.uleb128())
b := block{file: l.path, sl: sl, sc: uint32(c.uleb128())}
b.el = sl + uint32(c.uleb128())
b.ec = uint32(c.uleb128())
b.stmts = uint32(c.uleb128())
l.blocks[j] = b
}
d.layouts[i] = l
}
d.bitmaps = make([][][]byte, len(d.layouts))
for i, l := range d.layouts {
d.bitmaps[i] = make([][]byte, c.count())
for j := range d.bitmaps[i] {
d.bitmaps[i][j] = c.take((len(l.blocks) + 7) / 8)
}
}
d.profiles = make([]decodedProfile, c.count())
for i := range d.profiles {
p := decodedProfile{kind: c.u8(), name: string(str())}
n, l := c.count(), 0
for range n {
l += int(c.uleb128())
p.files = append(p.files, l)
p.index = append(p.index, int(c.uleb128()))
}
d.profiles[i] = p
}
if c.err != nil {
return nil, c.err
}
for _, p := range d.profiles {
for i, l := range p.files {
if l >= len(d.layouts) || p.index[i] >= len(d.bitmaps[l]) {
return nil, errMalformed
}
}
}
return d, nil
}

// dumpText writes the profiles as Go cover profile text, each after a "# all", "# unit" or
// "# test NAME" line. A test lists its covered blocks only.
func dumpText(w io.Writer, d *decoded, module string) error {
if _, err := fmt.Fprintf(w, "# coverage %s\n", d.meta); err != nil {
return err
}
for _, p := range d.profiles {
header := []string{"# all", "# unit"}
h := "# test " + p.name
if int(p.kind) < len(header) {
h = header[p.kind]
}
if _, err := fmt.Fprintln(w, h); err != nil {
return err
}
for i, l := range p.files {
bm := d.bitmaps[l][p.index[i]]
for j, b := range d.layouts[l].blocks {
covered := bm[j/8] >> (j % 8) & 1
if covered == 0 && p.kind == kindTest {
continue
}
if _, err := fmt.Fprintf(w, "%s/%s:%d.%d,%d.%d %d %d\n", module, b.file, b.sl, b.sc, b.el, b.ec, b.stmts, covered); err != nil {
return err
}
}
}
}
return nil
}
Loading
Loading