wa-lang.org/wazero@v1.0.2/experimental/fs_test.go (about)

     1  package experimental_test
     2  
     3  import (
     4  	"context"
     5  	_ "embed"
     6  	"testing"
     7  	"testing/fstest"
     8  
     9  	"wa-lang.org/wazero/experimental"
    10  	"wa-lang.org/wazero/internal/sys"
    11  	"wa-lang.org/wazero/internal/testing/require"
    12  )
    13  
    14  // This is a very basic integration of fs config. The main goal is to show how it is configured.
    15  func TestWithFS(t *testing.T) {
    16  	fileName := "animals.txt"
    17  	mapfs := fstest.MapFS{fileName: &fstest.MapFile{Data: []byte(`animals`)}}
    18  
    19  	// Set context to one that has experimental fs config
    20  	ctx, closer := experimental.WithFS(context.Background(), mapfs)
    21  	defer closer.Close(ctx)
    22  
    23  	v := ctx.Value(sys.FSKey{})
    24  	require.NotNil(t, v)
    25  	fsCtx, ok := v.(*sys.FSContext)
    26  	require.True(t, ok)
    27  
    28  	entry, ok := fsCtx.OpenedFile(ctx, 3)
    29  	require.True(t, ok)
    30  	require.Equal(t, "/", entry.Path)
    31  
    32  	// Override to nil context, ex to block file access
    33  	ctx, closer = experimental.WithFS(ctx, nil)
    34  	defer closer.Close(ctx)
    35  
    36  	v = ctx.Value(sys.FSKey{})
    37  	require.NotNil(t, v)
    38  	fsCtx, ok = v.(*sys.FSContext)
    39  	require.True(t, ok)
    40  
    41  	_, ok = fsCtx.OpenedFile(ctx, 3)
    42  	require.False(t, ok)
    43  }