예제 #1
0
파일: main.go 프로젝트: Grigs-b/withmock
func doit() error {
	// Before we get to work, parse the command line

	flag.Usage = usage
	flag.Parse()

	// We need at least one argument

	if flag.NArg() < 1 {
		usage()
		os.Exit(1)
	}

	// First we need to create a context

	ctxt, err := lib.NewContext()
	if err != nil {
		return err
	}
	defer ctxt.Close()

	if *work {
		ctxt.KeepWork()
	}

	if *raw {
		ctxt.DisableRewrite()
	}

	// Load the excluded packages file if configured

	if *exclFile != "" {
		if err := ctxt.ExcludePackagesFromFile(*exclFile); err != nil {
			return err
		}
	}

	// Load the config file if specified

	if *cfgFile != "" {
		if err := ctxt.LoadConfig(*cfgFile); err != nil {
			return err
		}
	}

	// Now we add the package that we want to test to the context, this will
	// install the imports used by that package (mocking them as approprite).

	pkg, err := lib.GetOutput("go", "list", ".")
	if err != nil {
		return err
	}

	testPkg, err := ctxt.AddPackage(pkg)
	if err != nil {
		return err
	}

	// Add extra packages if configured

	if *pkgFile != "" {
		if err := ctxt.LinkPackagesFromFile(*pkgFile); err != nil {
			return err
		}
	}

	// Add in the gocov library, so that we can run with gocov if we want.

	if flag.Arg(0) == "gocov" || *gocov {
		if err := ctxt.LinkPackage("github.com/axw/gocov"); err != nil {
			return err
		}
	}

	// Finally we can chdir into the test code, and run the command inside the
	// context

	if err := ctxt.Chdir(testPkg); err != nil {
		return err
	}

	return ctxt.Run(flag.Arg(0), flag.Args()[1:]...)
}
예제 #2
0
파일: main.go 프로젝트: Grigs-b/withmock
func doit() error {
	// Before we get to work, parse the command line

	flag.Usage = usage
	flag.Parse()

	args := flag.Args()
	if len(args) == 0 {
		args = []string{"."}
	}

	// We need at least one argument

	pkgs := []string{}

	for _, arg := range args {
		list, err := lib.GetOutput("go", "list", arg)
		if err != nil {
			return lib.Cerr{"go list", err}
		}
		for _, pkg := range strings.Split(list, "\n") {
			pkg = strings.TrimSpace(pkg)
			if len(pkg) == 0 {
				continue
			}
			pkgs = append(pkgs, pkg)
		}
	}

	if len(pkgs) == 0 {
		fmt.Printf("no packages to test\n")
		os.Exit(1)
	}

	// First we need to create a context

	ctxt, err := lib.NewContext()
	if err != nil {
		return lib.Cerr{"NewContext", err}
	}
	defer ctxt.Close()

	if *work {
		ctxt.KeepWork()
	}

	if *raw {
		ctxt.DisableRewrite()
	}

	// Load the excluded packages file if configured

	if *exclFile != "" {
		if err := ctxt.ExcludePackagesFromFile(*exclFile); err != nil {
			return lib.Cerr{"ExcludePackagesFromFile", err}
		}
	}

	// Load the config file if specified

	if *cfgFile != "" {
		if err := ctxt.LoadConfig(*cfgFile); err != nil {
			return lib.Cerr{"LoadConfig", err}
		}
	}

	// Start building the command string that we will run

	command := "go"
	args = []string{"test"}
	if *verbose {
		args = append(args, "-v")
	}
	if *compile {
		args = append(args, "-c")
	}

	// Now we add the packages that we want to test to the context, this will
	// install the imports used by those packages (mocking them as approprite).

	for _, pkg := range pkgs {
		name, err := ctxt.AddPackage(pkg)
		if err != nil {
			return lib.Cerr{"AddPackage", err}
		}
		args = append(args, name)
	}

	// Add extra packages if configured

	if *pkgFile != "" {
		if err := ctxt.LinkPackagesFromFile(*pkgFile); err != nil {
			return lib.Cerr{"LinkPackagesFromFile", err}
		}
	}

	// Add in the gocov library, so that we can run with gocov if we want.

	if *gocov {
		if *compile {
			return fmt.Errorf("gocov doesn't provide a compiliation only mode.")
		}
		if err := ctxt.LinkPackage("github.com/axw/gocov"); err != nil {
			return lib.Cerr{"LinkPackage(gocov)", err}
		}
		command = "gocov"
	}

	// Finally we can run the command inside the context

	if err := ctxt.Run(command, args...); err != nil {
		return lib.Cerr{"Run", err}
	}

	return nil
}