#24 Add option for querying main and test simultaneously.
Merged 4 years ago by qulogic. Opened 4 years ago by qulogic.
qulogic/golist with-tests  into  master

file modified
+15 -3
@@ -61,7 +61,11 @@ 

  		},

  		cli.BoolFlag{

  			Name:  "tests",

- 			Usage: "Apply the listing options over tests",

+ 			Usage: "Apply the listing options over tests only",

+ 		},

+ 		cli.BoolFlag{

+ 			Name:  "with-tests",

+ 			Usage: "Apply the listing options over both the main package and the tests",

  		},

  		cli.BoolFlag{

  			Name:  "show-main",
@@ -147,7 +151,11 @@ 

  		}

  

  		if c.Bool("provided") {

- 			pkgs, err := collector.BuildPackageTree(c.Bool("show-main"), c.Bool("tests"))

+ 			if c.Bool("with-tests") && c.Bool("tests") {

+ 				return fmt.Errorf("Both --with-tests and --tests cannot be set at the same time")

+ 			}

+ 

+ 			pkgs, err := collector.BuildPackageTree(c.Bool("show-main"), c.Bool("with-tests"), c.Bool("tests"))

  			if err != nil {

  				return err

  			}
@@ -162,7 +170,11 @@ 

  		}

  

  		if c.Bool("imported") {

- 			pkgs, err := collector.CollectProjectDeps(c.Bool("all-deps"), c.Bool("skip-self"), c.Bool("tests"))

+ 			if c.Bool("with-tests") && c.Bool("tests") {

+ 				return fmt.Errorf("Both --with-tests and --tests cannot be set at the same time")

+ 			}

+ 

+ 			pkgs, err := collector.CollectProjectDeps(c.Bool("all-deps"), c.Bool("skip-self"), c.Bool("with-tests"), c.Bool("tests"))

  			if err != nil {

  				return err

  			}

file modified
+15 -5
@@ -208,7 +208,7 @@ 

  	var err error

  

  	// Get provided packages

- 	if data.Packages, err = p.BuildPackageTree(false, false); err != nil {

+ 	if data.Packages, err = p.BuildPackageTree(false, false, false); err != nil {

  		return nil, err

  	}

  	sort.Strings(data.Packages)
@@ -285,13 +285,15 @@ 

  	return resources, nil

  }

  

- func (p *PackageInfoCollector) CollectProjectDeps(standard bool, skipSelf bool, tests bool) ([]string, error) {

+ func (p *PackageInfoCollector) CollectProjectDeps(standard, skipSelf, withTests, testsOnly bool) ([]string, error) {

  	imports := make(map[string]struct{})

  

  	for _, info := range p.packageInfos {

  		var pkgImports []string

- 		if tests {

+ 		if testsOnly {

  			pkgImports = info.TestImports

+ 		} else if withTests {

+ 			pkgImports = append(info.Imports, info.TestImports...)

  		} else {

  			pkgImports = info.Imports

  		}
@@ -331,16 +333,24 @@ 

  	return pkgs, nil

  }

  

- func (p *PackageInfoCollector) BuildPackageTree(includeMain bool, tests bool) ([]string, error) {

+ func (p *PackageInfoCollector) BuildPackageTree(includeMain, withTests, testsOnly bool) ([]string, error) {

  	// TODO(jchaloup): strip all main package unless explicitely requested

  	var entryPoints []string

- 	if tests {

+ 	if testsOnly {

  		for p, pkgInfo := range p.packageInfos {

  			if len(pkgInfo.TestGoFiles) > 0 {

  				entryPoints = append(entryPoints, p)

  			}

  		}

  	} else {

+ 		if withTests {

+ 			for p, pkgInfo := range p.packageInfos {

+ 				if len(pkgInfo.TestGoFiles) > 0 {

+ 					entryPoints = append(entryPoints, p)

+ 				}

+ 			}

+ 		}

+ 

  		for pkgName, pkgInfo := range p.packageInfos {

  			// check package name of each file

  			var nonMainFiles []string

--with-tests (as suggested in the original issue) is a fine API with me. I don’t have better ideas.

Please merge

Pull-Request has been merged by qulogic

4 years ago