update
This commit is contained in:
parent
fcc160af52
commit
55fb63917d
@ -107,13 +107,4 @@ func TestBuildBinaryForInternalPackage(t *testing.T) {
|
|||||||
fInfo, err := os.Lstat(obj)
|
fInfo, err := os.Lstat(obj)
|
||||||
assert.Equal(t, err, nil, "the binary should be generated.")
|
assert.Equal(t, err, nil, "the binary should be generated.")
|
||||||
assert.Equal(t, startTime.Before(fInfo.ModTime()), true, obj+"new binary should be generated, not the old one")
|
assert.Equal(t, startTime.Before(fInfo.ModTime()), true, obj+"new binary should be generated, not the old one")
|
||||||
|
|
||||||
// cmd := exec.Command("go", "tool", "objdump", "simple-project")
|
|
||||||
// cmd.Dir = workingDir
|
|
||||||
// out, _ := cmd.CombinedOutput()
|
|
||||||
// cnt := strings.Count(string(out), "GoCacheCover")
|
|
||||||
// assert.Equal(t, cnt > 0, true, "GoCacheCover variable for internal package should be in the binary")
|
|
||||||
|
|
||||||
// cnt = strings.Count(string(out), "internal.GoCover")
|
|
||||||
// assert.Equal(t, cnt > 0, true, "internal.GoCover variable should be in the binary")
|
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ import (
|
|||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/qiniu/goc/pkg/cover/cover2tool"
|
"github.com/qiniu/goc/pkg/cover/internal/tool"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -180,13 +180,8 @@ func Execute(coverInfo *CoverInfo) error {
|
|||||||
if pkg.Name == "main" {
|
if pkg.Name == "main" {
|
||||||
log.Printf("handle package: %v", pkg.ImportPath)
|
log.Printf("handle package: %v", pkg.ImportPath)
|
||||||
// inject the main package
|
// inject the main package
|
||||||
mainCover, mainDecl, _ := addCounters2(pkg, mode, globalCoverVarImportPath)
|
mainCover, mainDecl := AddCounters(pkg, mode, globalCoverVarImportPath)
|
||||||
allDecl += mainDecl
|
allDecl += mainDecl
|
||||||
// if err != nil {
|
|
||||||
// log.Errorf("failed to add counters for pkg %s, err: %v", pkg.ImportPath, err)
|
|
||||||
// return ErrCoverPkgFailed
|
|
||||||
// }
|
|
||||||
|
|
||||||
// new a testcover for this service
|
// new a testcover for this service
|
||||||
tc := TestCover{
|
tc := TestCover{
|
||||||
Mode: mode,
|
Mode: mode,
|
||||||
@ -207,7 +202,7 @@ func Execute(coverInfo *CoverInfo) error {
|
|||||||
|
|
||||||
//only focus package neither standard Go library nor dependency library
|
//only focus package neither standard Go library nor dependency library
|
||||||
if depPkg, ok := pkgs[dep]; ok {
|
if depPkg, ok := pkgs[dep]; ok {
|
||||||
packageCover, depDecl, _ := addCounters2(depPkg, mode, globalCoverVarImportPath)
|
packageCover, depDecl := AddCounters(depPkg, mode, globalCoverVarImportPath)
|
||||||
allDecl += depDecl
|
allDecl += depDecl
|
||||||
tc.DepsCover = append(tc.DepsCover, packageCover)
|
tc.DepsCover = append(tc.DepsCover, packageCover)
|
||||||
seen[dep] = packageCover
|
seen[dep] = packageCover
|
||||||
@ -268,40 +263,22 @@ func ListPackages(dir string, args string, newgopath string) (map[string]*Packag
|
|||||||
return pkgs, nil
|
return pkgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCounters add counters for all go files under the package
|
// AddCounters is different from official go tool cover
|
||||||
func AddCounters(pkg *Package, mode, newgopath string) (*PackageCover, error) {
|
|
||||||
coverVarMap := declareCoverVars(pkg)
|
|
||||||
|
|
||||||
for file, coverVar := range coverVarMap {
|
|
||||||
cmd := buildCoverCmd(file, coverVar, pkg, mode, newgopath)
|
|
||||||
out, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("execute go tool cover -mode=%s -var %s -o %s/%s failed, err: %v, out: %s", mode, coverVar.Var, pkg.Dir, file, err, string(out))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &PackageCover{
|
|
||||||
Package: pkg,
|
|
||||||
Vars: coverVarMap,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// addCounters2 is different from official go tool cover
|
|
||||||
// 1. only inject covervar++ into source file
|
// 1. only inject covervar++ into source file
|
||||||
// 2. no declarartions for these covervars
|
// 2. no declarartions for these covervars
|
||||||
// 3. return the declarations as string
|
// 3. return the declarations as string
|
||||||
func addCounters2(pkg *Package, mode string, globalCoverVarImportPath string) (*PackageCover, string, error) {
|
func AddCounters(pkg *Package, mode string, globalCoverVarImportPath string) (*PackageCover, string) {
|
||||||
coverVarMap := declareCoverVars(pkg)
|
coverVarMap := declareCoverVars(pkg)
|
||||||
|
|
||||||
decl := ""
|
decl := ""
|
||||||
for file, coverVar := range coverVarMap {
|
for file, coverVar := range coverVarMap {
|
||||||
decl += "\n" + cover2tool.Annotate(path.Join(pkg.Dir, file), mode, coverVar.Var, globalCoverVarImportPath) + "\n"
|
decl += "\n" + tool.Annotate(path.Join(pkg.Dir, file), mode, coverVar.Var, globalCoverVarImportPath) + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
return &PackageCover{
|
return &PackageCover{
|
||||||
Package: pkg,
|
Package: pkg,
|
||||||
Vars: coverVarMap,
|
Vars: coverVarMap,
|
||||||
}, decl, nil
|
}, decl
|
||||||
}
|
}
|
||||||
|
|
||||||
func isDirExist(path string) bool {
|
func isDirExist(path string) bool {
|
||||||
|
@ -385,14 +385,4 @@ func TestCoverResultForInternalPackage(t *testing.T) {
|
|||||||
if !assert.Equal(t, err, nil) {
|
if !assert.Equal(t, err, nil) {
|
||||||
assert.FailNow(t, "should generate http_cover_apis_auto_generated.go")
|
assert.FailNow(t, "should generate http_cover_apis_auto_generated.go")
|
||||||
}
|
}
|
||||||
|
|
||||||
// out, err := ioutil.ReadFile(filepath.Join(testDir, "http_cover_apis_auto_generated.go"))
|
|
||||||
// if err != nil {
|
|
||||||
// assert.FailNow(t, "failed to read http_cover_apis_auto_generated.go file")
|
|
||||||
// }
|
|
||||||
// cnt := strings.Count(string(out), "GoCacheCover")
|
|
||||||
// assert.Equal(t, cnt > 0, true, "GoCacheCover variable should be in http_cover_apis_auto_generated.go")
|
|
||||||
|
|
||||||
// cnt = strings.Count(string(out), "example.com/simple-project/internal/foo.go")
|
|
||||||
// assert.Equal(t, cnt > 0, true, "`example.com/simple-project/internal/foo.go` should be in http_cover_apis_auto_generated.go")
|
|
||||||
}
|
}
|
||||||
|
@ -402,7 +402,10 @@ func injectGlobalCoverVarFile(ci *CoverInfo, content string) error {
|
|||||||
|
|
||||||
packageName := "package " + filepath.Base(ci.GlobalCoverVarImportPath) + "\n\n"
|
packageName := "package " + filepath.Base(ci.GlobalCoverVarImportPath) + "\n\n"
|
||||||
|
|
||||||
_, _ = coverFile.WriteString(packageName)
|
_, err = coverFile.WriteString(packageName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
_, err = coverFile.WriteString(content)
|
_, err = coverFile.WriteString(content)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cover2tool
|
package tool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@ -3,7 +3,7 @@
|
|||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package edit implements buffered position-based editing of byte slices.
|
// Package edit implements buffered position-based editing of byte slices.
|
||||||
package cover2tool
|
package tool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
Loading…
Reference in New Issue
Block a user