Skip to content

Commit 8d9c908

Browse files
authored
[test] Cleanup JS engine handling (#25102)
- Add `get_v8()` to match existing `get_nodejs()` - Add `get_current_js_engine` helper
1 parent 28022f6 commit 8d9c908

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed

test/common.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,17 +1061,25 @@ def check_dylink(self):
10611061
if self.get_setting('MEMORY64') == 2:
10621062
self.skipTest('dynamic linking not supported with MEMORY64=2')
10631063

1064-
def require_v8(self):
1064+
def get_v8(self):
1065+
"""Return v8 engine, if one is configured, otherwise None"""
10651066
if not config.V8_ENGINE or config.V8_ENGINE not in config.JS_ENGINES:
1067+
return None
1068+
return config.V8_ENGINE
1069+
1070+
def require_v8(self):
1071+
v8 = self.get_v8()
1072+
if not v8:
10661073
if 'EMTEST_SKIP_V8' in os.environ:
10671074
self.skipTest('test requires v8 and EMTEST_SKIP_V8 is set')
10681075
else:
10691076
self.fail('d8 required to run this test. Use EMTEST_SKIP_V8 to skip')
1070-
self.require_engine(config.V8_ENGINE)
1077+
self.require_engine(v8)
10711078
self.cflags.append('-sENVIRONMENT=shell')
10721079

10731080
def get_nodejs(self):
1074-
if config.NODE_JS_TEST not in self.js_engines:
1081+
"""Return nodejs engine, if one is configured, otherwise None"""
1082+
if config.NODE_JS_TEST not in config.JS_ENGINES:
10751083
return None
10761084
return config.NODE_JS_TEST
10771085

@@ -1114,9 +1122,10 @@ def require_wasm64(self):
11141122
if self.try_require_node_version(24):
11151123
return
11161124

1117-
if config.V8_ENGINE and config.V8_ENGINE in self.js_engines:
1125+
v8 = self.get_v8()
1126+
if v8:
11181127
self.cflags.append('-sENVIRONMENT=shell')
1119-
self.js_engines = [config.V8_ENGINE]
1128+
self.js_engines = [v8]
11201129
return
11211130

11221131
if 'EMTEST_SKIP_WASM64' in os.environ:
@@ -1142,9 +1151,10 @@ def require_simd(self):
11421151
if self.try_require_node_version(16):
11431152
return
11441153

1145-
if config.V8_ENGINE and config.V8_ENGINE in self.js_engines:
1154+
v8 = self.get_v8()
1155+
if v8:
11461156
self.cflags.append('-sENVIRONMENT=shell')
1147-
self.js_engines = [config.V8_ENGINE]
1157+
self.js_engines = [v8]
11481158
return
11491159

11501160
if 'EMTEST_SKIP_SIMD' in os.environ:
@@ -1157,9 +1167,10 @@ def require_wasm_legacy_eh(self):
11571167
if self.try_require_node_version(17):
11581168
return
11591169

1160-
if config.V8_ENGINE and config.V8_ENGINE in self.js_engines:
1170+
v8 = self.get_v8()
1171+
if v8:
11611172
self.cflags.append('-sENVIRONMENT=shell')
1162-
self.js_engines = [config.V8_ENGINE]
1173+
self.js_engines = [v8]
11631174
return
11641175

11651176
if 'EMTEST_SKIP_EH' in os.environ:
@@ -1176,9 +1187,10 @@ def require_wasm_eh(self):
11761187
if self.is_browser_test():
11771188
return
11781189

1179-
if config.V8_ENGINE and config.V8_ENGINE in self.js_engines:
1190+
v8 = self.get_v8()
1191+
if v8:
11801192
self.cflags.append('-sENVIRONMENT=shell')
1181-
self.js_engines = [config.V8_ENGINE]
1193+
self.js_engines = [v8]
11821194
self.v8_args.append('--experimental-wasm-exnref')
11831195
return
11841196

@@ -1208,9 +1220,10 @@ def require_jspi(self):
12081220
self.node_args += exp_args
12091221
return
12101222

1211-
if config.V8_ENGINE and config.V8_ENGINE in self.js_engines:
1223+
v8 = self.get_v8()
1224+
if v8:
12121225
self.cflags.append('-sENVIRONMENT=shell')
1213-
self.js_engines = [config.V8_ENGINE]
1226+
self.js_engines = [v8]
12141227
self.v8_args += exp_args
12151228
return
12161229

@@ -1317,7 +1330,7 @@ def setUp(self):
13171330
self.required_engine = None
13181331
self.wasm_engines = config.WASM_ENGINES.copy()
13191332
self.use_all_engines = EMTEST_ALL_ENGINES
1320-
if self.js_engines[0] != config.NODE_JS_TEST:
1333+
if self.get_current_js_engine() != config.NODE_JS_TEST:
13211334
# If our primary JS engine is something other than node then enable
13221335
# shell support.
13231336
default_envs = 'web,webview,worker,node'
@@ -1463,7 +1476,10 @@ def verify_es5(self, filename):
14631476
# use --quiet once its available
14641477
# See: https://github.com/dollarshaveclub/es-check/pull/126/
14651478
es_check_env = os.environ.copy()
1466-
es_check_env['PATH'] = os.path.dirname(config.NODE_JS_TEST[0]) + os.pathsep + es_check_env['PATH']
1479+
# Use NODE_JS here (the version of node that the compiler uses) rather then NODE_JS_TEST (the
1480+
# version of node being used to run the tests) since we only care about having something that
1481+
# can run the es-check tool.
1482+
es_check_env['PATH'] = os.path.dirname(config.NODE_JS[0]) + os.pathsep + es_check_env['PATH']
14671483
inputfile = os.path.abspath(filename)
14681484
# For some reason es-check requires unix paths, even on windows
14691485
if WINDOWS:
@@ -1594,6 +1610,10 @@ def cleanup(line):
15941610
assert len(long_lines) == 1
15951611
return '\n'.join(lines)
15961612

1613+
def get_current_js_engine(self):
1614+
"""Return the default JS engine to run tests under"""
1615+
return self.js_engines[0]
1616+
15971617
def run_js(self, filename, engine=None, args=None,
15981618
assert_returncode=0,
15991619
interleaved_output=True,
@@ -1610,13 +1630,15 @@ def run_js(self, filename, engine=None, args=None,
16101630
error = None
16111631
timeout_error = None
16121632
if not engine:
1613-
engine = self.js_engines[0]
1633+
engine = self.get_current_js_engine()
1634+
# Make a copy of the engine command before we modify/extend it.
1635+
engine = list(engine)
16141636
if engine == config.NODE_JS_TEST:
1615-
engine = engine + self.node_args
1637+
engine += self.node_args
16161638
elif engine == config.V8_ENGINE:
1617-
engine = engine + self.v8_args
1639+
engine += self.v8_args
16181640
elif engine == config.SPIDERMONKEY_ENGINE:
1619-
engine = engine + self.spidermonkey_args
1641+
engine += self.spidermonkey_args
16201642
try:
16211643
jsrun.run_js(filename, engine, args,
16221644
stdout=stdout,

test/jsrun.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ def make_command(filename, engine, args=None):
2424
if engine[0] is None:
2525
executable = shared.replace_suffix(os.path.abspath(filename), '.exe')
2626
return [executable] + args
27-
if type(engine) is not list:
28-
engine = [engine]
2927
# Emscripten supports multiple javascript runtimes. The default is nodejs but
3028
# it can also use d8 (the v8 engine shell) or jsc (JavaScript Core aka
3129
# Safari). Both d8 and jsc require a '--' to delimit arguments to be passed

test/runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@
107107

108108

109109
def check_js_engines():
110-
working_engines = [e for e in config.JS_ENGINES if jsrun.check_engine(e)]
111-
if len(working_engines) < len(config.JS_ENGINES):
110+
if not all(jsrun.check_engine(e) for e in config.JS_ENGINES):
112111
print('Not all the JS engines in JS_ENGINES appears to work.')
113112
sys.exit(1)
114113

test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2995,7 +2995,7 @@ def test_dlfcn_missing(self):
29952995
}
29962996
'''
29972997

2998-
if self.js_engines == [config.V8_ENGINE]:
2998+
if self.get_current_js_engine() == config.V8_ENGINE:
29992999
expected = "error: Could not load dynamic lib: libfoo.so\nError: Error reading file"
30003000
else:
30013001
expected = "error: Could not load dynamic lib: libfoo.so\nError: ENOENT: no such file or directory"

test/test_other.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,8 +1781,7 @@ def test_ungetc_fscanf(self):
17811781
}
17821782
''')
17831783
create_file('my_test.input', 'abc')
1784-
self.emcc('main.c', ['--embed-file', 'my_test.input'], output_filename='a.out.js')
1785-
self.assertContained('zyx', self.run_process(config.JS_ENGINES[0] + ['a.out.js'], stdout=PIPE, stderr=PIPE).stdout)
1784+
self.do_runf('main.c', 'zyx', cflags=['--embed-file', 'my_test.input'])
17861785

17871786
def test_abspaths(self):
17881787
# Includes with absolute paths are generally dangerous, things like -I/usr/.. will get to system

test/test_sanity.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,10 @@ def test_js_engine_path(self):
614614
('nodejs', config.NODE_JS_TEST)]
615615
for filename, engine in jsengines:
616616
delete_file(SANITY_FILE)
617-
if type(engine) is list:
618-
engine = engine[0]
619617
if not engine:
620618
print('WARNING: Not testing engine %s, not configured.' % (filename))
621619
continue
622-
620+
engine = engine[0]
623621
print(filename, engine)
624622

625623
test_engine_path = os.path.join(test_path, filename)
@@ -628,7 +626,7 @@ def test_js_engine_path(self):
628626
f.write('exec %s $@\n' % (engine))
629627
make_executable(test_engine_path)
630628

631-
out = self.run_js(sample_script, engine=test_engine_path, args=['--foo'])
629+
out = self.run_js(sample_script, engine=[test_engine_path], args=['--foo'])
632630

633631
self.assertEqual('0: --foo', out.strip())
634632

0 commit comments

Comments
 (0)