forked from mattn/goveralls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocov.go
144 lines (133 loc) · 2.81 KB
/
gocov.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"encoding/json"
"flag"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
)
type GocovResult struct {
Packages []struct {
Name string
Functions []struct {
Name string
File string
Start, End int
Statements []struct {
Start, End, Reached int
}
}
}
}
func runGocov() (io.ReadCloser, error) {
cmd := exec.Command("gocov")
args := []string{"gocov", "test"}
if *verbose {
args = append(args, "-v")
}
if *race {
args = append(args, "-race")
}
args = append(args, flag.Args()...)
if *pkg != "" {
args = append(args, *pkg)
}
cmd.Args = args
cmd.Stderr = os.Stderr
ret, err := cmd.Output()
if err != nil {
return nil, err
}
return ioutil.NopCloser(bytes.NewReader(ret)), nil
}
func loadGocov() (io.ReadCloser, error) {
if *gocovjson == "" {
return runGocov()
} else {
return os.Open(*gocovjson)
}
}
func parseGocov(cov io.ReadCloser) ([]*SourceFile, error) {
var result GocovResult
d := json.NewDecoder(cov)
err := d.Decode(&result)
if err != nil {
return nil, err
}
cov.Close()
sourceFileMap := map[string]*SourceFile{}
var rv []*SourceFile
// Find all the files and load their content
fileContent := map[string][]byte{}
for _, pkg := range result.Packages {
for _, fun := range pkg.Functions {
b, ok := fileContent[fun.File]
if !ok {
b, err = ioutil.ReadFile(fun.File)
if err != nil {
log.Printf("Error reading %v: %v (skipping)", fun.File, err)
continue
}
fileContent[fun.File] = b
// Count the lines
sf := &SourceFile{
Name: getCoverallsSourceFileName(fun.File),
Source: string(b),
Coverage: make([]interface{}, bytes.Count(b, []byte{'\n'})),
}
sourceFileMap[fun.File] = sf
rv = append(rv, sf)
}
sf := sourceFileMap[fun.File]
// First, mark all parts of a mentioned function as covered.
linenum := 0
for i := range b {
if i >= fun.End {
break
}
if b[i] == '\n' {
linenum++
}
if i >= fun.Start {
// Leaving off a newline at the end of
// the file can cause us to compute line
// numbers where there are not lines.
if linenum < len(sf.Coverage) {
sf.Coverage[linenum] = 1
}
}
}
// Then paint each statement as directed. This will mark misses.
for _, st := range fun.Statements {
linenum := 0
for i := range b {
if i >= st.End {
break
}
if b[i] == '\n' {
linenum++
}
if i >= st.Start {
sf.Coverage[linenum] = st.Reached
break // only count the statement start
}
}
}
}
}
return rv, nil
}
func getCoverageGocov() []*SourceFile {
r, err := loadGocov()
if err != nil {
log.Fatalf("Error loading gocov results: %v", err)
}
rv, err := parseGocov(r)
if err != nil {
log.Fatalf("Error parsing gocov: %v", err)
}
return rv
}