Commit 22705d09 authored by Alberto García Hierro's avatar Alberto García Hierro Committed by Russ Cox

cmd/go: add support for coverage in CgoFiles

Add CgoFiles to the covered files when building
with cover support.

LGTM=rsc
R=golang-codereviews, gobot, r, rsc
CC=golang-codereviews
https://golang.org/cl/34680044
parent 3f1374fc
......@@ -813,25 +813,7 @@ func (b *builder) build(a *action) (err error) {
var gofiles, cfiles, sfiles, objects, cgoObjects []string
// If we're doing coverage, preprocess the .go files and put them in the work directory
if a.p.coverMode != "" {
for _, file := range a.p.GoFiles {
sourceFile := filepath.Join(a.p.Dir, file)
cover := a.p.coverVars[file]
if cover == nil || isTestFile(file) {
// Not covering this file.
gofiles = append(gofiles, file)
continue
}
coverFile := filepath.Join(obj, file)
if err := b.cover(a, coverFile, sourceFile, 0644, cover.Var); err != nil {
return err
}
gofiles = append(gofiles, coverFile)
}
} else {
gofiles = append(gofiles, a.p.GoFiles...)
}
gofiles = append(gofiles, a.p.GoFiles...)
cfiles = append(cfiles, a.p.CFiles...)
sfiles = append(sfiles, a.p.SFiles...)
......@@ -888,6 +870,35 @@ func (b *builder) build(a *action) (err error) {
gofiles = append(gofiles, outGo...)
}
// If we're doing coverage, preprocess the .go files and put them in the work directory
if a.p.coverMode != "" {
for i, file := range gofiles {
var sourceFile string
var coverFile string
var key string
if strings.HasSuffix(file, ".cgo1.go") {
// cgo files have absolute paths
base := filepath.Base(file)
sourceFile = file
coverFile = filepath.Join(obj, base)
key = strings.TrimSuffix(base, ".cgo1.go") + ".go"
} else {
sourceFile = filepath.Join(a.p.Dir, file)
coverFile = filepath.Join(obj, file)
key = file
}
cover := a.p.coverVars[key]
if cover == nil || isTestFile(file) {
// Not covering this file.
continue
}
if err := b.cover(a, coverFile, sourceFile, 0666, cover.Var); err != nil {
return err
}
gofiles[i] = coverFile
}
}
// Prepare Go import path list.
inc := b.includeArgs("-I", a.deps)
......
......@@ -415,7 +415,10 @@ func runTest(cmd *Command, args []string) {
p.Stale = true // rebuild
p.fake = true // do not warn about rebuild
p.coverMode = testCoverMode
p.coverVars = declareCoverVars(p.ImportPath, p.GoFiles...)
var coverFiles []string
coverFiles = append(coverFiles, p.GoFiles...)
coverFiles = append(coverFiles, p.CgoFiles...)
p.coverVars = declareCoverVars(p.ImportPath, coverFiles...)
}
}
......@@ -622,7 +625,10 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
if localCover {
ptest.coverMode = testCoverMode
ptest.coverVars = declareCoverVars(ptest.ImportPath, ptest.GoFiles...)
var coverFiles []string
coverFiles = append(coverFiles, ptest.GoFiles...)
coverFiles = append(coverFiles, ptest.CgoFiles...)
ptest.coverVars = declareCoverVars(ptest.ImportPath, coverFiles...)
}
} else {
ptest = p
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment