diff options
author | Ayke van Laethem <[email protected]> | 2021-08-10 21:35:52 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2021-08-12 21:19:24 +0200 |
commit | f57e9622fdb650f14a48cc327db01c216cd0d9c6 (patch) | |
tree | 1bda50a5662f0571e17a8363a6470b8555e244cf /main_test.go | |
parent | c25a7cc7477a5fc58d68e125ec2cf62836f23fe2 (diff) | |
download | tinygo-f57e9622fdb650f14a48cc327db01c216cd0d9c6.tar.gz tinygo-f57e9622fdb650f14a48cc327db01c216cd0d9c6.zip |
baremetal,wasm: support command line params and environment variables
This is mainly useful to be able to run `tinygo test`, for example:
tinygo test -target=cortex-m-qemu -v math
This is not currently supported, but will be in the future.
Diffstat (limited to 'main_test.go')
-rw-r--r-- | main_test.go | 86 |
1 files changed, 58 insertions, 28 deletions
diff --git a/main_test.go b/main_test.go index 2c1b2f864..a502115e4 100644 --- a/main_test.go +++ b/main_test.go @@ -162,15 +162,15 @@ func runPlatTests(target string, tests []string, t *testing.T) { runTest(name, target, t, nil, nil) }) } + t.Run("env.go", func(t *testing.T) { + t.Parallel() + runTest("env.go", target, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"}) + }) if target == "" || target == "wasi" { t.Run("filesystem.go", func(t *testing.T) { t.Parallel() runTest("filesystem.go", target, t, nil, nil) }) - t.Run("env.go", func(t *testing.T) { - t.Parallel() - runTest("env.go", target, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"}) - }) } if target == "" || target == "wasi" || target == "wasm" { t.Run("rand.go", func(t *testing.T) { @@ -233,6 +233,42 @@ func runTestWithConfig(name, target string, t *testing.T, options compileopts.Op } }() + // Determine whether we're on a system that supports environment variables + // and command line parameters (operating systems, WASI) or not (baremetal, + // WebAssembly in the browser). If we're on a system without an environment, + // we need to pass command line arguments and environment variables through + // global variables (built into the binary directly) instead of the + // conventional way. + spec, err := compileopts.LoadTarget(target) + if err != nil { + t.Fatal("failed to load target spec:", err) + } + needsEnvInVars := spec.GOOS == "js" + for _, tag := range spec.BuildTags { + if tag == "baremetal" { + needsEnvInVars = true + } + } + if needsEnvInVars { + runtimeGlobals := make(map[string]string) + if len(cmdArgs) != 0 { + runtimeGlobals["osArgs"] = strings.Join(cmdArgs, "\x00") + } + if len(environmentVars) != 0 { + runtimeGlobals["osEnv"] = strings.Join(environmentVars, "\x00") + } + if len(runtimeGlobals) != 0 { + // This sets the global variables like they would be set with + // `-ldflags="-X=runtime.osArgs=first\x00second`. + // The runtime package has two variables (osArgs and osEnv) that are + // both strings, from which the parameters and environment variables + // are read. + options.GlobalValues = map[string]map[string]string{ + "runtime": runtimeGlobals, + } + } + } + // Build the test binary. binary := filepath.Join(tmpdir, "test") err = runBuild("./"+path, binary, &options) @@ -242,37 +278,31 @@ func runTestWithConfig(name, target string, t *testing.T, options compileopts.Op return } - // Run the test. - runComplete := make(chan struct{}) + // Create the test command, taking care of emulators etc. var cmd *exec.Cmd - ranTooLong := false - if target == "" { + if len(spec.Emulator) == 0 { cmd = exec.Command(binary) - cmd.Env = append(cmd.Env, environmentVars...) - cmd.Args = append(cmd.Args, cmdArgs...) } else { - spec, err := compileopts.LoadTarget(target) - if err != nil { - t.Fatal("failed to load target spec:", err) - } - if len(spec.Emulator) == 0 { - cmd = exec.Command(binary) - } else { - args := append(spec.Emulator[1:], binary) - cmd = exec.Command(spec.Emulator[0], args...) + args := append(spec.Emulator[1:], binary) + cmd = exec.Command(spec.Emulator[0], args...) + } + if len(spec.Emulator) != 0 && spec.Emulator[0] == "wasmtime" { + // Allow reading from the current directory. + cmd.Args = append(cmd.Args, "--dir=.") + for _, v := range environmentVars { + cmd.Args = append(cmd.Args, "--env", v) } - - if len(spec.Emulator) != 0 && spec.Emulator[0] == "wasmtime" { - // Allow reading from the current directory. - cmd.Args = append(cmd.Args, "--dir=.") - for _, v := range environmentVars { - cmd.Args = append(cmd.Args, "--env", v) - } - cmd.Args = append(cmd.Args, cmdArgs...) - } else { + cmd.Args = append(cmd.Args, cmdArgs...) + } else { + if !needsEnvInVars { + cmd.Args = append(cmd.Args, cmdArgs...) // works on qemu-aarch64 etc cmd.Env = append(cmd.Env, environmentVars...) } } + + // Run the test. + runComplete := make(chan struct{}) + ranTooLong := false stdout := &bytes.Buffer{} cmd.Stdout = stdout cmd.Stderr = os.Stderr |