Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
remove import
Browse files Browse the repository at this point in the history
  • Loading branch information
artemikhaylov committed Oct 2, 2024
1 parent e5e62ed commit 5aa5ffb
Showing 1 changed file with 23 additions and 41 deletions.
64 changes: 23 additions & 41 deletions gocov/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
"github.com/axw/gocov/gocovutil"
json "github.com/json-iterator/go"
"go/ast"
"go/build"
"go/parser"
"go/token"
modfile "golang.org/x/mod/modfile"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/cover"
"io"
"os"
"path"
"path/filepath"
"strings"
"sync"
Expand All @@ -48,12 +48,9 @@ func marshalJson(w io.Writer, packages []*gocov.Package) error {
return json.NewEncoder(w).Encode(struct{ Packages []*gocov.Package }{packages})
}

type packagesCache map[string]*build.Package

func ConvertProfiles(filenames ...string) ([]byte, error) {
var (
ps gocovutil.Packages
packages = make(packagesCache)
ps gocovutil.Packages
)

goModContent, err := os.ReadFile(goModFilename)
Expand Down Expand Up @@ -81,16 +78,17 @@ func ConvertProfiles(filenames ...string) ([]byte, error) {
go func() {
for _, p := range profiles {
p := p
filename, err := filepath.Abs(strings.TrimPrefix(strings.TrimPrefix(p.FileName, moduleName), "/"))
relativeFilepath := strings.TrimPrefix(strings.TrimPrefix(p.FileName, moduleName), "/")
absFilepath, err := filepath.Abs(relativeFilepath)
if err != nil {
workerErr = fmt.Errorf("getting absolute path of file %q: %w", filename, err)
workerErr = fmt.Errorf("getting absolute path of file %q: %w", absFilepath, err)
break
}
_, ok := processedProfiles[filename]
_, ok := processedProfiles[absFilepath]
if !ok {
processedProfiles[filename] = nil
processedProfiles[absFilepath] = nil
funcsChan <- func() error {
if err := converter.fillFindFuncs(mu, filename); err != nil {
if err := converter.fillFindFuncs(mu, absFilepath); err != nil {
return fmt.Errorf("fillFindFuncs: %w", err)
}
return nil
Expand All @@ -101,7 +99,7 @@ func ConvertProfiles(filenames ...string) ([]byte, error) {
if !ok {
processedDirs[dir] = nil
funcsChan <- func() error {
if err := converter.fillAllPackages(mu, packages, p); err != nil {
if err := converter.fillAllPackages(mu, p, moduleName); err != nil {
return fmt.Errorf("failed to fill all packages: %w", err)
}
return nil
Expand Down Expand Up @@ -141,7 +139,7 @@ func ConvertProfiles(filenames ...string) ([]byte, error) {
for _, p := range profiles {
p := p
errGr.Go(func() error {
if err := converter.convertProfile(mu, packages, p); err != nil {
if err := converter.convertProfile(p, moduleName); err != nil {
return fmt.Errorf("failed to process concurrently: %w", err)
}
return nil
Expand Down Expand Up @@ -175,11 +173,8 @@ type statement struct {

var cacheFindFuncs = make(map[string][]*FuncExtent)

func (c *converter) fillAllPackages(mu *sync.Mutex, packages packagesCache, p *cover.Profile) error {
_, pkgpath, err := findFile(mu, packages, p.FileName, true)
if err != nil {
return err
}
func (c *converter) fillAllPackages(mu *sync.Mutex, p *cover.Profile, moduleName string) error {
_, pkgpath := findFile(moduleName, p.FileName)

mu.Lock()
pkg := c.packages[pkgpath]
Expand All @@ -203,11 +198,8 @@ func (c *converter) fillFindFuncs(mu *sync.Mutex, filename string) error {
return nil
}

func (c *converter) convertProfile(mu *sync.Mutex, packages packagesCache, p *cover.Profile) error {
file, pkgpath, err := findFile(mu, packages, p.FileName, false)
if err != nil {
return err
}
func (c *converter) convertProfile(p *cover.Profile, moduleName string) error {
file, pkgpath := findFile(moduleName, p.FileName)
pkg, ok := c.packages[pkgpath]
if !ok {
return fmt.Errorf("not found package: %s", pkgpath)
Expand Down Expand Up @@ -259,25 +251,15 @@ func (c *converter) convertProfile(mu *sync.Mutex, packages packagesCache, p *co
}

// findFile finds the location of the named file in GOROOT, GOPATH etc.
func findFile(mu *sync.Mutex, packages packagesCache, file string, write bool) (filename, pkgpath string, err error) {
dir, file := filepath.Split(file)
if dir != "" {
dir = strings.TrimSuffix(dir, "/")
}
var pkg *build.Package
if !write {
pkg = packages[dir]
} else {
pkg, err = build.Import(dir, ".", build.FindOnly)
if err != nil {
return "", "", fmt.Errorf("can't find %q: %w", file, err)
}
mu.Lock()
packages[dir] = pkg
mu.Unlock()
}

return filepath.Join(pkg.Dir, file), pkg.ImportPath, nil
// moduleName - gitlab.ozon.ru/re/das/api
// importPath - gitlab.ozon.ru/re/das/api/internal/clients
// packageFileName - gitlab.ozon.ru/re/das/api/internal/clients/redis.go
// returns
// absolute path to redis.go on machine and gitlab.ozon.ru/re/das/api/internal/clients
func findFile(moduleName, packageFileName string) (string, string) {
importPath, _ := path.Split(packageFileName)
absPath, _ := filepath.Abs(strings.TrimPrefix(strings.TrimPrefix(packageFileName, moduleName), "/"))
return absPath, strings.TrimSuffix(importPath, "/")
}

// findFuncs parses the file and returns a slice of FuncExtent descriptors.
Expand Down

0 comments on commit 5aa5ffb

Please sign in to comment.