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

     1  package experimental
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  
    10  	"wa-lang.org/wazero/internal/compilationcache"
    11  	"wa-lang.org/wazero/internal/testing/require"
    12  )
    13  
    14  func TestWithCompilationCacheDirName(t *testing.T) {
    15  	t.Run("ok", func(t *testing.T) {
    16  		dir := t.TempDir()
    17  		ctx, err := WithCompilationCacheDirName(context.Background(), dir)
    18  		require.NoError(t, err)
    19  		actual, ok := ctx.Value(compilationcache.FileCachePathKey{}).(string)
    20  		require.True(t, ok)
    21  		require.Equal(t, dir, actual)
    22  
    23  		// Ensure that the sanity check file has been removed.
    24  		entries, err := os.ReadDir(dir)
    25  		require.NoError(t, err)
    26  		require.Equal(t, 0, len(entries))
    27  	})
    28  	t.Run("create dir", func(t *testing.T) {
    29  		dir := path.Join(t.TempDir(), "1", "2", "3", t.Name()) // Non-existent directory.
    30  		fmt.Println(dir)
    31  		ctx, err := WithCompilationCacheDirName(context.Background(), dir)
    32  		require.NoError(t, err)
    33  		actual, ok := ctx.Value(compilationcache.FileCachePathKey{}).(string)
    34  		require.True(t, ok)
    35  		require.Equal(t, dir, actual)
    36  	})
    37  	t.Run("non dir", func(t *testing.T) {
    38  		f, err := os.CreateTemp(t.TempDir(), "nondir")
    39  		require.NoError(t, err)
    40  		defer f.Close()
    41  
    42  		_, err = WithCompilationCacheDirName(context.Background(), f.Name())
    43  		require.Contains(t, err.Error(), "is not dir")
    44  	})
    45  }