aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.go
diff options
context:
space:
mode:
authorRandy Reddig <[email protected]>2024-09-17 09:24:23 +0200
committerGitHub <[email protected]>2024-09-17 09:24:23 +0200
commit5a014dd6a3b80bba7f8f8a2d5e82b979eed26827 (patch)
tree272ef17946735bd317103a59fb2c179ad2909a6b /main.go
parentd948941d82261f26adf37b5a699ee2359f1eb42c (diff)
downloadtinygo-5a014dd6a3b80bba7f8f8a2d5e82b979eed26827.tar.gz
tinygo-5a014dd6a3b80bba7f8f8a2d5e82b979eed26827.zip
tinygo: add relative and absolute --dir options to wasmtime args (#4431)
main: add relative and absolute --dir options to wasmtime args
Diffstat (limited to 'main.go')
-rw-r--r--main.go90
1 files changed, 32 insertions, 58 deletions
diff --git a/main.go b/main.go
index 227c869c9..6109c00bf 100644
--- a/main.go
+++ b/main.go
@@ -283,46 +283,6 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
// Tests are always run in the package directory.
cmd.Dir = result.MainDir
- // wasmtime is the default emulator used for `-target=wasip1`. wasmtime
- // is a WebAssembly runtime CLI with WASI enabled by default. However,
- // only stdio are allowed by default. For example, while STDOUT routes
- // to the host, other files don't. It also does not inherit environment
- // variables from the host. Some tests read testdata files, often from
- // outside the package directory. Other tests require temporary
- // writeable directories. We allow this by adding wasmtime flags below.
- if config.EmulatorName() == "wasmtime" {
- // At this point, The current working directory is at the package
- // directory. Ex. $GOROOT/src/compress/flate for compress/flate.
- // buildAndRun has already added arguments for wasmtime, that allow
- // read-access to files such as "testdata/huffman-zero.in".
- //
- // Ex. main(.wasm) --dir=. -- -test.v
-
- // Below adds additional wasmtime flags in case a test reads files
- // outside its directory, like "../testdata/e.txt". This allows any
- // relative directory up to the module root, even if the test never
- // reads any files.
- //
- // Ex. run --dir=.. --dir=../.. --dir=../../..
- var dirs []string
- switch config.Target.GOOS {
- case "wasip1":
- dirs = dirsToModuleRootRel(result.MainDir, result.ModuleRoot)
- default:
- dirs = dirsToModuleRootAbs(result.MainDir, result.ModuleRoot)
- }
-
- args := []string{"run"}
- for _, d := range dirs {
- args = append(args, "--dir="+d)
- }
-
- args = append(args, "--env=PWD="+cmd.Dir)
-
- args = append(args, cmd.Args[1:]...)
- cmd.Args = args
- }
-
// Run the test.
start := time.Now()
err = cmd.Run()
@@ -848,12 +808,11 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
for _, v := range environmentVars {
emuArgs = append(emuArgs, "--env", v)
}
- if len(cmdArgs) != 0 {
- // Use of '--' argument no longer necessary as of Wasmtime v14:
- // https://github.com/bytecodealliance/wasmtime/pull/6946
- // args = append(args, "--")
- args = append(args, cmdArgs...)
- }
+
+ // Use of '--' argument no longer necessary as of Wasmtime v14:
+ // https://github.com/bytecodealliance/wasmtime/pull/6946
+ // args = append(args, "--")
+ args = append(args, cmdArgs...)
// Set this for nicer backtraces during tests, but don't override the user.
if _, ok := os.LookupEnv("WASMTIME_BACKTRACE_DETAILS"); !ok {
@@ -903,21 +862,36 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
name = emulator[0]
+ // wasmtime is a WebAssembly runtime CLI with WASI enabled by default.
+ // By default, only stdio is allowed. For example, while STDOUT routes
+ // to the host, other files don't. It also does not inherit environment
+ // variables from the host. Some tests read testdata files, often from
+ // outside the package directory. Other tests require temporary
+ // writeable directories. We allow this by adding wasmtime flags below.
if name == "wasmtime" {
- // Wasmtime needs some special flags to pass environment variables
- // and allow reading from the current directory.
- switch config.Options.Target {
- case "wasip1":
- emuArgs = append(emuArgs, "--dir=.")
- case "wasip2":
- dir := result.MainDir
- if isSingleFile {
- cwd, _ := os.Getwd()
- dir = cwd
+ // Below adds additional wasmtime flags in case a test reads files
+ // outside its directory, like "../testdata/e.txt". This allows any
+ // relative directory up to the module root, even if the test never
+ // reads any files.
+ if config.TestConfig.CompileTestBinary {
+ // Add relative dirs (../, ../..) up to module root (for wasip1)
+ dirs := dirsToModuleRootRel(result.MainDir, result.ModuleRoot)
+
+ // Add absolute dirs up to module root (for wasip2)
+ dirs = append(dirs, dirsToModuleRootAbs(result.MainDir, result.ModuleRoot)...)
+
+ for _, d := range dirs {
+ emuArgs = append(emuArgs, "--dir="+d)
}
- emuArgs = append(emuArgs, "--dir="+dir)
- emuArgs = append(emuArgs, "--env=PWD="+dir)
}
+
+ dir := result.MainDir
+ if isSingleFile {
+ dir, _ = os.Getwd()
+ }
+ emuArgs = append(emuArgs, "--dir=.")
+ emuArgs = append(emuArgs, "--dir="+dir)
+ emuArgs = append(emuArgs, "--env=PWD="+dir)
}
emuArgs = append(emuArgs, emulator[1:]...)