Exemple #1
0
/*
递归目录的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)
}
Exemple #2
0
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)
	})
}