19
19
from . import support
20
20
21
21
22
+ def js_test_wrap ():
23
+ # common wrapper code for JS tests, waiting for binaryen.js to become ready
24
+ # and providing common utility used by all tests:
25
+ return '''
26
+ (async function __in_test_code__() {
27
+ var binaryen = await Binaryen()
28
+ function assert(x) { if (!x) throw Error('Test assertion failed'); }
29
+ %TEST%
30
+ })();
31
+ '''
32
+
33
+
34
+ def make_js_test (input_js_file , binaryen_js ):
35
+ basename = os .path .basename (input_js_file )
36
+ outname = os .path .splitext (basename )[0 ] + '.mjs'
37
+ with open (outname , 'w' ) as f :
38
+ # avoid stdout/stderr ordering issues in some js shells - use just stdout
39
+ f .write ('console.warn = console.error = console.log;' )
40
+ binaryen_js = open (binaryen_js ).read ()
41
+ f .write (binaryen_js )
42
+ test_src = open (input_js_file ).read ()
43
+ f .write (js_test_wrap ().replace ('%TEST%' , test_src ))
44
+ return outname
45
+
46
+
22
47
def do_test_binaryen_js_with (which ):
23
48
if not (shared .MOZJS or shared .NODEJS ):
24
49
shared .fail_with_error ('no vm to run binaryen.js tests' )
@@ -30,16 +55,7 @@ def do_test_binaryen_js_with(which):
30
55
print ('\n [ checking binaryen.js testcases (' + which + ')... ]\n ' )
31
56
32
57
for s in shared .get_tests (shared .get_test_dir ('binaryen.js' ), ['.js' ]):
33
- basename = os .path .basename (s )
34
- outname = os .path .splitext (basename )[0 ] + '.mjs'
35
- f = open (outname , 'w' )
36
- # avoid stdout/stderr ordering issues in some js shells - use just stdout
37
- f .write ('console.warn = console.error = console.log;' )
38
- binaryen_js = open (which ).read ()
39
- f .write (binaryen_js )
40
- test_src = open (s ).read ()
41
- f .write (support .js_test_wrap ().replace ('%TEST%' , test_src ))
42
- f .close ()
58
+ outname = make_js_test (s , which )
43
59
44
60
def test (cmd ):
45
61
if 'fatal' not in s :
@@ -55,10 +71,11 @@ def test(cmd):
55
71
if shared .MOZJS :
56
72
test ([shared .MOZJS , '-m' , outname ])
57
73
if shared .NODEJS :
74
+ test_src = open (s ).read ()
58
75
if node_has_wasm or 'WebAssembly.' not in test_src :
59
76
test ([shared .NODEJS , outname ])
60
77
else :
61
- print ('Skipping ' + basename + ' because WebAssembly might not be supported' )
78
+ print ('Skipping ' + s + ' because WebAssembly might not be supported' )
62
79
63
80
64
81
def update_binaryen_js_tests ():
@@ -73,18 +90,10 @@ def update_binaryen_js_tests():
73
90
print ('\n [ checking binaryen.js testcases... ]\n ' )
74
91
node_has_wasm = shared .NODEJS and support .node_has_webassembly (shared .NODEJS )
75
92
for s in shared .get_tests (shared .get_test_dir ('binaryen.js' ), ['.js' ]):
76
- basename = os .path .basename (s )
77
- outname = os .path .splitext (basename )[0 ] + '.mjs'
78
- f = open (outname , 'w' )
79
- # avoid stdout/stderr ordering issues in some js shells - use just stdout
80
- f .write ('console.warn = console.error = console.log;' )
81
- f .write (open (shared .BINARYEN_JS ).read ())
82
- test_src = open (s ).read ()
83
- f .write (support .js_test_wrap ().replace ('%TEST%' , test_src ))
84
- f .close ()
93
+ outname = make_js_test (s , shared .BINARYEN_JS )
85
94
86
95
def update (cmd ):
87
- if 'fatal' not in basename :
96
+ if 'fatal' not in outname :
88
97
out = support .run_command (cmd , stderr = subprocess .STDOUT )
89
98
else :
90
99
# expect an error - the specific error code will depend on the vm
@@ -93,12 +102,13 @@ def update(cmd):
93
102
o .write (out )
94
103
95
104
# run in available shell
105
+ test_src = open (s ).read ()
96
106
if shared .MOZJS :
97
107
update ([shared .MOZJS , '-m' , outname ])
98
108
elif node_has_wasm or 'WebAssembly.' not in test_src :
99
109
update ([shared .NODEJS , outname ])
100
110
else :
101
- print ('Skipping ' + basename + ' because WebAssembly might not be supported' )
111
+ print ('Skipping ' + s + ' because WebAssembly might not be supported' )
102
112
103
113
104
114
def test_binaryen_js ():
0 commit comments