]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: support type checking of external tests with gotype
authorRobert Griesemer <gri@golang.org>
Thu, 2 Mar 2017 01:15:43 +0000 (17:15 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 2 Mar 2017 22:41:26 +0000 (22:41 +0000)
- renamed -a flag to -t
- added -x flag to specify external test files
- improved documentation and usage string

Change-Id: I7c850bd28a10ceaa55d599c22db07774147aa3f7
Reviewed-on: https://go-review.googlesource.com/37656
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/gotype.go

index 14c3d6914d8434996a84eed8644f1cb1d0efd106..196fc9bbd604162de61eb3dff70149a0ae40797b 100644 (file)
@@ -7,30 +7,38 @@
 // Build this command explicitly: go build gotype.go
 
 /*
-The gotype command does syntactic and semantic analysis of Go files
-and packages like the front-end of a Go compiler. Errors are reported
-if the analysis fails; otherwise gotype is quiet (unless -v is set).
+The gotype command, like the front-end of a Go compiler, parses and
+type-checks a single Go package. Errors are reported if the analysis
+fails; otherwise gotype is quiet (unless -v is set).
 
 Without a list of paths, gotype reads from standard input, which
 must provide a single Go source file defining a complete package.
 
-If a single path is specified that is a directory, gotype checks
-the Go files in that directory; they must all belong to the same
-package.
+With a single directory argument, gotype checks the Go files in
+that directory, comprising a single package. Use -t to include the
+(in-package) _test.go files. Use -x to type check only external
+test files.
 
-Otherwise, each path must be the filename of Go file belonging to
-the same package.
+Otherwise, each path must be the filename of a Go file belonging
+to the same package.
 
 Imports are processed by importing directly from the source of
 imported packages (default), or by importing from compiled and
 installed packages (by setting -c to the respective compiler).
 
+The -c flag must be set to a compiler ("gc", "gccgo") when type-
+checking packages containing imports with relative import paths
+(import "./mypkg") because the source importer cannot know which
+files to include for such packages.
+
 Usage:
        gotype [flags] [path...]
 
 The flags are:
-       -a
-               use all (incl. _test.go) files when processing a directory
+       -t
+               include local test files in a directory (ignored if -x is provided)
+       -x
+               consider only external test files in a directory
        -e
                report all errors (not just the first 10)
        -v
@@ -52,13 +60,14 @@ To check the files a.go, b.go, and c.go:
 
        gotype a.go b.go c.go
 
-To check an entire package in the directory dir and print the processed files:
+To check an entire package including (in-package) tests in the directory dir and print the processed files:
 
-       gotype -v dir
+       gotype -t -v dir
 
-To check an entire package including tests in the local directory:
+To check the external test package (if any) in the current directory, based on installed packages compiled with
+cmd/compile:
 
-       gotype -a .
+       gotype -c=gc -x .
 
 To verify the output of a pipe:
 
@@ -86,12 +95,13 @@ import (
 
 var (
        // main operation modes
-       allFiles  = flag.Bool("a", false, "use all (incl. _test.go) files when processing a directory")
-       allErrors = flag.Bool("e", false, "report all errors (not just the first 10)")
-       verbose   = flag.Bool("v", false, "verbose mode")
-       compiler  = flag.String("c", "source", "compiler used for installed packages (gc, gccgo, or source)")
+       testFiles  = flag.Bool("t", false, "include in-package test files in a directory")
+       xtestFiles = flag.Bool("x", false, "consider only external test files in a directory")
+       allErrors  = flag.Bool("e", false, "report all errors, not just the first 10")
+       verbose    = flag.Bool("v", false, "verbose mode")
+       compiler   = flag.String("c", "source", "compiler used for installed packages (gc, gccgo, or source)")
 
-       // debugging support
+       // additional output control
        printAST      = flag.Bool("ast", false, "print AST (forces -seq)")
        printTrace    = flag.Bool("trace", false, "print parse trace (forces -seq)")
        parseComments = flag.Bool("comments", false, "parse comments (ignored unless -ast or -trace is provided)")
@@ -120,8 +130,35 @@ func initParserMode() {
        }
 }
 
+const usageString = `usage: gotype [flags] [path ...]
+
+The gotype command, like the front-end of a Go compiler, parses and
+type-checks a single Go package. Errors are reported if the analysis
+fails; otherwise gotype is quiet (unless -v is set).
+
+Without a list of paths, gotype reads from standard input, which
+must provide a single Go source file defining a complete package.
+
+With a single directory argument, gotype checks the Go files in
+that directory, comprising a single package. Use -t to include the
+(in-package) _test.go files. Use -x to type check only external
+test files.
+
+Otherwise, each path must be the filename of a Go file belonging
+to the same package.
+
+Imports are processed by importing directly from the source of
+imported packages (default), or by importing from compiled and
+installed packages (by setting -c to the respective compiler).
+
+The -c flag must be set to a compiler ("gc", "gccgo") when type-
+checking packages containing imports with relative import paths
+(import "./mypkg") because the source importer cannot know which
+files to include for such packages.
+`
+
 func usage() {
-       fmt.Fprintln(os.Stderr, "usage: gotype [flags] [path ...]")
+       fmt.Fprintln(os.Stderr, usageString)
        flag.PrintDefaults()
        os.Exit(2)
 }
@@ -188,11 +225,15 @@ func parseDir(dir string) ([]*ast.File, error) {
        if _, nogo := err.(*build.NoGoError); err != nil && !nogo {
                return nil, err
        }
+
+       if *xtestFiles {
+               return parseFiles(dir, pkginfo.XTestGoFiles)
+       }
+
        filenames := append(pkginfo.GoFiles, pkginfo.CgoFiles...)
-       if *allFiles {
+       if *testFiles {
                filenames = append(filenames, pkginfo.TestGoFiles...)
        }
-
        return parseFiles(dir, filenames)
 }