github.com/tiagovtristao/plz@v13.4.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 "github.com/thought-machine/please/src/cli" 13 "github.com/thought-machine/please/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 Verbosity cli.Verbosity `short:"v" long:"verbosity" default:"warning" description:"Verbosity of output (higher number = more output)"` 21 Dir string `short:"d" long:"dir" description:"Directory to search for Go package files for coverage"` 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 ImportPath string `short:"i" long:"import_path" description:"Full import path to the package"` 26 Args struct { 27 Go string `positional-arg-name:"go" description:"Location of go command" required:"true"` 28 Sources []string `positional-arg-name:"sources" description:"Test source files" required:"true"` 29 } `positional-args:"true" required:"true"` 30 } 31 32 func main() { 33 cli.ParseFlagsOrDie("plz_go_test", &opts) 34 cli.InitLogging(opts.Verbosity) 35 coverVars, err := gotest.FindCoverVars(opts.Dir, opts.ImportPath, opts.Exclude, opts.Args.Sources) 36 if err != nil { 37 log.Fatalf("Error scanning for coverage: %s", err) 38 } 39 if err = gotest.WriteTestMain(opts.Package, opts.ImportPath, gotest.IsVersion18(opts.Args.Go), opts.Args.Sources, opts.Output, coverVars); err != nil { 40 log.Fatalf("Error writing test main: %s", err) 41 } 42 os.Exit(0) 43 }