]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add support for coverage in CgoFiles
authorAlberto GarcĂ­a Hierro <alberto@garciahierro.com>
Wed, 5 Mar 2014 19:28:11 +0000 (14:28 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 5 Mar 2014 19:28:11 +0000 (14:28 -0500)
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

src/cmd/go/build.go
src/cmd/go/test.go

index bf30be70e4d34ab1c534aae32fd74e4a79c4b46b..6966a4e9128630c0f4560c3dc04419d280ffcedd 100644 (file)
@@ -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)
 
index a6fe19d2cb517e778565576c795b8c980f905157..3344f0e5b86c3c6c31a619f632a6e4166a015424 100644 (file)
@@ -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