Add test command to tinygo (#243)

* Add test command to tinygo
This commit is contained in:
Carolyn Van Slyck
2019-06-18 03:23:59 -07:00
committed by Ron Evans
parent a3d1f1a514
commit 208e1719ad
9 changed files with 288 additions and 13 deletions
+33
View File
@@ -54,6 +54,7 @@ type BuildConfig struct {
cFlags []string
ldFlags []string
wasmAbi string
testConfig compiler.TestConfig
}
// Helper function for Compiler object.
@@ -108,6 +109,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
GOROOT: goroot,
GOPATH: getGopath(),
BuildTags: tags,
TestConfig: config.testConfig,
}
c, err := compiler.NewCompiler(pkgName, compilerConfig)
if err != nil {
@@ -349,6 +351,30 @@ func Build(pkgName, outpath, target string, config *BuildConfig) error {
})
}
func Test(pkgName, target string, config *BuildConfig) error {
spec, err := LoadTarget(target)
if err != nil {
return err
}
spec.BuildTags = append(spec.BuildTags, "test")
config.testConfig.CompileTestBinary = true
return Compile(pkgName, ".elf", spec, config, func(tmppath string) error {
cmd := exec.Command(tmppath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
// Propagate the exit code
if err, ok := err.(*exec.ExitError); ok {
os.Exit(err.ExitCode())
}
return &commandError{"failed to run compiled binary", tmppath, err}
}
return nil
})
}
func Flash(pkgName, target, port string, config *BuildConfig) error {
spec, err := LoadTarget(target)
if err != nil {
@@ -656,6 +682,13 @@ func main() {
}
err := Run(flag.Arg(0), *target, config)
handleCompilerError(err)
case "test":
pkgRoot := "."
if flag.NArg() == 1 {
pkgRoot = flag.Arg(0)
}
err := Test(pkgRoot, *target, config)
handleCompilerError(err)
case "clean":
// remove cache directory
dir := cacheDir()