Skip to content

Commit 1807705

Browse files
authored
Use ES6 import to run JS tests. NFC (#7536)
Previously each test would take binaryen.js can modify a copy of it. Now they import it, as is (hopefully) more common in practice.
1 parent aee3229 commit 1807705

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

scripts/test/binaryenjs.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,41 @@
1313
# limitations under the License.
1414

1515
import os
16+
import shutil
1617
import subprocess
1718

1819
from . import shared
1920
from . import support
2021

2122

22-
def js_test_wrap():
23+
def make_js_test_header(binaryen_js):
2324
# common wrapper code for JS tests, waiting for binaryen.js to become ready
2425
# and providing common utility used by all tests:
2526
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-
'''
27+
import Binaryen from "%s";
28+
var binaryen = await Binaryen()
29+
30+
// avoid stdout/stderr ordering issues in some js shells - use just stdout
31+
console.warn = console.error = console.log;
32+
33+
function assert(x) {
34+
if (!x) throw Error('Test assertion failed');
35+
}
36+
''' % binaryen_js
3237

3338

3439
def make_js_test(input_js_file, binaryen_js):
40+
# Copy the binaryen.js file to binaryen.mjs for now since file
41+
# extensions matter under node.
42+
# TODO(sbc): Should binaryen build as a `.mjs` file itself?
43+
shutil.copyfile(binaryen_js, 'binaryen.mjs')
44+
3545
basename = os.path.basename(input_js_file)
3646
outname = os.path.splitext(basename)[0] + '.mjs'
3747
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)
48+
f.write(make_js_test_header('./binaryen.mjs'))
4249
test_src = open(input_js_file).read()
43-
f.write(js_test_wrap().replace('%TEST%', test_src))
50+
f.write(test_src)
4451
return outname
4552

4653

0 commit comments

Comments
 (0)