-
Notifications
You must be signed in to change notification settings - Fork 0
/
cover.go
137 lines (127 loc) · 4.12 KB
/
cover.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
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"flag"
"fmt"
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"strconv"
)
// cover transforms a source file with "go tool cover". It is invoked by the
// Go rules as an action.
func cover(args []string) error {
args, err := expandParamsFiles(args)
if err != nil {
return err
}
flags := flag.NewFlagSet("cover", flag.ExitOnError)
var coverSrc, coverVar, origSrc, srcName, mode string
flags.StringVar(&coverSrc, "o", "", "coverage output file")
flags.StringVar(&coverVar, "var", "", "name of cover variable")
flags.StringVar(&origSrc, "src", "", "original source file")
flags.StringVar(&srcName, "srcname", "", "source name printed in coverage data")
flags.StringVar(&mode, "mode", "set", "coverage mode to use")
goenv := envFlags(flags)
if err := flags.Parse(args); err != nil {
return err
}
if err := goenv.checkFlags(); err != nil {
return err
}
if coverSrc == "" {
return fmt.Errorf("-o was not set")
}
if coverVar == "" {
return fmt.Errorf("-var was not set")
}
if origSrc == "" {
return fmt.Errorf("-src was not set")
}
if srcName == "" {
srcName = origSrc
}
return instrumentForCoverage(goenv, origSrc, srcName, coverVar, mode, coverSrc)
}
// instrumentForCoverage runs "go tool cover" on a source file to produce
// a coverage-instrumented version of the file. It also registers the file
// with the coverdata package.
func instrumentForCoverage(goenv *env, srcPath, srcName, coverVar, mode, outPath string) error {
goargs := goenv.goTool("cover", "-var", coverVar, "-mode", mode, "-o", outPath, srcPath)
if err := goenv.runCommand(goargs); err != nil {
return err
}
return registerCoverage(outPath, coverVar, srcName)
}
// registerCoverage modifies coverSrc, the output file from go tool cover. It
// adds a call to coverdata.RegisterCoverage, which ensures the coverage
// data from each file is reported. The name by which the file is registered
// need not match its original name (it may use the importpath).
func registerCoverage(coverSrc, varName, srcName string) error {
// Parse the file.
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, coverSrc, nil, parser.ParseComments)
if err != nil {
return nil // parse error: proceed and let the compiler fail
}
// Ensure coverdata is imported in the AST. Use an existing import if present
// or add a new one.
const coverdataPath = "github.com/bazelbuild/rules_go/go/tools/coverdata"
var coverdataName string
for _, imp := range f.Imports {
path, err := strconv.Unquote(imp.Path.Value)
if err != nil {
return nil // parse error: proceed and let the compiler fail
}
if path == coverdataPath {
if imp.Name != nil {
// renaming import
if imp.Name.Name == "_" {
// Change blank import to named import
imp.Name.Name = "coverdata"
}
coverdataName = imp.Name.Name
} else {
// default import
coverdataName = "coverdata"
}
break
}
}
if coverdataName == "" {
// No existing import. Add a new one.
coverdataName = "coverdata"
addNamedImport(fset, f, coverdataName, coverdataPath)
}
var buf bytes.Buffer
if err := format.Node(&buf, fset, f); err != nil {
return fmt.Errorf("registerCoverage: could not reformat coverage source %s: %v", coverSrc, err)
}
// Append an init function.
fmt.Fprintf(&buf, `
func init() {
%s.RegisterFile(%q,
%[3]s.Count[:],
%[3]s.Pos[:],
%[3]s.NumStmt[:])
}
`, coverdataName, srcName, varName)
if err := ioutil.WriteFile(coverSrc, buf.Bytes(), 0666); err != nil {
return fmt.Errorf("registerCoverage: %v", err)
}
return nil
}