| 1 |
//go:build !coverage |
| 2 |
// +build !coverage |
| 3 |
|
| 4 |
// Generate an HTML visualization of a Go coverage profile. |
| 5 |
// Serves a similar purpose to "go tool cover -html", but has a different |
| 6 |
// visual style. |
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"flag" |
| 11 |
"fmt" |
| 12 |
"html/template" |
| 13 |
"math" |
| 14 |
"os" |
| 15 |
"strings" |
| 16 |
|
| 17 |
"golang.org/x/tools/cover" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
input = flag.String("input", "", "input file") |
| 22 |
output = flag.String("output", "", "output file") |
| 23 |
strip = flag.Int("strip", 0, "how many path entries to strip") |
| 24 |
title = flag.String("title", "Coverage report", "page title") |
| 25 |
notes = flag.String("notes", "", "notes to add at the beginning (HTML)") |
| 26 |
) |
| 27 |
|
| 28 |
func errorf(f string, a ...interface{}) { |
| 29 |
fmt.Printf(f, a...) |
| 30 |
os.Exit(1) |
| 31 |
} |
| 32 |
|
| 33 |
func main() { |
| 34 |
flag.Parse() |
| 35 |
|
| 36 |
profiles, err := cover.ParseProfiles(*input) |
| 37 |
if err != nil { |
| 38 |
errorf("Error parsing input %q: %v\n", *input, err) |
| 39 |
} |
| 40 |
|
| 41 |
totals := &Totals{ |
| 42 |
totalF: map[string]int{}, |
| 43 |
coveredF: map[string]int{}, |
| 44 |
} |
| 45 |
files := []string{} |
| 46 |
code := map[string]template.HTML{} |
| 47 |
for _, p := range profiles { |
| 48 |
files = append(files, p.FileName) |
| 49 |
totals.Add(p) |
| 50 |
|
| 51 |
fname := strings.Join(strings.Split(p.FileName, "/")[*strip:], "/") |
| 52 |
src, err := os.ReadFile(fname) |
| 53 |
if err != nil { |
| 54 |
errorf("Failed to read %q: %v", fname, err) |
| 55 |
} |
| 56 |
|
| 57 |
code[p.FileName] = genHTML(src, p.Boundaries(src)) |
| 58 |
} |
| 59 |
|
| 60 |
out, err := os.Create(*output) |
| 61 |
if err != nil { |
| 62 |
errorf("Failed to open output file %q: %v", *output, err) |
| 63 |
} |
| 64 |
|
| 65 |
data := struct { |
| 66 |
Title string |
| 67 |
Notes template.HTML |
| 68 |
Files []string |
| 69 |
Code map[string]template.HTML |
| 70 |
Totals *Totals |
| 71 |
}{ |
| 72 |
Title: *title, |
| 73 |
Notes: template.HTML(*notes), |
| 74 |
Files: files, |
| 75 |
Code: code, |
| 76 |
Totals: totals, |
| 77 |
} |
| 78 |
|
| 79 |
tmpl := template.Must(template.New("html").Parse(htmlTmpl)) |
| 80 |
err = tmpl.Execute(out, data) |
| 81 |
if err != nil { |
| 82 |
errorf("Failed to execute template: %v", err) |
| 83 |
} |
| 84 |
|
| 85 |
for _, f := range files { |
| 86 |
fmt.Printf("%5.1f%% %v\n", totals.Percent(f), f) |
| 87 |
} |
| 88 |
fmt.Printf("\n") |
| 89 |
fmt.Printf("Total: %.1f\n", totals.TotalPercent()) |
| 90 |
} |
| 91 |
|
| 92 |
// Totals is used to keep track of total counters. |
| 93 |
type Totals struct { |
| 94 |
// Total statements. |
| 95 |
total int |
| 96 |
|
| 97 |
// Covered statements. |
| 98 |
covered int |
| 99 |
|
| 100 |
// Total statements per file. |
| 101 |
totalF map[string]int |
| 102 |
|
| 103 |
// Covered statements per file. |
| 104 |
coveredF map[string]int |
| 105 |
} |
| 106 |
|
| 107 |
// Add the given profile to the total counters. |
| 108 |
func (t *Totals) Add(p *cover.Profile) { |
| 109 |
for _, b := range p.Blocks { |
| 110 |
t.total += b.NumStmt |
| 111 |
t.totalF[p.FileName] += b.NumStmt |
| 112 |
if b.Count > 0 { |
| 113 |
t.covered += b.NumStmt |
| 114 |
t.coveredF[p.FileName] += b.NumStmt |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// Percent covered for the given file. |
| 120 |
func (t *Totals) Percent(f string) float32 { |
| 121 |
return float32(t.coveredF[f]) / float32(t.totalF[f]) * 100 |
| 122 |
} |
| 123 |
|
| 124 |
// TotalPercent covered, across all files. |
| 125 |
func (t *Totals) TotalPercent() float32 { |
| 126 |
return float32(t.covered) / float32(t.total) * 100 |
| 127 |
} |
| 128 |
|
| 129 |
func genHTML(src []byte, boundaries []cover.Boundary) template.HTML { |
| 130 |
// Position -> []Boundary |
| 131 |
// The order matters, we expect to receive start-end pairs in order, so |
| 132 |
// they are properly added. |
| 133 |
bs := map[int][]cover.Boundary{} |
| 134 |
for _, b := range boundaries { |
| 135 |
bs[b.Offset] = append(bs[b.Offset], b) |
| 136 |
} |
| 137 |
|
| 138 |
w := &strings.Builder{} |
| 139 |
for i := range src { |
| 140 |
// Emit boundary markers. |
| 141 |
for _, b := range bs[i] { |
| 142 |
if b.Start { |
| 143 |
n := 0 |
| 144 |
if b.Count > 0 { |
| 145 |
n = int(math.Floor(b.Norm*4)) + 1 |
| 146 |
} |
| 147 |
fmt.Fprintf(w, `<span class="cov%v" title="%v">`, n, b.Count) |
| 148 |
} else { |
| 149 |
w.WriteString("</span>") |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
switch b := src[i]; b { |
| 154 |
case '>': |
| 155 |
w.WriteString(">") |
| 156 |
case '<': |
| 157 |
w.WriteString("<") |
| 158 |
case '&': |
| 159 |
w.WriteString("&") |
| 160 |
case '\t': |
| 161 |
w.WriteString(" ") |
| 162 |
default: |
| 163 |
w.WriteByte(b) |
| 164 |
} |
| 165 |
} |
| 166 |
return template.HTML(w.String()) |
| 167 |
} |
| 168 |
|
| 169 |
const htmlTmpl = `<!DOCTYPE html> |
| 170 |
<html> |
| 171 |
<head> |
| 172 |
<meta charset="utf-8"> |
| 173 |
<title>{{.Title}}</title> |
| 174 |
|
| 175 |
<style> |
| 176 |
:root { |
| 177 |
--txt: #333; |
| 178 |
--bg: white; |
| 179 |
--a: #1c3986; |
| 180 |
--a-hover: #069; |
| 181 |
--odd-bg: #f5f5f7; |
| 182 |
--pre-bg: #f8f8f8; |
| 183 |
--pre-border: #e5e5e5; |
| 184 |
|
| 185 |
// color palette from graphiq. |
| 186 |
--cov0: red; |
| 187 |
--cov1: #0b7bab; |
| 188 |
--cov2: #09639b; |
| 189 |
--cov3: #034a8b; |
| 190 |
--cov4: #00337c; |
| 191 |
--cov5: #032663; |
| 192 |
} |
| 193 |
|
| 194 |
@media (prefers-color-scheme: dark) { |
| 195 |
:root { |
| 196 |
--txt: rgba(255, 255, 255, 0.90); |
| 197 |
--bg: #121212; |
| 198 |
--a: #44b4ec; |
| 199 |
--a-hover: #7fc9ee; |
| 200 |
--odd-bg: #222222; |
| 201 |
--pre-bg: #111; |
| 202 |
--pre-border: #333; |
| 203 |
--cov0: #f2a359; |
| 204 |
--cov1: #2b84b1; |
| 205 |
--cov2: #3393c4; |
| 206 |
--cov3: #3ca3d7; |
| 207 |
--cov4: #44b3eb; |
| 208 |
--cov5: #4dc3ff; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
body { |
| 213 |
font: 100%/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, |
| 214 |
Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", |
| 215 |
"Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", |
| 216 |
"Segoe UI Emoji", "Segoe UI Symbol"; |
| 217 |
color: var(--txt); |
| 218 |
background: var(--bg); |
| 219 |
} |
| 220 |
|
| 221 |
h1 { |
| 222 |
margin: 0 0 0.5em; |
| 223 |
} |
| 224 |
|
| 225 |
a { |
| 226 |
color: var(--a); |
| 227 |
text-decoration: none; |
| 228 |
cursor: pointer; |
| 229 |
} |
| 230 |
|
| 231 |
a:hover { |
| 232 |
color: var(--a-hover); |
| 233 |
} |
| 234 |
|
| 235 |
table { |
| 236 |
border-collapse: collapse; |
| 237 |
} |
| 238 |
|
| 239 |
tr:nth-child(odd) { |
| 240 |
background: var(--odd-bg); |
| 241 |
} |
| 242 |
|
| 243 |
tr.total { |
| 244 |
border-top: 1px solid; |
| 245 |
font-weight: bold; |
| 246 |
} |
| 247 |
|
| 248 |
td { |
| 249 |
padding: 0.2em 1em; |
| 250 |
} |
| 251 |
|
| 252 |
td.pcnt { |
| 253 |
text-align: right; |
| 254 |
} |
| 255 |
|
| 256 |
code, pre, tt { |
| 257 |
font-family: Monaco, Bitstream Vera Sans Mono, Lucida Console, |
| 258 |
Terminal, Consolas, Liberation Mono, DejaVu Sans Mono, |
| 259 |
Courier New, monospace; |
| 260 |
color: var(--txt); |
| 261 |
} |
| 262 |
|
| 263 |
pre { |
| 264 |
padding: 0.5em 0.8em; |
| 265 |
background: var(--pre-bg); |
| 266 |
border-radius: 1em; |
| 267 |
border:1px solid var(--pre-border); |
| 268 |
overflow-x: auto; |
| 269 |
} |
| 270 |
|
| 271 |
.cov0 { color: var(--cov0); } |
| 272 |
.cov1 { color: var(--cov1); } |
| 273 |
.cov2 { color: var(--cov2); } |
| 274 |
.cov3 { color: var(--cov3); } |
| 275 |
.cov4 { color: var(--cov4); } |
| 276 |
.cov5 { color: var(--cov5); } |
| 277 |
</style> |
| 278 |
|
| 279 |
<script> |
| 280 |
function visible(id) { |
| 281 |
history.replaceState(undefined, undefined, "#" + id); |
| 282 |
var all = document.getElementsByClassName("file"); |
| 283 |
for (var i = 0; i < all.length; i++) { |
| 284 |
var elem = all.item(i); |
| 285 |
elem.style.display = "none"; |
| 286 |
} |
| 287 |
var chosen = document.getElementById(id); |
| 288 |
chosen.style.display = "block"; |
| 289 |
} |
| 290 |
|
| 291 |
window.onload = function() { |
| 292 |
var id = window.location.hash.replace("#", ""); |
| 293 |
if (id != "") { |
| 294 |
visible(id); |
| 295 |
} |
| 296 |
}; |
| 297 |
</script> |
| 298 |
</head> |
| 299 |
|
| 300 |
<body> |
| 301 |
<h1>{{.Title}}</h1> |
| 302 |
|
| 303 |
{{.Notes}}<p> |
| 304 |
|
| 305 |
<table> |
| 306 |
{{range .Files}} |
| 307 |
<tr> |
| 308 |
<td><a onclick="visible('f::{{.}}')" tabindex="0"> {{.}} </a></td> |
| 309 |
<td class="pcnt">{{$.Totals.Percent . | printf "%.1f%%"}}</td> |
| 310 |
</tr> |
| 311 |
{{- end}} |
| 312 |
|
| 313 |
<tr class="total"> |
| 314 |
<td>Total</td> |
| 315 |
<td class="pcnt">{{.Totals.TotalPercent | printf "%.1f"}}%</td> |
| 316 |
</tr> |
| 317 |
</table> |
| 318 |
|
| 319 |
<div id="source"> |
| 320 |
{{range .Files}} |
| 321 |
<pre class="file" id="f::{{.}}" style="display: none">{{index $.Code .}}</pre> |
| 322 |
{{end}} |
| 323 |
</div> |
| 324 |
|
| 325 |
</body> |
| 326 |
</html> |
| 327 |
` |