github.phpd.cn/thought-machine/please@v12.2.0+incompatible/tools/please_go_test/plz_go_test.go (about)

     1  // Package main implements plz_go_test, a code templater for go tests.
     2  // This is essentially equivalent to what 'go test' does but it lifts some restrictions
     3  // on file organisation and allows the code to be instrumented for coverage as separate
     4  // build targets rather than having to repeat it for every test.
     5  package main
     6  
     7  import (
     8  	"os"
     9  
    10  	"gopkg.in/op/go-logging.v1"
    11  
    12  	"cli"
    13  	"tools/please_go_test/gotest"
    14  )
    15  
    16  var log = logging.MustGetLogger("plz_go_test")
    17  
    18  var opts struct {
    19  	Usage     string   `usage:"please_go_test is a code templater for Go tests.\n\nIt writes out the test main file required for each test, similar to what 'go test' does but as a separate tool that Please can invoke."`
    20  	Dir       string   `short:"d" long:"dir" description:"Directory to search for Go package files for coverage"`
    21  	Verbosity int      `short:"v" long:"verbose" default:"1" description:"Verbosity of output (higher number = more output, default 1 -> warnings and errors only)"`
    22  	Exclude   []string `short:"x" long:"exclude" default:"third_party/go" description:"Directories to exclude from search"`
    23  	Output    string   `short:"o" long:"output" description:"Output filename" required:"true"`
    24  	Package   string   `short:"p" long:"package" description:"Package containing this test" env:"PKG"`
    25  	Args      struct {
    26  		Go      string   `positional-arg-name:"go" description:"Location of go command" required:"true"`
    27  		Sources []string `positional-arg-name:"sources" description:"Test source files" required:"true"`
    28  	} `positional-args:"true" required:"true"`
    29  }
    30  
    31  func main() {
    32  	cli.ParseFlagsOrDie("plz_go_test", "7.2.0", &opts)
    33  	cli.InitLogging(opts.Verbosity)
    34  	coverVars, err := gotest.FindCoverVars(opts.Dir, opts.Exclude, opts.Args.Sources)
    35  	if err != nil {
    36  		log.Fatalf("Error scanning for coverage: %s", err)
    37  	}
    38  	if err = gotest.WriteTestMain(opts.Package, gotest.IsVersion18(opts.Args.Go), opts.Args.Sources, opts.Output, coverVars); err != nil {
    39  		log.Fatalf("Error writing test main: %s", err)
    40  	}
    41  	os.Exit(0)
    42  }