Files
wazero/internal/testing/fs/fs_test.go
Crypt Keeper 105cdcdef7 cli: rewrites compositeFS to syscallfs and adds read-only (:ro) mounts (#1030)
This rewrites compositeFS to syscallfs.FS following wasi-sdk preopen
rules. Notably, this allows use of read-only mounts now.

For example,
```bash
$ GOOS=js GOARCH=wasm bin/go test -c -o template.wasm text/template
$ wazero run -mount=src/text/template:/ -mount=/tmp:/tmp template.wasm -test.v
=== RUN   TestExecute
--- PASS: TestExecute (0.07s)
--snip--
```

This is the first step to native WASI handling of multiple pre-opens.
After this change, it is still the case that there's only one pre-open
FD visible to wasm. A later change will make it possible for WASI to see
multiple pre-opens while `GOOS=js` which doesn't use preopens, remains
on a rootFS.

A future PR may need to add a CLI flag to disable escaping directories,
(e.g. make ../.. EINVAL), similar to `fs.FS` in Go. The simplest way to
allow this is to use a host-side RootFS even in WASI, and wrap that with
a `syscallfs` filename filter.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-13 15:50:11 +08:00

33 lines
630 B
Go

package testfs
import (
"io/fs"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
)
func TestFS(t *testing.T) {
testFS := &FS{}
t.Run("path not found", func(t *testing.T) {
f, err := testFS.Open("foo.txt")
require.Nil(t, f)
require.EqualError(t, err, "open foo.txt: file does not exist")
})
(*testFS)["foo.txt"] = &File{}
f, err := testFS.Open("foo.txt")
require.NoError(t, err)
require.Equal(t, f, &File{})
}
func TestFile(t *testing.T) {
f := &File{CloseErr: fs.ErrClosed}
t.Run("returns close error", func(t *testing.T) {
err := f.Close()
require.Equal(t, fs.ErrClosed, err)
})
}