Coverage for tests/component/test_component.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-01 19:40 +0000

1import unittest 

2import tempfile 

3 

4from wasmtime import * 

5from wasmtime.component import Component, ExportIndex 

6 

7class TestComponent(unittest.TestCase): 

8 def test_smoke(self): 

9 Component(Engine(), '(component)') 

10 Component(Engine(), bytes(b'\0asm\x0d\0\x01\0')) 

11 Component(Engine(), bytearray(b'\0asm\x0d\0\x01\0')) 

12 

13 with self.assertRaises(WasmtimeError): 

14 Component(Engine(), '(component2)') 

15 with self.assertRaises(WasmtimeError): 

16 Component(Engine(), bytes(b'\0asm\x01\0\0\0')) 

17 

18 def test_invalid(self): 

19 with self.assertRaises(TypeError): 

20 Component(1, b'') # type: ignore 

21 with self.assertRaises(TypeError): 

22 Component(Engine(), 2) # type: ignore 

23 with self.assertRaises(WasmtimeError): 

24 Component(Engine(), b'') 

25 with self.assertRaises(WasmtimeError): 

26 Component(Engine(), b'\x00') 

27 

28 def test_serialize(self): 

29 engine = Engine() 

30 component = Component(engine, '(component)') 

31 encoded = component.serialize() 

32 component = Component.deserialize(engine, encoded) 

33 with tempfile.TemporaryDirectory() as d: 

34 path = d + '/component.bin' 

35 with open(path, 'wb') as f: 

36 f.write(encoded) 

37 # Run the destructor for `Component` which has an mmap to the file 

38 # which prevents deletion on Windows. 

39 with Component.deserialize_file(engine, path): 

40 pass 

41 

42 def test_exports(self): 

43 engine = Engine() 

44 

45 c = Component(engine, '(component)') 

46 self.assertIsNone(c.get_export_index('foo')) 

47 self.assertIsNone(c.get_export_index('foo', instance = None)) 

48 

49 c = Component(engine, """ 

50 (component 

51 (core module (export "foo")) 

52 ) 

53 """) 

54 foo = c.get_export_index('foo') 

55 self.assertIsNotNone(foo) 

56 self.assertIsNone(c.get_export_index('foo', instance = foo)) 

57 self.assertIsInstance(foo, ExportIndex) 

58 

59 c = Component(engine, """ 

60 (component 

61 (core module $a) 

62 (instance (export "x") 

63 (export "m" (core module $a)) 

64 ) 

65 ) 

66 """) 

67 self.assertIsNotNone(c.get_export_index('x')) 

68 self.assertIsNotNone(c.get_export_index('m', instance = c.get_export_index('x'))) 

69 

70 c2 = Component(engine, """ 

71 (component 

72 (core module $a) 

73 (instance (export "x") 

74 (export "m" (core module $a)) 

75 ) 

76 ) 

77 """) 

78 self.assertIsNone(c2.get_export_index('m', instance = c.get_export_index('x')))