Coverage for tests/test_wasi.py: 92%
40 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
1import unittest
2import tempfile
4from wasmtime import *
5from pathlib import Path
8class TestWasi(unittest.TestCase):
9 def test_config(self):
10 config = WasiConfig()
11 config.argv = ['a', 'b']
12 config.inherit_argv()
13 config.env = [['a', 'b']]
14 config.inherit_env()
16 with tempfile.NamedTemporaryFile() as f:
17 config.stdin_file = f.name
18 config.stdin_file = Path(f.name)
19 config.inherit_stdin()
20 config.stdout_file = f.name
21 config.stdout_file = Path(f.name)
22 config.inherit_stdout()
23 config.stderr_file = f.name
24 config.stderr_file = Path(f.name)
25 config.inherit_stderr()
27 with self.assertRaises(WasmtimeError):
28 config.stdin_file = 'somewhere-over-the-rainboat'
29 with self.assertRaises(WasmtimeError):
30 config.stdout_file = 'some-directory/without-a-rainbow'
31 with self.assertRaises(WasmtimeError):
32 config.stderr_file = 'some-directory/without-a-rainbow'
33 config.preopen_dir('wasmtime', 'other', DirPerms.READ_WRITE, FilePerms.READ_WRITE)
34 config.preopen_dir('wasmtime', 'other2')
36 def test_preview1(self):
37 linker = Linker(Engine())
38 linker.define_wasi()
40 module = Module(linker.engine, """
41 (module
42 (import "wasi_snapshot_preview1" "random_get"
43 (func (param i32 i32) (result i32)))
44 )
45 """)
47 store = Store(linker.engine)
48 store.set_wasi(WasiConfig())
49 linker.instantiate(store, module)
51 def preopen_nonexistent(self):
52 config = WasiConfig()
53 with self.assertRaises(WasmtimeError):
54 config.preopen_dir('/path/to/nowhere', '/', DirPerms.READ_ONLY, FilePerms.READ_ONLY)