From 6e489043611f67d983ac6958c1f02b898fc57241 Mon Sep 17 00:00:00 2001 From: Matyáš Kroupa Date: Apr 19 2022 08:49:10 +0000 Subject: Deduplicate CollectProjectDeps --- diff --git a/pkg/util/util.go b/pkg/util/util.go index 59381e9..4b54429 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -348,47 +348,42 @@ func (p *PackageInfoCollector) CollectProjectDeps(standard, skipSelf, withTests, func (p *PackageInfoCollector) BuildPackageTree(includeMain, withTests, testsOnly bool) ([]string, error) { // TODO(jchaloup): strip all main package unless explicitely requested var entryPoints []string - if testsOnly { + if testsOnly || withTests { for p, pkgInfo := range p.packageInfos { if len(pkgInfo.TestGoFiles) > 0 || len(pkgInfo.XTestGoFiles) > 0 { entryPoints = append(entryPoints, p) } } - } else { - if withTests { - for p, pkgInfo := range p.packageInfos { - if len(pkgInfo.TestGoFiles) > 0 || len(pkgInfo.XTestGoFiles) > 0 { - entryPoints = append(entryPoints, p) - } - } + if testsOnly { + return entryPoints, nil } + } - for pkgName, pkgInfo := range p.packageInfos { - // check package name of each file - var nonMainFiles []string - files := pkgInfo.GoFiles - files = append(files, pkgInfo.CgoFiles...) - for _, file := range files { - f, err := parser.ParseFile(token.NewFileSet(), path.Join(pkgInfo.Dir, file), nil, 0) - if err != nil { - return nil, err - } - if f.Name.Name == "main" && file != "doc.go" { - importsList := make([]string, 0) - for _, i := range f.Imports { - importsList = append(importsList, i.Path.Value[1:len(i.Path.Value)-1]) - } - p.mainFiles[path.Join(pkgInfo.ImportPath, file)] = importsList - } - - if !includeMain && f.Name.Name == "main" { - continue + for pkgName, pkgInfo := range p.packageInfos { + // check package name of each file + var nonMainFiles []string + files := pkgInfo.GoFiles + files = append(files, pkgInfo.CgoFiles...) + for _, file := range files { + f, err := parser.ParseFile(token.NewFileSet(), path.Join(pkgInfo.Dir, file), nil, 0) + if err != nil { + return nil, err + } + if f.Name.Name == "main" && file != "doc.go" { + importsList := make([]string, 0) + for _, i := range f.Imports { + importsList = append(importsList, i.Path.Value[1:len(i.Path.Value)-1]) } - nonMainFiles = append(nonMainFiles, file) + p.mainFiles[path.Join(pkgInfo.ImportPath, file)] = importsList } - if len(nonMainFiles) > 0 { - entryPoints = append(entryPoints, pkgName) + + if !includeMain && f.Name.Name == "main" { + continue } + nonMainFiles = append(nonMainFiles, file) + } + if len(nonMainFiles) > 0 { + entryPoints = append(entryPoints, pkgName) } }