github.com/gsquire/gb@v0.4.4-0.20161112235727-3982dc872064/cmd/gb/test.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "sort" 8 9 "github.com/constabulary/gb" 10 "github.com/constabulary/gb/cmd" 11 "github.com/constabulary/gb/internal/debug" 12 "github.com/constabulary/gb/test" 13 ) 14 15 func init() { 16 registerCommand(TestCmd) 17 } 18 19 var ( 20 tfs []string // Arguments passed to the test binary 21 testCover bool 22 testCoverMode string 23 testCoverPkg string 24 testVerbose bool // enable verbose output of test commands 25 testNope bool // do not execute test binaries, compile and link only 26 ) 27 28 func addTestFlags(fs *flag.FlagSet) { 29 addBuildFlags(fs) 30 fs.BoolVar(&testCover, "cover", false, "enable coverage analysis") 31 fs.StringVar(&testCoverMode, "covermode", "set", "Set covermode: set (default), count, atomic") 32 fs.StringVar(&testCoverPkg, "coverpkg", "", "enable coverage analysis") 33 fs.BoolVar(&testVerbose, "v", false, "enable verbose output of subcommands") 34 fs.BoolVar(&testNope, "n", false, "do not execute test binaries, compile only") 35 } 36 37 var TestCmd = &cmd.Command{ 38 Name: "test", 39 UsageLine: "test [build flags] -n -v [packages] [flags for test binary]", 40 Short: "test packages", 41 Long: ` 42 Test automates testing the packages named by the import paths. 43 44 'gb test' recompiles each package along with any files with names matching 45 the file pattern "*_test.go". 46 47 Flags: 48 49 -v 50 print output from test subprocess. 51 -n 52 do not execute test binaries, compile only 53 `, 54 Run: func(ctx *gb.Context, args []string) error { 55 ctx.Force = F 56 ctx.Install = !FF 57 ctx.Verbose = testVerbose 58 ctx.Nope = testNope 59 r := test.TestResolver(ctx) 60 61 // gb build builds packages in dependency order, however 62 // gb test tests packages in alpha order. This matches the 63 // expected behaviour from go test; tests are executed in 64 // stable order. 65 sort.Strings(args) 66 67 pkgs, err := resolveRootPackages(r, args...) 68 if err != nil { 69 return err 70 } 71 72 test, err := test.TestPackages(TestFlags(tfs), pkgs...) 73 if err != nil { 74 return err 75 } 76 77 if dotfile != "" { 78 f, err := os.Create(dotfile) 79 if err != nil { 80 return err 81 } 82 defer f.Close() 83 printActions(f, test) 84 } 85 86 startSigHandlers() 87 return gb.ExecuteConcurrent(test, P, interrupted) 88 }, 89 AddFlags: addTestFlags, 90 FlagParse: func(flags *flag.FlagSet, args []string) error { 91 var err error 92 debug.Debugf("%s", args) 93 args, tfs, err = TestFlagsExtraParse(args[2:]) 94 debug.Debugf("%s %s", args, tfs) 95 if err != nil { 96 fmt.Fprintf(os.Stderr, "gb test: %s\n", err) 97 fmt.Fprintf(os.Stderr, `run "go help test" or "go help testflag" for more information`+"\n") 98 exit(2) 99 } 100 return flags.Parse(args) 101 }, 102 }