|
| 1 | +package main |
| 2 | + |
| 3 | +// The packed file, zstd-compressed. Inside, a uvarint is unsigned LEB128 (encoding/binary's |
| 4 | +// uvarint): |
| 5 | +// |
| 6 | +// "CLICOV1\n" |
| 7 | +// uvarint n, n bytes meta, JSON: the -meta object plus "go", "module" and "commit" (the commit that was |
| 8 | +// checked out and tested; for a PR, GitHub's merge of it into the base branch) |
| 9 | +// uvarint L layouts, sorted by path: |
| 10 | +// uvarint n, n bytes path relative to the module ("bundle/deploy/x.go") |
| 11 | +// 20 bytes git blob sha of the file as read (zeros if it was not found) |
| 12 | +// uvarint line count |
| 13 | +// uvarint B blocks, sorted by start, then end; per block: |
| 14 | +// uvarint sl - previous sl, uvarint sc, uvarint el - sl, uvarint ec, uvarint statements |
| 15 | +// (as in a Go cover profile: lines and byte columns 1-based, the end exclusive) |
| 16 | +// L times the distinct bitmaps of each layout, in layout order: |
| 17 | +// uvarint P, then P bitmaps of ceil(B / 8) bytes: block i is bit i % 8 of byte i / 8 |
| 18 | +// uvarint N profiles: |
| 19 | +// 1 byte kind 0: all acceptance tests merged, 1: unit tests merged, 2: one acceptance test |
| 20 | +// uvarint n, n bytes test name (t.Name()), empty for kinds 0 and 1 |
| 21 | +// uvarint F files, by layout; per file: uvarint layout - previous layout, uvarint bitmap index |
| 22 | +// |
| 23 | +// Kinds 0 and 1 list every file of their build, covered or not: they are its inventory. A file |
| 24 | +// that a test (kind 2) does not list was not covered by it if the build has the file, else it is |
| 25 | +// unknown. |
| 26 | + |
| 27 | +import ( |
| 28 | + "encoding/binary" |
| 29 | + "errors" |
| 30 | + "fmt" |
| 31 | + "io" |
| 32 | +) |
| 33 | + |
| 34 | +const magic = "CLICOV1\n" |
| 35 | + |
| 36 | +const ( |
| 37 | + kindAll = 0 |
| 38 | + kindUnit = 1 |
| 39 | + kindTest = 2 |
| 40 | +) |
| 41 | + |
| 42 | +type layout struct { |
| 43 | + path string // relative to the module |
| 44 | + sha [20]byte |
| 45 | + lines int |
| 46 | + blocks []block |
| 47 | + first int // catalog id of blocks[0]; the ids of a layout are consecutive |
| 48 | +} |
| 49 | + |
| 50 | +type profile struct { |
| 51 | + kind byte |
| 52 | + name string |
| 53 | + files []int // layouts, ascending |
| 54 | + bits bitset |
| 55 | +} |
| 56 | + |
| 57 | +type bitset []uint64 |
| 58 | + |
| 59 | +func newBitset(n int) bitset { return make(bitset, (n+63)/64) } |
| 60 | + |
| 61 | +func (b bitset) set(i int32) { b[i>>6] |= 1 << (i & 63) } |
| 62 | +func (b bitset) has(i int) bool { return b[i>>6]>>(i&63)&1 != 0 } |
| 63 | + |
| 64 | +func (b bitset) or(o bitset) { |
| 65 | + for i := range b { |
| 66 | + b[i] |= o[i] |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// bitmap is the bits of a layout, in the order of the file. |
| 71 | +func (b bitset) bitmap(l *layout) []byte { |
| 72 | + out := make([]byte, (len(l.blocks)+7)/8) |
| 73 | + for i := range l.blocks { |
| 74 | + if b.has(l.first + i) { |
| 75 | + out[i/8] |= 1 << (i % 8) |
| 76 | + } |
| 77 | + } |
| 78 | + return out |
| 79 | +} |
| 80 | + |
| 81 | +// encode returns the uncompressed file and the number of distinct bitmaps in it. |
| 82 | +func encode(meta []byte, layouts []*layout, profiles []profile) ([]byte, int) { |
| 83 | + distinct := make([]map[string]int, len(layouts)) |
| 84 | + order := make([][][]byte, len(layouts)) |
| 85 | + index := make([][]int, len(profiles)) |
| 86 | + for p, prof := range profiles { |
| 87 | + for _, l := range prof.files { |
| 88 | + bm := prof.bits.bitmap(layouts[l]) |
| 89 | + if distinct[l] == nil { |
| 90 | + distinct[l] = map[string]int{} |
| 91 | + } |
| 92 | + i, ok := distinct[l][string(bm)] |
| 93 | + if !ok { |
| 94 | + i = len(order[l]) |
| 95 | + distinct[l][string(bm)] = i |
| 96 | + order[l] = append(order[l], bm) |
| 97 | + } |
| 98 | + index[p] = append(index[p], i) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + out := []byte(magic) |
| 103 | + put := func(v int) { out = binary.AppendUvarint(out, uint64(v)) } |
| 104 | + putBytes := func(b []byte) { put(len(b)); out = append(out, b...) } |
| 105 | + |
| 106 | + putBytes(meta) |
| 107 | + put(len(layouts)) |
| 108 | + for _, l := range layouts { |
| 109 | + putBytes([]byte(l.path)) |
| 110 | + out = append(out, l.sha[:]...) |
| 111 | + put(l.lines) |
| 112 | + put(len(l.blocks)) |
| 113 | + prev := uint32(0) |
| 114 | + for _, b := range l.blocks { |
| 115 | + put(int(b.sl - prev)) |
| 116 | + put(int(b.sc)) |
| 117 | + put(int(b.el - b.sl)) |
| 118 | + put(int(b.ec)) |
| 119 | + put(int(b.stmts)) |
| 120 | + prev = b.sl |
| 121 | + } |
| 122 | + } |
| 123 | + total := 0 |
| 124 | + for l := range layouts { |
| 125 | + put(len(order[l])) |
| 126 | + for _, bm := range order[l] { |
| 127 | + out = append(out, bm...) |
| 128 | + } |
| 129 | + total += len(order[l]) |
| 130 | + } |
| 131 | + put(len(profiles)) |
| 132 | + for p, prof := range profiles { |
| 133 | + out = append(out, prof.kind) |
| 134 | + putBytes([]byte(prof.name)) |
| 135 | + put(len(prof.files)) |
| 136 | + prev := 0 |
| 137 | + for i, l := range prof.files { |
| 138 | + put(l - prev) |
| 139 | + put(index[p][i]) |
| 140 | + prev = l |
| 141 | + } |
| 142 | + } |
| 143 | + return out, total |
| 144 | +} |
| 145 | + |
| 146 | +// decoded is a packed file read back. |
| 147 | +type decoded struct { |
| 148 | + meta []byte |
| 149 | + layouts []*layout |
| 150 | + bitmaps [][][]byte // per layout |
| 151 | + profiles []decodedProfile |
| 152 | +} |
| 153 | + |
| 154 | +type decodedProfile struct { |
| 155 | + kind byte |
| 156 | + name string |
| 157 | + files []int // layouts, ascending |
| 158 | + index []int // bitmap of each file |
| 159 | +} |
| 160 | + |
| 161 | +func decode(data []byte) (*decoded, error) { |
| 162 | + c := &cursor{b: data} |
| 163 | + if string(c.take(len(magic))) != magic { |
| 164 | + return nil, errors.New("not a coverpack file") |
| 165 | + } |
| 166 | + str := func() []byte { return c.take(c.count()) } |
| 167 | + d := &decoded{meta: str()} |
| 168 | + d.layouts = make([]*layout, c.count()) |
| 169 | + for i := range d.layouts { |
| 170 | + l := &layout{path: string(str())} |
| 171 | + copy(l.sha[:], c.take(20)) |
| 172 | + l.lines = int(c.uleb128()) |
| 173 | + l.blocks = make([]block, c.count()) |
| 174 | + sl := uint32(0) |
| 175 | + for j := range l.blocks { |
| 176 | + sl += uint32(c.uleb128()) |
| 177 | + b := block{file: l.path, sl: sl, sc: uint32(c.uleb128())} |
| 178 | + b.el = sl + uint32(c.uleb128()) |
| 179 | + b.ec = uint32(c.uleb128()) |
| 180 | + b.stmts = uint32(c.uleb128()) |
| 181 | + l.blocks[j] = b |
| 182 | + } |
| 183 | + d.layouts[i] = l |
| 184 | + } |
| 185 | + d.bitmaps = make([][][]byte, len(d.layouts)) |
| 186 | + for i, l := range d.layouts { |
| 187 | + d.bitmaps[i] = make([][]byte, c.count()) |
| 188 | + for j := range d.bitmaps[i] { |
| 189 | + d.bitmaps[i][j] = c.take((len(l.blocks) + 7) / 8) |
| 190 | + } |
| 191 | + } |
| 192 | + d.profiles = make([]decodedProfile, c.count()) |
| 193 | + for i := range d.profiles { |
| 194 | + p := decodedProfile{kind: c.u8(), name: string(str())} |
| 195 | + n, l := c.count(), 0 |
| 196 | + for range n { |
| 197 | + l += int(c.uleb128()) |
| 198 | + p.files = append(p.files, l) |
| 199 | + p.index = append(p.index, int(c.uleb128())) |
| 200 | + } |
| 201 | + d.profiles[i] = p |
| 202 | + } |
| 203 | + if c.err != nil { |
| 204 | + return nil, c.err |
| 205 | + } |
| 206 | + for _, p := range d.profiles { |
| 207 | + for i, l := range p.files { |
| 208 | + if l < 0 || l >= len(d.layouts) || p.index[i] < 0 || p.index[i] >= len(d.bitmaps[l]) { |
| 209 | + return nil, errMalformed |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + return d, nil |
| 214 | +} |
| 215 | + |
| 216 | +// dumpText writes the profiles as Go cover profile text, each after a "# all", "# unit" or |
| 217 | +// "# test NAME" line. A test lists its covered blocks only. |
| 218 | +func dumpText(w io.Writer, d *decoded, module string) error { |
| 219 | + if _, err := fmt.Fprintf(w, "# coverage %s\n", d.meta); err != nil { |
| 220 | + return err |
| 221 | + } |
| 222 | + for _, p := range d.profiles { |
| 223 | + header := []string{"# all", "# unit"} |
| 224 | + h := "# test " + p.name |
| 225 | + if int(p.kind) < len(header) { |
| 226 | + h = header[p.kind] |
| 227 | + } |
| 228 | + if _, err := fmt.Fprintln(w, h); err != nil { |
| 229 | + return err |
| 230 | + } |
| 231 | + for i, l := range p.files { |
| 232 | + bm := d.bitmaps[l][p.index[i]] |
| 233 | + for j, b := range d.layouts[l].blocks { |
| 234 | + covered := bm[j/8] >> (j % 8) & 1 |
| 235 | + if covered == 0 && p.kind == kindTest { |
| 236 | + continue |
| 237 | + } |
| 238 | + 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 { |
| 239 | + return err |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + return nil |
| 245 | +} |
0 commit comments