From 769c9b7a94fbe6491efdb1faf9908aba2227c0fe Mon Sep 17 00:00:00 2001 From: Jouni Hogander Date: Thu, 20 Aug 2020 15:48:28 +0300 Subject: pkg/cover: add new view for function coverage Add new view where function coverage is shown when clicking file coverage percent --- pkg/cover/report.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/pkg/cover/report.go b/pkg/cover/report.go index 666babb02..0a65c11a7 100644 --- a/pkg/cover/report.go +++ b/pkg/cover/report.go @@ -330,14 +330,36 @@ func (rg *ReportGenerator) generateHTML(w io.Writer, progs []Prog, files map[str } d.Contents = append(d.Contents, template.HTML(buf.String())) f.Index = len(d.Contents) - 1 + + addFunctionCoverage(file, d) } for _, prog := range progs { d.Progs = append(d.Progs, template.HTML(html.EscapeString(prog.Data))) } + processDir(d.Root) return coverTemplate.Execute(w, d) } +func addFunctionCoverage(file *file, data *templateData) { + var buf bytes.Buffer + for functionName, function := range file.functions { + var percentage string + if len(function.coverPCs) > 0 { + percentage = fmt.Sprintf("%d%%", percent(len(function.coverPCs), len(function.totalPCs))) + } else { + percentage = "---" + } + buf.WriteString(fmt.Sprintf("%v", functionName)) + buf.WriteString(fmt.Sprintf("%v", percentage)) + buf.WriteString(fmt.Sprintf("of %v", strconv.Itoa(len(function.totalPCs)))) + buf.WriteString("
\n") + } + if buf.Len() > 0 { + data.Functions = append(data.Functions, template.HTML(buf.String())) + } +} + func processDir(dir *templateDir) { for len(dir.Dirs) == 1 && len(dir.Files) == 0 { for _, child := range dir.Dirs { @@ -611,9 +633,10 @@ func archCallInsn(target *targets.Target) ([][]byte, [][]byte) { } type templateData struct { - Root *templateDir - Contents []template.HTML - Progs []template.HTML + Root *templateDir + Contents []template.HTML + Progs []template.HTML + Functions []template.HTML } type templateBase struct { @@ -663,6 +686,18 @@ var coverTemplate = template.Must(template.New("").Parse(` left: 0; width: 24%; } + .function { + height: 100%; + position: fixed; + z-index: 1; + top: 0; + overflow-x: hidden; + display: none; + } + .list { + left: 24%; + width: 30%; + } .right { border-left: 2px solid #444; right: 0; @@ -738,6 +773,9 @@ var coverTemplate = template.Must(template.New("").Parse(` {{range $i, $p := .Progs}}
{{$p}}
{{end}} + {{range $i, $p := .Functions}} +
{{$p}}
+ {{end}}