-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow passing fs.FS when calling functions (#571)
Fixes #563 Signed-off-by: knqyf263 <[email protected]> Co-authored-by: Adrian Cole <[email protected]>
- Loading branch information
Showing
13 changed files
with
455 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
greet sub dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package experimental | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
|
||
"github.com/tetratelabs/wazero/api" | ||
internalfs "github.com/tetratelabs/wazero/internal/fs" | ||
) | ||
|
||
// WithFS overrides fs.FS in the context-based manner. Caller needs to take responsibility for closing the filesystem. | ||
func WithFS(ctx context.Context, fs fs.FS) (context.Context, api.Closer, error) { | ||
fsConfig := internalfs.NewFSConfig().WithFS(fs) | ||
preopens, err := fsConfig.Preopens() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
fsCtx := internalfs.NewContext(preopens) | ||
return context.WithValue(ctx, internalfs.Key{}, fsCtx), fsCtx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package experimental_test | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"log" | ||
"testing" | ||
"testing/fstest" | ||
|
||
"github.com/tetratelabs/wazero/experimental" | ||
"github.com/tetratelabs/wazero/internal/fs" | ||
"github.com/tetratelabs/wazero/internal/testing/require" | ||
) | ||
|
||
// This is a very basic integration of fs config. The main goal is to show how it is configured. | ||
func TestWithFS(t *testing.T) { | ||
fileName := "animals.txt" | ||
mapfs := fstest.MapFS{fileName: &fstest.MapFile{Data: []byte(`animals`)}} | ||
|
||
// Set context to one that has experimental fs config | ||
ctx, closer, err := experimental.WithFS(context.Background(), mapfs) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
defer closer.Close(ctx) | ||
|
||
v := ctx.Value(fs.Key{}) | ||
require.NotNil(t, v) | ||
fsCtx, ok := v.(*fs.Context) | ||
require.True(t, ok) | ||
|
||
entry, ok := fsCtx.OpenedFile(3) | ||
require.True(t, ok) | ||
require.Equal(t, "/", entry.Path) | ||
require.Equal(t, mapfs, entry.FS) | ||
|
||
entry, ok = fsCtx.OpenedFile(4) | ||
require.True(t, ok) | ||
require.Equal(t, ".", entry.Path) | ||
require.Equal(t, mapfs, entry.FS) | ||
} |
Oops, something went wrong.