From e7f9308d43198223b192f94a7030f9ebdf92e551 Mon Sep 17 00:00:00 2001 From: Joey Jiao Date: Tue, 17 May 2022 14:30:46 +0800 Subject: pkg/cover: show percent of covered pcs in funcs --- pkg/cover/html.go | 72 ++++++++++++++++++++++++++++++++++-------------- pkg/cover/report_test.go | 6 ++-- 2 files changed, 55 insertions(+), 23 deletions(-) (limited to 'pkg') diff --git a/pkg/cover/html.go b/pkg/cover/html.go index 15e14eced..848505e13 100644 --- a/pkg/cover/html.go +++ b/pkg/cover/html.go @@ -96,12 +96,19 @@ func (rg *ReportGenerator) DoHTML(w io.Writer, progs []Prog, coverFilter map[uin pos = pos.Dirs[dir] fname = fname[sep+1:] } + var TotalInCoveredFunc int + for _, function := range file.functions { + if function.covered > 0 { + TotalInCoveredFunc += function.pcs + } + } f := &templateFile{ templateBase: templateBase{ - Path: path, - Name: fname, - Total: file.totalPCs, - Covered: file.coveredPCs, + Path: path, + Name: fname, + Total: file.totalPCs, + TotalInCoveredFunc: TotalInCoveredFunc, + Covered: file.coveredPCs, }, HasFunctions: len(file.functions) != 0, } @@ -349,7 +356,7 @@ func groupCoverByFilePrefixes(datas []fileStats, subsystems []mgrconfig.Subsyste var percentLines float64 var percentPCsInFile float64 var percentPCsInFunc float64 - var percentPCsInCoveredFunc float64 + var percentInCoveredFunc float64 var percentCoveredFunc float64 for _, path := range subsystem.Paths { @@ -379,7 +386,7 @@ func groupCoverByFilePrefixes(datas []fileStats, subsystems []mgrconfig.Subsyste percentPCsInFunc = 100.0 * float64(coveredPCsInFuncs) / float64(pcsInFuncs) } if pcsInCoveredFuncs != 0 { - percentPCsInCoveredFunc = 100.0 * float64(coveredPCsInFuncs) / float64(pcsInCoveredFuncs) + percentInCoveredFunc = 100.0 * float64(coveredPCsInFuncs) / float64(pcsInCoveredFuncs) } if totalFuncs != 0 { percentCoveredFunc = 100.0 * float64(coveredFuncs) / float64(totalFuncs) @@ -391,7 +398,7 @@ func groupCoverByFilePrefixes(datas []fileStats, subsystems []mgrconfig.Subsyste "PCsInFiles": fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFile, totalPCsInFile, percentPCsInFile), "Funcs": fmt.Sprintf("%v / %v / %.2f%%", coveredFuncs, totalFuncs, percentCoveredFunc), "PCsInFuncs": fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFuncs, pcsInFuncs, percentPCsInFunc), - "PCsInCoveredFuncs": fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFuncs, pcsInCoveredFuncs, percentPCsInCoveredFunc), + "PCsInCoveredFuncs": fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFuncs, pcsInCoveredFuncs, percentInCoveredFunc), } } @@ -425,7 +432,7 @@ func groupCoverByModule(datas []fileStats) map[string]map[string]string { percentLines := make(map[string]float64) percentPCsInFile := make(map[string]float64) percentPCsInFunc := make(map[string]float64) - percentPCsInCoveredFunc := make(map[string]float64) + percentInCoveredFunc := make(map[string]float64) percentCoveredFunc := make(map[string]float64) for _, data := range datas { @@ -451,7 +458,7 @@ func groupCoverByModule(datas []fileStats) map[string]map[string]string { percentPCsInFunc[m] = 100.0 * float64(coveredPCsInFuncs[m]) / float64(pcsInFuncs[m]) } if pcsInCoveredFuncs[m] != 0 { - percentPCsInCoveredFunc[m] = 100.0 * float64(coveredPCsInFuncs[m]) / float64(pcsInCoveredFuncs[m]) + percentInCoveredFunc[m] = 100.0 * float64(coveredPCsInFuncs[m]) / float64(pcsInCoveredFuncs[m]) } if totalFuncs[m] != 0 { percentCoveredFunc[m] = 100.0 * float64(coveredFuncs[m]) / float64(totalFuncs[m]) @@ -460,7 +467,7 @@ func groupCoverByModule(datas []fileStats) map[string]map[string]string { pcsInFiles := fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFile[m], totalPCsInFile[m], percentPCsInFile[m]) funcs := fmt.Sprintf("%v / %v / %.2f%%", coveredFuncs[m], totalFuncs[m], percentCoveredFunc[m]) pcsInFuncs := fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFuncs[m], pcsInFuncs[m], percentPCsInFunc[m]) - covedFuncs := fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFuncs[m], pcsInCoveredFuncs[m], percentPCsInCoveredFunc[m]) + covedFuncs := fmt.Sprintf("%v / %v / %.2f%%", coveredPCsInFuncs[m], pcsInCoveredFuncs[m], percentInCoveredFunc[m]) d[m] = map[string]string{ "name": m, "lines": lines, @@ -656,10 +663,14 @@ func mergeLine(chunks []lineCoverChunk, start, end int, covered bool) []lineCove func addFunctionCoverage(file *file, data *templateData) { var buf bytes.Buffer + var coveredTotal int + var TotalInCoveredFunc int for _, function := range file.functions { percentage := "" + coveredTotal += function.covered if function.covered > 0 { percentage = fmt.Sprintf("%v%%", percent(function.covered, function.pcs)) + TotalInCoveredFunc += function.pcs } else { percentage = "---" } @@ -668,6 +679,17 @@ func addFunctionCoverage(file *file, data *templateData) { buf.WriteString(fmt.Sprintf("of %v", strconv.Itoa(function.pcs))) buf.WriteString("
\n") } + buf.WriteString("-----------
\n") + buf.WriteString("SUMMARY") + percentInCoveredFunc := "" + if TotalInCoveredFunc > 0 { + percentInCoveredFunc = fmt.Sprintf("%v%%", percent(coveredTotal, TotalInCoveredFunc)) + } else { + percentInCoveredFunc = "---" + } + buf.WriteString(fmt.Sprintf("%v", percentInCoveredFunc)) + buf.WriteString(fmt.Sprintf("of %v", strconv.Itoa(TotalInCoveredFunc))) + buf.WriteString("
\n") data.Functions = append(data.Functions, template.HTML(buf.String())) } @@ -685,14 +707,22 @@ func processDir(dir *templateDir) { for _, f := range dir.Files { dir.Total += f.Total dir.Covered += f.Covered + dir.TotalInCoveredFunc += f.TotalInCoveredFunc f.Percent = percent(f.Covered, f.Total) + if f.TotalInCoveredFunc > 0 { + f.PercentInCoveredFunc = percent(f.Covered, f.TotalInCoveredFunc) + } } for _, child := range dir.Dirs { processDir(child) dir.Total += child.Total dir.Covered += child.Covered + dir.TotalInCoveredFunc += child.TotalInCoveredFunc } dir.Percent = percent(dir.Covered, dir.Total) + if dir.TotalInCoveredFunc > 0 { + dir.PercentInCoveredFunc += percent(dir.Covered, dir.TotalInCoveredFunc) + } if dir.Covered == 0 { dir.Dirs = nil dir.Files = nil @@ -741,11 +771,13 @@ type templateProg struct { } type templateBase struct { - Name string - Path string - Total int - Covered int - Percent int + Name string + Path string + Total int + Covered int + TotalInCoveredFunc int + Percent int + PercentInCoveredFunc int } type templateDir struct { @@ -809,7 +841,7 @@ var coverTemplate = template.Must(template.New("").Parse(` } .cover { float: right; - width: 120px; + width: 250px; padding-right: 4px; } .cover-right { @@ -954,8 +986,8 @@ var coverTemplate = template.Must(template.New("").Parse(` {{$dir.Name}} - {{if $dir.Covered}}{{$dir.Percent}}%{{else}}---{{end}} - of {{$dir.Total}} + {{if $dir.Covered}}{{$dir.Percent}}%({{$dir.PercentInCoveredFunc}}%){{else}}---{{end}} + of {{$dir.Total}}({{$dir.TotalInCoveredFunc}})