github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/internal/coverage/cformat/format.go (about) 1 // Copyright 2022 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cformat 6 7 // This package provides apis for producing human-readable summaries 8 // of coverage data (e.g. a coverage percentage for a given package or 9 // set of packages) and for writing data in the legacy test format 10 // emitted by "go test -coverprofile=<outfile>". 11 // 12 // The model for using these apis is to create a Formatter object, 13 // then make a series of calls to SetPackage and AddUnit passing in 14 // data read from coverage meta-data and counter-data files. E.g. 15 // 16 // myformatter := cformat.NewFormatter() 17 // ... 18 // for each package P in meta-data file: { 19 // myformatter.SetPackage(P) 20 // for each function F in P: { 21 // for each coverable unit U in F: { 22 // myformatter.AddUnit(U) 23 // } 24 // } 25 // } 26 // myformatter.EmitPercent(os.Stdout, "") 27 // myformatter.EmitTextual(somefile) 28 // 29 // These apis are linked into tests that are built with "-cover", and 30 // called at the end of test execution to produce text output or 31 // emit coverage percentages. 32 33 import ( 34 "fmt" 35 "internal/coverage" 36 "internal/coverage/cmerge" 37 "io" 38 "sort" 39 "text/tabwriter" 40 ) 41 42 type Formatter struct { 43 // Maps import path to package state. 44 pm map[string]*pstate 45 // Records current package being visited. 46 pkg string 47 // Pointer to current package state. 48 p *pstate 49 // Counter mode. 50 cm coverage.CounterMode 51 } 52 53 // pstate records package-level coverage data state: 54 // - a table of functions (file/fname/literal) 55 // - a map recording the index/ID of each func encountered so far 56 // - a table storing execution count for the coverable units in each func 57 type pstate struct { 58 // slice of unique functions 59 funcs []fnfile 60 // maps function to index in slice above (index acts as function ID) 61 funcTable map[fnfile]uint32 62 63 // A table storing coverage counts for each coverable unit. 64 unitTable map[extcu]uint32 65 } 66 67 // extcu encapsulates a coverable unit within some function. 68 type extcu struct { 69 fnfid uint32 // index into p.funcs slice 70 coverage.CoverableUnit 71 } 72 73 // fnfile is a function-name/file-name tuple. 74 type fnfile struct { 75 file string 76 fname string 77 lit bool 78 } 79 80 func NewFormatter(cm coverage.CounterMode) *Formatter { 81 return &Formatter{ 82 pm: make(map[string]*pstate), 83 cm: cm, 84 } 85 } 86 87 // SetPackage tells the formatter that we're about to visit the 88 // coverage data for the package with the specified import path. 89 // Note that it's OK to call SetPackage more than once with the 90 // same import path; counter data values will be accumulated. 91 func (fm *Formatter) SetPackage(importpath string) { 92 if importpath == fm.pkg { 93 return 94 } 95 fm.pkg = importpath 96 ps, ok := fm.pm[importpath] 97 if !ok { 98 ps = new(pstate) 99 fm.pm[importpath] = ps 100 ps.unitTable = make(map[extcu]uint32) 101 ps.funcTable = make(map[fnfile]uint32) 102 } 103 fm.p = ps 104 } 105 106 // AddUnit passes info on a single coverable unit (file, funcname, 107 // literal flag, range of lines, and counter value) to the formatter. 108 // Counter values will be accumulated where appropriate. 109 func (fm *Formatter) AddUnit(file string, fname string, isfnlit bool, unit coverage.CoverableUnit, count uint32) { 110 if fm.p == nil { 111 panic("AddUnit invoked before SetPackage") 112 } 113 fkey := fnfile{file: file, fname: fname, lit: isfnlit} 114 idx, ok := fm.p.funcTable[fkey] 115 if !ok { 116 idx = uint32(len(fm.p.funcs)) 117 fm.p.funcs = append(fm.p.funcs, fkey) 118 fm.p.funcTable[fkey] = idx 119 } 120 ukey := extcu{fnfid: idx, CoverableUnit: unit} 121 pcount := fm.p.unitTable[ukey] 122 var result uint32 123 if fm.cm == coverage.CtrModeSet { 124 if count != 0 || pcount != 0 { 125 result = 1 126 } 127 } else { 128 // Use saturating arithmetic. 129 result, _ = cmerge.SaturatingAdd(pcount, count) 130 } 131 fm.p.unitTable[ukey] = result 132 } 133 134 // sortUnits sorts a slice of extcu objects in a package according to 135 // source position information (e.g. file and line). Note that we don't 136 // include function name as part of the sorting criteria, the thinking 137 // being that is better to provide things in the original source order. 138 func (p *pstate) sortUnits(units []extcu) { 139 sort.Slice(units, func(i, j int) bool { 140 ui := units[i] 141 uj := units[j] 142 ifile := p.funcs[ui.fnfid].file 143 jfile := p.funcs[uj.fnfid].file 144 if ifile != jfile { 145 return ifile < jfile 146 } 147 // NB: not taking function literal flag into account here (no 148 // need, since other fields are guaranteed to be distinct). 149 if units[i].StLine != units[j].StLine { 150 return units[i].StLine < units[j].StLine 151 } 152 if units[i].EnLine != units[j].EnLine { 153 return units[i].EnLine < units[j].EnLine 154 } 155 if units[i].StCol != units[j].StCol { 156 return units[i].StCol < units[j].StCol 157 } 158 if units[i].EnCol != units[j].EnCol { 159 return units[i].EnCol < units[j].EnCol 160 } 161 return units[i].NxStmts < units[j].NxStmts 162 }) 163 } 164 165 // EmitTextual writes the accumulated coverage data in the legacy 166 // cmd/cover text format to the writer 'w'. We sort the data items by 167 // importpath, source file, and line number before emitting (this sorting 168 // is not explicitly mandated by the format, but seems like a good idea 169 // for repeatable/deterministic dumps). 170 func (fm *Formatter) EmitTextual(w io.Writer) error { 171 if fm.cm == coverage.CtrModeInvalid { 172 panic("internal error, counter mode unset") 173 } 174 if _, err := fmt.Fprintf(w, "mode: %s\n", fm.cm.String()); err != nil { 175 return err 176 } 177 pkgs := make([]string, 0, len(fm.pm)) 178 for importpath := range fm.pm { 179 pkgs = append(pkgs, importpath) 180 } 181 sort.Strings(pkgs) 182 for _, importpath := range pkgs { 183 p := fm.pm[importpath] 184 units := make([]extcu, 0, len(p.unitTable)) 185 for u := range p.unitTable { 186 units = append(units, u) 187 } 188 p.sortUnits(units) 189 for _, u := range units { 190 count := p.unitTable[u] 191 file := p.funcs[u.fnfid].file 192 if _, err := fmt.Fprintf(w, "%s:%d.%d,%d.%d %d %d\n", 193 file, u.StLine, u.StCol, 194 u.EnLine, u.EnCol, u.NxStmts, count); err != nil { 195 return err 196 } 197 } 198 } 199 return nil 200 } 201 202 // EmitPercent writes out a "percentage covered" string to the writer 'w'. 203 func (fm *Formatter) EmitPercent(w io.Writer, covpkgs string, noteEmpty bool) error { 204 pkgs := make([]string, 0, len(fm.pm)) 205 for importpath := range fm.pm { 206 pkgs = append(pkgs, importpath) 207 } 208 sort.Strings(pkgs) 209 seenPkg := false 210 for _, importpath := range pkgs { 211 seenPkg = true 212 p := fm.pm[importpath] 213 var totalStmts, coveredStmts uint64 214 for unit, count := range p.unitTable { 215 nx := uint64(unit.NxStmts) 216 totalStmts += nx 217 if count != 0 { 218 coveredStmts += nx 219 } 220 } 221 if _, err := fmt.Fprintf(w, "\t%s\t", importpath); err != nil { 222 return err 223 } 224 if totalStmts == 0 { 225 if _, err := fmt.Fprintf(w, "coverage: [no statements]\n"); err != nil { 226 return err 227 } 228 } else { 229 if _, err := fmt.Fprintf(w, "coverage: %.1f%% of statements%s\n", 100*float64(coveredStmts)/float64(totalStmts), covpkgs); err != nil { 230 return err 231 } 232 } 233 } 234 if noteEmpty && !seenPkg { 235 if _, err := fmt.Fprintf(w, "coverage: [no statements]\n"); err != nil { 236 return err 237 } 238 } 239 240 return nil 241 } 242 243 // EmitFuncs writes out a function-level summary to the writer 'w'. A 244 // note on handling function literals: although we collect coverage 245 // data for unnamed literals, it probably does not make sense to 246 // include them in the function summary since there isn't any good way 247 // to name them (this is also consistent with the legacy cmd/cover 248 // implementation). We do want to include their counts in the overall 249 // summary however. 250 func (fm *Formatter) EmitFuncs(w io.Writer) error { 251 if fm.cm == coverage.CtrModeInvalid { 252 panic("internal error, counter mode unset") 253 } 254 perc := func(covered, total uint64) float64 { 255 if total == 0 { 256 total = 1 257 } 258 return 100.0 * float64(covered) / float64(total) 259 } 260 tabber := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0) 261 defer tabber.Flush() 262 allStmts := uint64(0) 263 covStmts := uint64(0) 264 265 pkgs := make([]string, 0, len(fm.pm)) 266 for importpath := range fm.pm { 267 pkgs = append(pkgs, importpath) 268 } 269 sort.Strings(pkgs) 270 271 // Emit functions for each package, sorted by import path. 272 for _, importpath := range pkgs { 273 p := fm.pm[importpath] 274 if len(p.unitTable) == 0 { 275 continue 276 } 277 units := make([]extcu, 0, len(p.unitTable)) 278 for u := range p.unitTable { 279 units = append(units, u) 280 } 281 282 // Within a package, sort the units, then walk through the 283 // sorted array. Each time we hit a new function, emit the 284 // summary entry for the previous function, then make one last 285 // emit call at the end of the loop. 286 p.sortUnits(units) 287 fname := "" 288 ffile := "" 289 flit := false 290 var fline uint32 291 var cstmts, tstmts uint64 292 captureFuncStart := func(u extcu) { 293 fname = p.funcs[u.fnfid].fname 294 ffile = p.funcs[u.fnfid].file 295 flit = p.funcs[u.fnfid].lit 296 fline = u.StLine 297 } 298 emitFunc := func(u extcu) error { 299 // Don't emit entries for function literals (see discussion 300 // in function header comment above). 301 if !flit { 302 if _, err := fmt.Fprintf(tabber, "%s:%d:\t%s\t%.1f%%\n", 303 ffile, fline, fname, perc(cstmts, tstmts)); err != nil { 304 return err 305 } 306 } 307 captureFuncStart(u) 308 allStmts += tstmts 309 covStmts += cstmts 310 tstmts = 0 311 cstmts = 0 312 return nil 313 } 314 for k, u := range units { 315 if k == 0 { 316 captureFuncStart(u) 317 } else { 318 if fname != p.funcs[u.fnfid].fname { 319 // New function; emit entry for previous one. 320 if err := emitFunc(u); err != nil { 321 return err 322 } 323 } 324 } 325 tstmts += uint64(u.NxStmts) 326 count := p.unitTable[u] 327 if count != 0 { 328 cstmts += uint64(u.NxStmts) 329 } 330 } 331 if err := emitFunc(extcu{}); err != nil { 332 return err 333 } 334 } 335 if _, err := fmt.Fprintf(tabber, "%s\t%s\t%.1f%%\n", 336 "total", "(statements)", perc(covStmts, allStmts)); err != nil { 337 return err 338 } 339 return nil 340 }