Skip to content

Commit

Permalink
Prepare package cache in advance (#84)
Browse files Browse the repository at this point in the history
prepara cache
  • Loading branch information
yangzuo0621 authored Mar 1, 2023
1 parent c422fcf commit 6654fa6
Show file tree
Hide file tree
Showing 4 changed files with 4,326 additions and 10 deletions.
49 changes: 39 additions & 10 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type Parser struct {

// Parse parses cover profiles into statements, and modify their state based on git changes.
func (parser *Parser) Parse(changes []*gittool.Change) (Packages, error) {
if err := parser.buildPackageCache(); err != nil {
parser.logger.WithError(err).Error("build package cache")
return nil, err
}

var result Packages

for _, coverProfile := range parser.coverProfileFiles {
Expand All @@ -65,6 +70,34 @@ func (parser *Parser) Parse(changes []*gittool.Change) (Packages, error) {
return result, nil
}

func (parser *Parser) buildPackageCache() error {

for _, coverFile := range parser.coverProfileFiles {
profiles, err := cover.ParseProfiles(coverFile)
if err != nil {
return err
}

for _, profile := range profiles {
dir, _ := filepath.Split(profile.FileName)
if dir != "" {
dir = strings.TrimSuffix(dir, "/")
}
_, ok := parser.packagesCache[dir]
if !ok {
pkg, err := build.Import(dir, ".", build.FindOnly)
if err != nil {
return err
}
parser.packagesCache[dir] = pkg
parser.packages[pkg.ImportPath] = &Package{Name: pkg.ImportPath}
}
}
}

return nil
}

// wrapper for Statement
type statement struct {
*Statement
Expand Down Expand Up @@ -183,11 +216,7 @@ func findFile(packages packagesCache, file string) (filename, pkgpath string, er
}
pkg, ok := packages[dir]
if !ok {
pkg, err = build.Import(dir, ".", build.FindOnly)
if err != nil {
return "", "", fmt.Errorf("can't find %q: %w", file, err)
}
packages[dir] = pkg
return "", "", fmt.Errorf("no package found for %s", file)
}

return filepath.Join(pkg.Dir, file), pkg.ImportPath, nil
Expand Down Expand Up @@ -408,11 +437,11 @@ func isCodeLine(line string) bool {
// It sort statements by startline first, then try to find first statement
// that line number is greater than or equals the startline of the statement.
// There are two edge cases:
// 1. When line number is less than all the statements, `Search` function will return 0,
// but there is no suitable statement, should return immediately.
// 2. Otherwise, `Search` function will return first statement that its startline is greater than changed line number,
// then statement of that position minus one is the statement we want,
// but still need to check whether the changed line is among the statement scope.
// 1. When line number is less than all the statements, `Search` function will return 0,
// but there is no suitable statement, should return immediately.
// 2. Otherwise, `Search` function will return first statement that its startline is greater than changed line number,
// then statement of that position minus one is the statement we want,
// but still need to check whether the changed line is among the statement scope.
func (parser *Parser) setStatementsStateByLineNumber(changedlineNumber int, statements []*statement) {
idx := sort.Search(len(statements), func(i int) bool {
return statements[i].startLine > changedlineNumber
Expand Down
30 changes: 30 additions & 0 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ import (
"github.com/sirupsen/logrus"
)

func TestParser(t *testing.T) {
t.Run("buildPackageCache", func(t *testing.T) {
parser := &Parser{
coverProfileFiles: []string{"testdata/cover.out"},
packages: make(map[string]*Package),
packagesCache: make(packagesCache),
logger: logrus.New(),
}

parser.buildPackageCache()
for _, pkg := range []string{
"github.com/Azure/gocover/pkg/parser",
"github.com/Azure/gocover/pkg/report",
"github.com/Azure/gocover/pkg/gocover",
"github.com/Azure/gocover/pkg/gittool",
"github.com/Azure/gocover/pkg/dbclient",
"github.com/Azure/gocover/pkg/cmd",
"github.com/Azure/gocover/pkg/annotation",
"github.com/Azure/gocover",
} {
if _, ok := parser.packagesCache[pkg]; !ok {
t.Errorf("package %s is not in packagesCache", pkg)
}
if _, ok := parser.packages[pkg]; !ok {
t.Errorf("package %s is not in packages", pkg)
}
}
})
}

func TestSetStatementsState(t *testing.T) {
t.Run("change is nil", func(t *testing.T) {
parser := &Parser{}
Expand Down
8 changes: 8 additions & 0 deletions pkg/parser/testdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## How to prepare test data

In the root folder of the project, run the following command:

```bash
$ go test ./... -coverprofile cover.out -coverpkg=./... cover.out
$ mv cover.out pkg/parser/testdata/
```
Loading

0 comments on commit 6654fa6

Please sign in to comment.