2013-06-14 16:53:10 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-07-09 00:58:15 -06:00
|
|
|
// TODO(adg): set-specific legend
|
|
|
|
|
2013-06-14 16:53:10 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2013-07-06 00:58:50 -06:00
|
|
|
"bytes"
|
2013-06-14 16:53:10 -06:00
|
|
|
"fmt"
|
|
|
|
"go/build"
|
2013-07-06 00:58:50 -06:00
|
|
|
"html/template"
|
2013-06-14 16:53:10 -06:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-07-04 21:53:09 -06:00
|
|
|
"math"
|
2013-06-14 16:53:10 -06:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// htmlOutput reads the profile data from profile and generates an HTML
|
|
|
|
// coverage report, writing it to outfile. If outfile is empty,
|
|
|
|
// it writes the report to a temporary file and opens it in a web browser.
|
|
|
|
func htmlOutput(profile, outfile string) error {
|
|
|
|
pf, err := os.Open(profile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pf.Close()
|
|
|
|
|
|
|
|
profiles, err := ParseProfiles(pf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-06 00:58:50 -06:00
|
|
|
var files []*templateFile
|
2013-06-14 16:53:10 -06:00
|
|
|
|
|
|
|
for fn, profile := range profiles {
|
|
|
|
dir, file := filepath.Split(fn)
|
|
|
|
pkg, err := build.Import(dir, ".", build.FindOnly)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't find %q: %v", fn, err)
|
|
|
|
}
|
|
|
|
src, err := ioutil.ReadFile(filepath.Join(pkg.Dir, file))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't read %q: %v", fn, err)
|
|
|
|
}
|
2013-07-06 00:58:50 -06:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = htmlGen(&buf, src, profile.Tokens(src))
|
2013-06-14 16:53:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-06 00:58:50 -06:00
|
|
|
files = append(files, &templateFile{
|
|
|
|
Name: fn,
|
|
|
|
Body: template.HTML(buf.String()),
|
|
|
|
})
|
2013-06-14 16:53:10 -06:00
|
|
|
}
|
|
|
|
|
2013-07-06 00:58:50 -06:00
|
|
|
var out *os.File
|
|
|
|
if outfile == "" {
|
|
|
|
var dir string
|
|
|
|
dir, err = ioutil.TempDir("", "cover")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out, err = os.Create(filepath.Join(dir, "coverage.html"))
|
|
|
|
} else {
|
|
|
|
out, err = os.Create(outfile)
|
|
|
|
}
|
|
|
|
err = htmlTemplate.Execute(out, templateData{Files: files})
|
|
|
|
if err == nil {
|
|
|
|
err = out.Close()
|
|
|
|
}
|
2013-06-14 16:53:10 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if outfile == "" {
|
|
|
|
if !startBrowser("file://" + out.Name()) {
|
2013-06-19 10:11:32 -06:00
|
|
|
fmt.Fprintf(os.Stderr, "HTML output written to %s\n", out.Name())
|
2013-06-14 16:53:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profile represents the profiling data for a specific file.
|
|
|
|
type Profile struct {
|
|
|
|
Blocks []ProfileBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProfileBlock represents a single block of profiling data.
|
|
|
|
type ProfileBlock struct {
|
|
|
|
StartLine, StartCol int
|
|
|
|
EndLine, EndCol int
|
|
|
|
NumStmt, Count int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseProfiles parses profile data from the given Reader and returns a
|
|
|
|
// Profile for each file.
|
|
|
|
func ParseProfiles(r io.Reader) (map[string]*Profile, error) {
|
|
|
|
files := make(map[string]*Profile)
|
2013-06-21 15:19:57 -06:00
|
|
|
buf := bufio.NewReader(r)
|
|
|
|
// First line is mode.
|
|
|
|
mode, err := buf.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_ = mode // TODO: Use the mode to affect the display.
|
|
|
|
// Rest of file is in the format
|
|
|
|
// encoding/base64/base64.go:34.44,37.40 3 1
|
|
|
|
// where the fields are: name.go:line.column,line.column numberOfStatements count
|
|
|
|
s := bufio.NewScanner(buf)
|
2013-06-14 16:53:10 -06:00
|
|
|
for s.Scan() {
|
|
|
|
line := s.Text()
|
|
|
|
m := lineRe.FindStringSubmatch(line)
|
|
|
|
if m == nil {
|
|
|
|
return nil, fmt.Errorf("line %q doesn't match expected format: %v", m, lineRe)
|
|
|
|
}
|
|
|
|
fn := m[1]
|
|
|
|
p := files[fn]
|
|
|
|
if p == nil {
|
|
|
|
p = new(Profile)
|
|
|
|
files[fn] = p
|
|
|
|
}
|
|
|
|
p.Blocks = append(p.Blocks, ProfileBlock{
|
|
|
|
StartLine: toInt(m[2]),
|
|
|
|
StartCol: toInt(m[3]),
|
|
|
|
EndLine: toInt(m[4]),
|
|
|
|
EndCol: toInt(m[5]),
|
|
|
|
NumStmt: toInt(m[6]),
|
|
|
|
Count: toInt(m[7]),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, p := range files {
|
|
|
|
sort.Sort(blocksByStart(p.Blocks))
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type blocksByStart []ProfileBlock
|
|
|
|
|
|
|
|
func (b blocksByStart) Len() int { return len(b) }
|
|
|
|
func (b blocksByStart) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
func (b blocksByStart) Less(i, j int) bool {
|
|
|
|
return b[i].StartLine < b[j].StartLine || b[i].StartLine == b[j].StartLine && b[i].StartCol < b[j].StartCol
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineRe = regexp.MustCompile(`^(.+):([0-9]+).([0-9]+),([0-9]+).([0-9]+) ([0-9]+) ([0-9]+)$`)
|
|
|
|
|
|
|
|
func toInt(s string) int {
|
|
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return int(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token represents the position in a source file of an opening or closing
|
|
|
|
// <span> tag. These are used to colorize the source.
|
|
|
|
type Token struct {
|
|
|
|
Pos int
|
|
|
|
Start bool
|
|
|
|
Count int
|
2013-07-04 21:53:09 -06:00
|
|
|
Norm float64 // count normalized to 0-1
|
2013-06-14 16:53:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tokens returns a Profile as a set of Tokens within the provided src.
|
|
|
|
func (p *Profile) Tokens(src []byte) (tokens []Token) {
|
2013-07-04 21:53:09 -06:00
|
|
|
// Find maximum counts.
|
|
|
|
max := 0
|
|
|
|
for _, b := range p.Blocks {
|
|
|
|
if b.Count > max {
|
|
|
|
max = b.Count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Divisor for normalization.
|
2013-07-09 00:58:15 -06:00
|
|
|
divisor := math.Log(float64(max))
|
2013-07-04 21:53:09 -06:00
|
|
|
|
|
|
|
// tok returns a Token, populating the Norm field with a normalized Count.
|
|
|
|
tok := func(pos int, start bool, count int) Token {
|
|
|
|
t := Token{Pos: pos, Start: start, Count: count}
|
|
|
|
if !start || count == 0 {
|
|
|
|
return t
|
|
|
|
}
|
2013-07-09 00:58:15 -06:00
|
|
|
if max <= 1 {
|
2013-07-04 21:53:09 -06:00
|
|
|
t.Norm = 0.4 // "set" mode; use pale color
|
2013-07-09 00:58:15 -06:00
|
|
|
} else if count > 0 {
|
|
|
|
t.Norm = math.Log(float64(count)) / divisor
|
2013-07-04 21:53:09 -06:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
line, col := 1, 2
|
2013-06-14 16:53:10 -06:00
|
|
|
for si, bi := 0, 0; si < len(src) && bi < len(p.Blocks); {
|
|
|
|
b := p.Blocks[bi]
|
|
|
|
if b.StartLine == line && b.StartCol == col {
|
2013-07-04 21:53:09 -06:00
|
|
|
tokens = append(tokens, tok(si, true, b.Count))
|
2013-06-14 16:53:10 -06:00
|
|
|
}
|
|
|
|
if b.EndLine == line && b.EndCol == col {
|
2013-07-04 21:53:09 -06:00
|
|
|
tokens = append(tokens, tok(si, false, 0))
|
2013-06-14 16:53:10 -06:00
|
|
|
bi++
|
|
|
|
continue // Don't advance through src; maybe the next block starts here.
|
|
|
|
}
|
|
|
|
if src[si] == '\n' {
|
|
|
|
line++
|
|
|
|
col = 0
|
|
|
|
}
|
|
|
|
col++
|
|
|
|
si++
|
|
|
|
}
|
|
|
|
sort.Sort(tokensByPos(tokens))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type tokensByPos []Token
|
|
|
|
|
|
|
|
func (t tokensByPos) Len() int { return len(t) }
|
|
|
|
func (t tokensByPos) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
|
|
|
|
func (t tokensByPos) Less(i, j int) bool {
|
|
|
|
if t[i].Pos == t[j].Pos {
|
|
|
|
return !t[i].Start && t[j].Start
|
|
|
|
}
|
|
|
|
return t[i].Pos < t[j].Pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// htmlGen generates an HTML coverage report with the provided filename,
|
|
|
|
// source code, and tokens, and writes it to the given Writer.
|
2013-07-06 00:58:50 -06:00
|
|
|
func htmlGen(w io.Writer, src []byte, tokens []Token) error {
|
2013-06-14 16:53:10 -06:00
|
|
|
dst := bufio.NewWriter(w)
|
|
|
|
for i := range src {
|
|
|
|
for len(tokens) > 0 && tokens[0].Pos == i {
|
|
|
|
t := tokens[0]
|
|
|
|
if t.Start {
|
2013-07-06 00:58:50 -06:00
|
|
|
n := 0
|
2013-07-04 21:53:09 -06:00
|
|
|
if t.Count > 0 {
|
2013-07-09 00:58:15 -06:00
|
|
|
n = int(math.Floor(t.Norm*9)) + 1
|
2013-06-14 16:53:10 -06:00
|
|
|
}
|
2013-07-06 00:58:50 -06:00
|
|
|
fmt.Fprintf(dst, `<span class="cov%v" title="%v">`, n, t.Count)
|
2013-06-14 16:53:10 -06:00
|
|
|
} else {
|
|
|
|
dst.WriteString("</span>")
|
|
|
|
}
|
|
|
|
tokens = tokens[1:]
|
|
|
|
}
|
|
|
|
switch b := src[i]; b {
|
|
|
|
case '>':
|
|
|
|
dst.WriteString(">")
|
|
|
|
case '<':
|
|
|
|
dst.WriteString("<")
|
2013-07-06 00:58:50 -06:00
|
|
|
case '&':
|
|
|
|
dst.WriteString("&")
|
2013-06-14 16:53:10 -06:00
|
|
|
case '\t':
|
|
|
|
dst.WriteString(" ")
|
|
|
|
default:
|
|
|
|
dst.WriteByte(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// startBrowser tries to open the URL in a browser
|
|
|
|
// and returns whether it succeed.
|
|
|
|
func startBrowser(url string) bool {
|
|
|
|
// try to start the browser
|
|
|
|
var args []string
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
args = []string{"open"}
|
|
|
|
case "windows":
|
|
|
|
args = []string{"cmd", "/c", "start"}
|
|
|
|
default:
|
|
|
|
args = []string{"xdg-open"}
|
|
|
|
}
|
|
|
|
cmd := exec.Command(args[0], append(args[1:], url)...)
|
|
|
|
return cmd.Start() == nil
|
|
|
|
}
|
2013-07-06 00:58:50 -06:00
|
|
|
|
|
|
|
// rgb returns an rgb value for the specified coverage value
|
2013-07-09 00:58:15 -06:00
|
|
|
// between 0 (no coverage) and 10 (max coverage).
|
2013-07-06 00:58:50 -06:00
|
|
|
func rgb(n int) string {
|
|
|
|
if n == 0 {
|
2013-07-09 00:58:15 -06:00
|
|
|
return "rgb(192, 0, 0)" // Red
|
2013-07-06 00:58:50 -06:00
|
|
|
}
|
2013-07-09 00:58:15 -06:00
|
|
|
// Gradient from gray to green.
|
|
|
|
r := 128 - 12*(n-1)
|
|
|
|
g := 128 + 12*(n-1)
|
|
|
|
b := 128 + 3*(n-1)
|
2013-07-07 23:24:34 -06:00
|
|
|
return fmt.Sprintf("rgb(%v, %v, %v)", r, g, b)
|
2013-07-06 00:58:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// colors generates the CSS rules for coverage colors.
|
|
|
|
func colors() template.CSS {
|
|
|
|
var buf bytes.Buffer
|
2013-07-09 00:58:15 -06:00
|
|
|
for i := 0; i < 11; i++ {
|
2013-07-06 00:58:50 -06:00
|
|
|
fmt.Fprintf(&buf, ".cov%v { color: %v }\n", i, rgb(i))
|
|
|
|
}
|
|
|
|
return template.CSS(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
var htmlTemplate = template.Must(template.New("html").Funcs(template.FuncMap{
|
|
|
|
"colors": colors,
|
|
|
|
}).Parse(tmplHTML))
|
|
|
|
|
|
|
|
type templateData struct {
|
|
|
|
Files []*templateFile
|
|
|
|
}
|
|
|
|
|
|
|
|
type templateFile struct {
|
|
|
|
Name string
|
|
|
|
Body template.HTML
|
|
|
|
}
|
|
|
|
|
|
|
|
const tmplHTML = `
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<style>
|
2013-07-07 23:24:34 -06:00
|
|
|
body {
|
2013-07-09 00:58:15 -06:00
|
|
|
background: black;
|
|
|
|
color: rgb(80, 80, 80);
|
2013-07-07 23:24:34 -06:00
|
|
|
}
|
2013-07-09 21:22:04 -06:00
|
|
|
body, pre, #legend span {
|
|
|
|
font-family: Menlo, monospace;
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
#topbar {
|
|
|
|
background: black;
|
|
|
|
position: fixed;
|
|
|
|
top: 0; left: 0; right: 0;
|
|
|
|
height: 42px;
|
|
|
|
border-bottom: 1px solid rgb(80, 80, 80);
|
|
|
|
}
|
|
|
|
#content {
|
|
|
|
margin-top: 50px;
|
|
|
|
}
|
|
|
|
#nav, #legend {
|
|
|
|
float: left;
|
|
|
|
margin-left: 10px;
|
|
|
|
}
|
2013-07-07 23:24:34 -06:00
|
|
|
#legend {
|
2013-07-09 21:22:04 -06:00
|
|
|
margin-top: 12px;
|
2013-07-07 23:24:34 -06:00
|
|
|
}
|
2013-07-09 21:22:04 -06:00
|
|
|
#nav {
|
|
|
|
margin-top: 10px;
|
2013-07-07 23:24:34 -06:00
|
|
|
}
|
|
|
|
#legend span {
|
|
|
|
margin: 0 5px;
|
|
|
|
}
|
2013-07-06 00:58:50 -06:00
|
|
|
{{colors}}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2013-07-09 21:22:04 -06:00
|
|
|
<div id="topbar">
|
|
|
|
<div id="nav">
|
|
|
|
<select id="files">
|
|
|
|
{{range $i, $f := .Files}}
|
|
|
|
<option value="file{{$i}}">{{$f.Name}}</option>
|
|
|
|
{{end}}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
<div id="legend">
|
2013-07-07 23:24:34 -06:00
|
|
|
<span>not tracked</span>
|
|
|
|
<span class="cov0">no coverage</span>
|
|
|
|
<span class="cov1">low coverage</span>
|
|
|
|
<span class="cov2">*</span>
|
|
|
|
<span class="cov3">*</span>
|
|
|
|
<span class="cov4">*</span>
|
|
|
|
<span class="cov5">*</span>
|
|
|
|
<span class="cov6">*</span>
|
|
|
|
<span class="cov7">*</span>
|
|
|
|
<span class="cov8">*</span>
|
|
|
|
<span class="cov9">*</span>
|
2013-07-09 00:58:15 -06:00
|
|
|
<span class="cov10">high coverage</span>
|
2013-07-07 23:24:34 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2013-07-09 21:22:04 -06:00
|
|
|
<div id="content">
|
2013-07-06 00:58:50 -06:00
|
|
|
{{range $i, $f := .Files}}
|
|
|
|
<pre class="file" id="file{{$i}}" {{if $i}}style="display: none"{{end}}>{{$f.Body}}</pre>
|
|
|
|
{{end}}
|
2013-07-09 21:22:04 -06:00
|
|
|
</div>
|
2013-07-06 00:58:50 -06:00
|
|
|
</body>
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
var files = document.getElementById('files');
|
|
|
|
var visible = document.getElementById('file0');
|
|
|
|
files.addEventListener('change', onChange, false);
|
|
|
|
function onChange() {
|
|
|
|
visible.style.display = 'none';
|
|
|
|
visible = document.getElementById(files.value);
|
|
|
|
visible.style.display = 'block';
|
2013-07-09 21:22:04 -06:00
|
|
|
window.scrollTo(0, 0);
|
2013-07-06 00:58:50 -06:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
</script>
|
|
|
|
</html>
|
|
|
|
`
|