github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/gojs/fs_test.go (about) 1 package gojs_test 2 3 import ( 4 "os" 5 "path" 6 "testing" 7 8 "github.com/bananabytelabs/wazero" 9 "github.com/bananabytelabs/wazero/internal/fstest" 10 "github.com/bananabytelabs/wazero/internal/gojs/config" 11 "github.com/bananabytelabs/wazero/internal/platform" 12 "github.com/bananabytelabs/wazero/internal/testing/require" 13 ) 14 15 func Test_fs(t *testing.T) { 16 t.Parallel() 17 18 stdout, stderr, err := compileAndRun(testCtx, "fs", func(moduleConfig wazero.ModuleConfig) (wazero.ModuleConfig, *config.Config) { 19 return defaultConfig(moduleConfig.WithFS(testFS)) 20 }) 21 22 require.Zero(t, stderr) 23 require.NoError(t, err) 24 require.Equal(t, `sub mode drwxr-xr-x 25 /animals.txt mode -rw-r--r-- 26 animals.txt mode -rw-r--r-- 27 contents: bear 28 cat 29 shark 30 dinosaur 31 human 32 33 empty: 34 `, stdout) 35 } 36 37 // Test_testsfs runs fstest.TestFS inside wasm. 38 func Test_testfs(t *testing.T) { 39 t.Parallel() 40 41 // Setup /testfs which is used in the wasm invocation of testfs.TestFS. 42 tmpDir := t.TempDir() 43 testfsDir := path.Join(tmpDir, "testfs") 44 require.NoError(t, os.Mkdir(testfsDir, 0o700)) 45 require.NoError(t, fstest.WriteTestFiles(testfsDir)) 46 47 fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") 48 stdout, stderr, err := compileAndRun(testCtx, "testfs", func(moduleConfig wazero.ModuleConfig) (wazero.ModuleConfig, *config.Config) { 49 return defaultConfig(moduleConfig.WithFSConfig(fsConfig)) 50 }) 51 52 require.Zero(t, stderr) 53 require.NoError(t, err) 54 require.Zero(t, stdout) 55 } 56 57 func Test_writefs(t *testing.T) { 58 t.Parallel() 59 tmpDir := t.TempDir() 60 fsConfig := wazero.NewFSConfig().WithDirMount(tmpDir, "/") 61 62 // test expects to write under /tmp 63 require.NoError(t, os.Mkdir(path.Join(tmpDir, "tmp"), 0o700)) 64 65 stdout, stderr, err := compileAndRun(testCtx, "writefs", func(moduleConfig wazero.ModuleConfig) (wazero.ModuleConfig, *config.Config) { 66 return defaultConfig(moduleConfig.WithFSConfig(fsConfig)) 67 }) 68 69 require.Zero(t, stderr) 70 require.NoError(t, err) 71 72 if platform.CompilerSupported() { 73 // Note: as of Go 1.19, only the Sec field is set on update in fs_js.go. 74 require.Equal(t, `/tmp/dir mode drwx------ 75 /tmp/dir/file mode -rw------- 76 dir times: 123000000000 567000000000 77 `, stdout) 78 } else { // only mtimes will return on a plarform we don't support in sysfs 79 require.Equal(t, `/tmp/dir mode drwx------ 80 /tmp/dir/file mode -rw------- 81 dir times: 567000000000 567000000000 82 `, stdout) 83 } 84 }