Skip to content

Commit 8c22aa5

Browse files
authored
[test] Extract function in scripts/test/binaryenjs.py. NFC (#7535)
Also, move `js_test_wrap` into binaryenjs.py which is its only callsite.
1 parent f1cb466 commit 8c22aa5

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

scripts/test/binaryenjs.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@
1919
from . import support
2020

2121

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+
2247
def do_test_binaryen_js_with(which):
2348
if not (shared.MOZJS or shared.NODEJS):
2449
shared.fail_with_error('no vm to run binaryen.js tests')
@@ -30,16 +55,7 @@ def do_test_binaryen_js_with(which):
3055
print('\n[ checking binaryen.js testcases (' + which + ')... ]\n')
3156

3257
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)
4359

4460
def test(cmd):
4561
if 'fatal' not in s:
@@ -55,10 +71,11 @@ def test(cmd):
5571
if shared.MOZJS:
5672
test([shared.MOZJS, '-m', outname])
5773
if shared.NODEJS:
74+
test_src = open(s).read()
5875
if node_has_wasm or 'WebAssembly.' not in test_src:
5976
test([shared.NODEJS, outname])
6077
else:
61-
print('Skipping ' + basename + ' because WebAssembly might not be supported')
78+
print('Skipping ' + s + ' because WebAssembly might not be supported')
6279

6380

6481
def update_binaryen_js_tests():
@@ -73,18 +90,10 @@ def update_binaryen_js_tests():
7390
print('\n[ checking binaryen.js testcases... ]\n')
7491
node_has_wasm = shared.NODEJS and support.node_has_webassembly(shared.NODEJS)
7592
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)
8594

8695
def update(cmd):
87-
if 'fatal' not in basename:
96+
if 'fatal' not in outname:
8897
out = support.run_command(cmd, stderr=subprocess.STDOUT)
8998
else:
9099
# expect an error - the specific error code will depend on the vm
@@ -93,12 +102,13 @@ def update(cmd):
93102
o.write(out)
94103

95104
# run in available shell
105+
test_src = open(s).read()
96106
if shared.MOZJS:
97107
update([shared.MOZJS, '-m', outname])
98108
elif node_has_wasm or 'WebAssembly.' not in test_src:
99109
update([shared.NODEJS, outname])
100110
else:
101-
print('Skipping ' + basename + ' because WebAssembly might not be supported')
111+
print('Skipping ' + s + ' because WebAssembly might not be supported')
102112

103113

104114
def test_binaryen_js():

scripts/test/support.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,3 @@ def run_command(cmd, expected_status=0, stderr=None,
204204
def node_has_webassembly(cmd):
205205
cmd = [cmd, '-e', 'process.stdout.write(typeof WebAssembly)']
206206
return run_command(cmd) == 'object'
207-
208-
209-
def js_test_wrap():
210-
# common wrapper code for JS tests, waiting for binaryen.js to become ready
211-
# and providing common utility used by all tests:
212-
return '''
213-
(async function __in_test_code__() {
214-
var binaryen = await Binaryen()
215-
function assert(x) { if (!x) throw Error('Test assertion failed'); }
216-
%TEST%
217-
})();
218-
'''

0 commit comments

Comments
 (0)