github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/imports/wasi_snapshot_preview1/example/cat_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/fs" 7 "os" 8 "os/exec" 9 "path/filepath" 10 "runtime" 11 "testing" 12 13 "github.com/bananabytelabs/wazero/internal/testing/maintester" 14 "github.com/bananabytelabs/wazero/internal/testing/require" 15 ) 16 17 // Test_main ensures the following will work: 18 // 19 // go run cat.go /test.txt 20 func Test_main(t *testing.T) { 21 for _, toolchain := range []string{"cargo-wasi", "tinygo", "zig", "zig-cc"} { 22 toolchain := toolchain 23 t.Run(toolchain, func(t *testing.T) { 24 t.Setenv("TOOLCHAIN", toolchain) 25 stdout, stderr := maintester.TestMain(t, main, "cat", "test.txt") 26 require.Equal(t, "", stderr) 27 require.Equal(t, "greet filesystem\n", stdout) 28 }) 29 } 30 } 31 32 // Test_cli ensures the following will work: 33 // 34 // go run github.com/bananabytelabs/wazero/cmd/wazero run -mount=testdata:/ cat.wasm /test.txt 35 func Test_cli(t *testing.T) { 36 tests := []struct { 37 toolchain string 38 wasm []byte 39 }{ 40 { 41 toolchain: "cargo-wasi", 42 wasm: catWasmCargoWasi, 43 }, 44 { 45 toolchain: "tinygo", 46 wasm: catWasmTinyGo, 47 }, 48 { 49 toolchain: "zig", 50 wasm: catWasmZig, 51 }, 52 { 53 toolchain: "zig-cc", 54 wasm: catWasmZigCc, 55 }, 56 } 57 58 for _, tc := range tests { 59 tt := tc 60 t.Run(tt.toolchain, func(t *testing.T) { 61 for _, testPath := range []string{"test.txt", "testcases/test.txt"} { 62 if tt.toolchain == "zig" && testPath == "testcases/test.txt" { 63 // Zig only resolves absolute paths under the first 64 // pre-open (cwd), so it won't find this file until #1077 65 continue 66 } 67 t.Run(testPath, func(t *testing.T) { 68 // Write out embedded files instead of accessing directly for docker cross-architecture tests. 69 wasmPath := filepath.Join(t.TempDir(), "cat.wasm") 70 require.NoError(t, os.WriteFile(wasmPath, tt.wasm, 0o755)) 71 72 testTxt, err := fs.ReadFile(catFS, "testdata/test.txt") 73 require.NoError(t, err) 74 testTxtPath := filepath.Join(t.TempDir(), "test.txt") 75 require.NoError(t, os.WriteFile(testTxtPath, testTxt, 0o755)) 76 77 // We can't invoke go run in our docker based cross-architecture tests. We do want to use 78 // otherwise so running unit tests normally does not require special build steps. 79 var cmdExe string 80 var cmdArgs []string 81 if cmdPath := os.Getenv("WAZEROCLI"); cmdPath != "" { 82 cmdExe = cmdPath 83 } else { 84 cmdExe = filepath.Join(runtime.GOROOT(), "bin", "go") 85 cmdArgs = []string{"run", "../../../cmd/wazero"} 86 } 87 88 cmdArgs = append(cmdArgs, "run", 89 "-hostlogging=filesystem", 90 fmt.Sprintf("-mount=%s:/", filepath.Dir(testTxtPath)), 91 fmt.Sprintf("-mount=%s:/testcases", filepath.Dir(testTxtPath)), 92 wasmPath, testPath) 93 94 var stdOut, stdErr bytes.Buffer 95 cmd := exec.Command(cmdExe, cmdArgs...) 96 cmd.Stdout = &stdOut 97 cmd.Stderr = &stdErr 98 require.NoError(t, cmd.Run(), stdErr.String()) 99 require.Equal(t, "greet filesystem\n", stdOut.String()) 100 }) 101 } 102 }) 103 } 104 }