Skip to content

Commit 5b7777a

Browse files
committed
add CacheDir tests
1 parent dd6cf2b commit 5b7777a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func createGlobalCompiledModule(
6363
if cacheDir == "" || disableBuildCache {
6464
cache = wazero.NewCompilationCache()
6565
} else if cache, err = wazero.NewCompilationCacheWithDir(cacheDir); err != nil {
66-
return fmt.Errorf("failed to create compilation cache: %w", err)
66+
return fmt.Errorf("failed to create compilation cache with dir %s: %w", cacheDir, err)
6767
}
6868

6969
cachedRuntimeConfig = wazero.

runtime_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,34 @@ func TestRuntime(t *testing.T) {
255255
require.NoError(t, err, "Normal runtime creation should still work after invalid attempt")
256256
defer rt2.Close()
257257
})
258+
259+
t.Run("CacheDirOption", func(t *testing.T) {
260+
rt1, err := qjs.New(qjs.Option{CacheDir: t.TempDir()})
261+
require.NoError(t, err)
262+
defer rt1.Close()
263+
264+
val, err := rt1.Eval("test.js", qjs.Code("21 + 21"))
265+
require.NoError(t, err)
266+
assert.Equal(t, int32(42), val.Int32())
267+
val.Free()
268+
})
269+
270+
t.Run("CacheDirWithEmptyString", func(t *testing.T) {
271+
rt, err := qjs.New(qjs.Option{CacheDir: ""})
272+
require.NoError(t, err)
273+
defer rt.Close()
274+
275+
val, err := rt.Eval("test.js", qjs.Code("2 * 21"))
276+
require.NoError(t, err)
277+
assert.Equal(t, int32(42), val.Int32())
278+
val.Free()
279+
})
280+
281+
t.Run("CacheDirWithInvalidPath", func(t *testing.T) {
282+
_, err := qjs.New(qjs.Option{CacheDir: "/invalid/path/that/does/not/exist", QuickJSWasmBytes: []byte("cachedir")})
283+
require.Error(t, err)
284+
assert.Contains(t, err.Error(), "failed to create compilation cache")
285+
})
258286
}
259287

260288
// Concurrent Runtime Usage Tests

0 commit comments

Comments
 (0)