/* 递归目录的go test 支持.kmg.yml目录结构提示文件(该文件必须存在) -v 更详细的描述 -m 一个模块名,从这个模块名开始递归目录测试 -d 一个目录名,从这个目录开始递归目录测试 -bench benchmarks参数,直接传递到go test -onePackage 不递归目录测试,仅测试一个package */ func runGoTest() { command := GoTest{} flag.BoolVar(&command.v, "v", false, "show output of test") flag.StringVar(&command.dir, "d", "", "dir path to test") flag.StringVar(&command.moduleName, "m", "", "module name to test") flag.StringVar(&command.bench, "bench", "", "bench parameter pass to go test") flag.BoolVar(&command.onePackage, "onePackage", false, "only test one package") flag.StringVar(&command.runArg, "run", "", "Run only those tests and examples matching the regular expression.") flag.BoolVar(&command.onlyBuild, "onlyBuild", false, "only build all package(not test)") flag.Parse() kmgc, err := kmgConfig.LoadEnvFromWd() if err == nil { command.gopath = kmgc.GOPATH[0] } else { if kmgConfig.IsNotFound(err) { command.gopath = os.Getenv("GOPATH") } else { kmgConsole.ExitOnErr(err) } } //find root path root, err := command.findRootPath() kmgConsole.ExitOnErr(err) command.buildContext = &build.Context{ GOPATH: command.gopath, Compiler: build.Default.Compiler, } if command.onePackage { err = command.handlePath(root) kmgConsole.ExitOnErr(err) } err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { return nil } if kmgFile.IsDotFile(path) { return filepath.SkipDir } return command.handlePath(path) }) kmgConsole.ExitOnErr(err) }
func (command *GoTest) Execute(context *console.Context) (err error) { command.context = context kmgc, err := kmgConfig.LoadEnvFromWd() if err == nil { command.gopath = kmgc.GOPATH[0] } else { if kmgConfig.IsNotFound(err) { command.gopath = os.Getenv("GOPATH") } else { return } } //find root path root, err := command.findRootPath(context) if err != nil { return } command.buildContext = &build.Context{ GOPATH: command.gopath, Compiler: build.Default.Compiler, } if command.onePackage { return command.handlePath(root) } return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { return nil } if kmgFile.IsDotFile(path) { return filepath.SkipDir } return command.handlePath(path) }) }
func DefaultIsIgnorePath(path string) bool { return kmgFile.IsDotFile(path) }
func (command *GoAllImport) Execute(context *console.Context) (err error) { kmgc, err := kmgConfig.LoadEnvFromWd() if err != nil { return } command.gopath = kmgc.GOPATH[0] root := command.dir exist, err := kmgFile.FileExist(root) if err != nil { return } if !exist { return fmt.Errorf("[GoAllImport] dir path[%s] not exist", root) } c := &build.Context{ GOPATH: kmgc.GOPATHToString(), Compiler: build.Default.Compiler, } root, err = kmgFile.Realpath(root) if err != nil { return } moduleList := []string{} err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { return nil } if kmgFile.IsDotFile(path) { return filepath.SkipDir } pkg, err := c.ImportDir(path, build.ImportMode(0)) if err != nil { //忽略异常文件夹,(不是golang的目录之类的) return nil } if pkg.IsCommand() { return nil } pkgFullName, err := filepath.Rel(command.gopath, path) if err != nil { return err } moduleList = append(moduleList, strings.TrimPrefix(filepath.ToSlash(pkgFullName), "src/")) return nil }) if err != nil { return } tpl := template.Must(template.New("").Parse(`package {{.PackageName}} import( {{range .ModuleList}} _ "{{.}}" {{end}} ) `)) buf := &bytes.Buffer{} tpl.Execute(buf, struct { PackageName string ModuleList []string }{ PackageName: command.packageName, ModuleList: moduleList, }) fmt.Println(string(buf.Bytes())) return }