Skip to content

Commit f40f61f

Browse files
authored
[test] Improve emcc test helper. NFC (#25517)
Add `maybe_test_file` helper and use it in `emcc` helper method.
1 parent 02c9e71 commit f40f61f

File tree

4 files changed

+43
-39
lines changed

4 files changed

+43
-39
lines changed

test/common.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ def test_file(*path_components):
179179
return str(Path(TEST_ROOT, *path_components))
180180

181181

182+
def maybe_test_file(filename):
183+
if not os.path.exists(filename) and os.path.exists(test_file(filename)):
184+
filename = test_file(filename)
185+
return filename
186+
187+
182188
def copytree(src, dest):
183189
shutil.copytree(src, dest, dirs_exist_ok=True)
184190

@@ -1495,8 +1501,7 @@ def verify_es5(self, filename):
14951501

14961502
# Build JavaScript code from source code
14971503
def build(self, filename, libraries=None, includes=None, force_c=False, cflags=None, output_basename=None, output_suffix=None):
1498-
if not os.path.exists(filename):
1499-
filename = test_file(filename)
1504+
filename = maybe_test_file(filename)
15001505
compiler = [compiler_for(filename, force_c)]
15011506

15021507
if force_c:
@@ -1856,6 +1861,7 @@ def run_process(self, cmd, check=True, **kwargs):
18561861
return rtn
18571862

18581863
def emcc(self, filename, args=[], output_filename=None, **kwargs): # noqa
1864+
filename = maybe_test_file(filename)
18591865
compile_only = '-c' in args or '-sSIDE_MODULE' in args
18601866
cmd = [compiler_for(filename), filename] + self.get_cflags(compile_only=compile_only) + args
18611867
if output_filename:
@@ -2025,8 +2031,7 @@ def do_runf(self, filename, expected_output=None, **kwargs):
20252031
return self._build_and_run(filename, expected_output, **kwargs)
20262032

20272033
def do_run_in_out_file_test(self, srcfile, **kwargs):
2028-
if not os.path.exists(srcfile):
2029-
srcfile = test_file(srcfile)
2034+
srcfile = maybe_test_file(srcfile)
20302035
out_suffix = kwargs.pop('out_suffix', '')
20312036
outfile = shared.unsuffixed(srcfile) + out_suffix + '.out'
20322037
if EMTEST_REBASELINE:
@@ -2753,8 +2758,7 @@ def compile_btest(self, filename, cflags, reporting=Reporting.FULL):
27532758
cflags += ['report_result.o', '-include', test_file('report_result.h')]
27542759
if EMTEST_BROWSER == 'node':
27552760
cflags.append('-DEMTEST_NODE')
2756-
if not os.path.exists(filename):
2757-
filename = test_file(filename)
2761+
filename = maybe_test_file(filename)
27582762
self.run_process([compiler_for(filename), filename] + self.get_cflags() + cflags)
27592763
# Remove the file since some tests have assertions for how many files are in
27602764
# the output directory.

test/test_browser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,7 @@ def test_emscripten_async_wget2_data(self):
25072507
self.btest('test_emscripten_async_wget2_data.cpp', expected='0')
25082508

25092509
def test_emscripten_async_wget_side_module(self):
2510-
self.emcc(test_file('test_emscripten_async_wget_side_module.c'), ['-o', 'lib.wasm', '-O2', '-sSIDE_MODULE'])
2510+
self.emcc('test_emscripten_async_wget_side_module.c', ['-o', 'lib.wasm', '-O2', '-sSIDE_MODULE'])
25112511
self.btest_exit('test_emscripten_async_wget_main_module.c', cflags=['-O2', '-sMAIN_MODULE=2'])
25122512

25132513
@parameterized({
@@ -3205,7 +3205,7 @@ def test_sdl2_misc_main_module(self):
32053205
self.btest_exit('test_sdl2_misc.c', cflags=['-sUSE_SDL=2', '-sMAIN_MODULE'])
32063206

32073207
def test_sdl2_misc_via_object(self):
3208-
self.emcc(test_file('browser/test_sdl2_misc.c'), ['-c', '-sUSE_SDL=2', '-o', 'test.o'])
3208+
self.emcc('browser/test_sdl2_misc.c', ['-c', '-sUSE_SDL=2', '-o', 'test.o'])
32093209
self.compile_btest('test.o', ['-sEXIT_RUNTIME', '-sUSE_SDL=2', '-o', 'test.html'])
32103210
self.run_browser('test.html', '/report_result?exit:0')
32113211

@@ -3544,7 +3544,7 @@ def test_dlopen_async(self):
35443544

35453545
@requires_shared_array_buffer
35463546
def test_dlopen_blocking(self):
3547-
self.emcc(test_file('other/test_dlopen_blocking_side.c'), ['-o', 'libside.so', '-sSIDE_MODULE', '-pthread', '-Wno-experimental'])
3547+
self.emcc('other/test_dlopen_blocking_side.c', ['-o', 'libside.so', '-sSIDE_MODULE', '-pthread', '-Wno-experimental'])
35483548
# Attempt to use dlopen the side module (without preloading) should fail on the main thread
35493549
# since the syncronous `readBinary` function does not exist.
35503550
self.btest_exit('other/test_dlopen_blocking.c', assert_returncode=1, cflags=['-sMAIN_MODULE=2', '-sAUTOLOAD_DYLIBS=0', 'libside.so'])
@@ -5641,7 +5641,7 @@ def test_program_arg_separator(self):
56415641
self.assertContained('remember to add `--` between arguments', err)
56425642

56435643
def test_emrun(self):
5644-
self.emcc(test_file('test_emrun.c'), ['--emrun', '-o', 'test_emrun.html'])
5644+
self.emcc('test_emrun.c', ['--emrun', '-o', 'test_emrun.html'])
56455645
if not has_browser():
56465646
self.skipTest('need a browser')
56475647

test/test_codesize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,5 +402,5 @@ def test_codesize_file_preload(self):
402402
self.run_codesize_test('hello_world.c', cflags=['-sSTRICT', '-O3', '--preload-file=somefile.txt'], check_full_js=True)
403403

404404
def test_small_js_flags(self):
405-
self.emcc(test_file('browser_test_hello_world.c'), ['-O3', '--closure=1', '-sINCOMING_MODULE_JS_API=[]', '-sENVIRONMENT=web', '--output-eol=linux'])
405+
self.emcc('browser_test_hello_world.c', ['-O3', '--closure=1', '-sINCOMING_MODULE_JS_API=[]', '-sENVIRONMENT=web', '--output-eol=linux'])
406406
self.check_output_sizes('a.out.js')

test/test_other.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,28 +2461,28 @@ def test_sdl_get_key_name(self):
24612461

24622462
@requires_network
24632463
def test_sdl2_mixer_wav(self):
2464-
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['-sUSE_SDL_MIXER=2'], output_filename='a.out.js')
2465-
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['--use-port=sdl2_mixer'], output_filename='a.out.js')
2466-
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['--use-port=sdl2_mixer:formats=ogg'], output_filename='a.out.js')
2464+
self.emcc('browser/test_sdl2_mixer_wav.c', ['-sUSE_SDL_MIXER=2'], output_filename='a.out.js')
2465+
self.emcc('browser/test_sdl2_mixer_wav.c', ['--use-port=sdl2_mixer'], output_filename='a.out.js')
2466+
self.emcc('browser/test_sdl2_mixer_wav.c', ['--use-port=sdl2_mixer:formats=ogg'], output_filename='a.out.js')
24672467

24682468
def test_sdl2_linkable(self):
24692469
# Ensure that SDL2 can be built with LINKABLE. This implies there are no undefined
24702470
# symbols in the library (because LINKABLE includes the entire library).
2471-
self.emcc(test_file('browser/test_sdl2_misc.c'), ['-sLINKABLE', '-Wno-deprecated', '-sUSE_SDL=2'], output_filename='a.out.js')
2472-
self.emcc(test_file('browser/test_sdl2_misc.c'), ['-sLINKABLE', '-Wno-deprecated', '--use-port=sdl2'], output_filename='a.out.js')
2471+
self.emcc('browser/test_sdl2_misc.c', ['-sLINKABLE', '-Wno-deprecated', '-sUSE_SDL=2'], output_filename='a.out.js')
2472+
self.emcc('browser/test_sdl2_misc.c', ['-sLINKABLE', '-Wno-deprecated', '--use-port=sdl2'], output_filename='a.out.js')
24732473

24742474
def test_sdl3_linkable(self):
24752475
# Ensure that SDL3 can be built with LINKABLE. This implies there are no undefined
24762476
# symbols in the library (because LINKABLE includes the entire library).
24772477
self.cflags.append('-Wno-experimental')
2478-
self.emcc(test_file('browser/test_sdl3_misc.c'), ['-sLINKABLE', '-Wno-deprecated', '-sUSE_SDL=3'], output_filename='a.out.js')
2479-
self.emcc(test_file('browser/test_sdl3_misc.c'), ['-sLINKABLE', '-Wno-deprecated', '--use-port=sdl3'], output_filename='a.out.js')
2478+
self.emcc('browser/test_sdl3_misc.c', ['-sLINKABLE', '-Wno-deprecated', '-sUSE_SDL=3'], output_filename='a.out.js')
2479+
self.emcc('browser/test_sdl3_misc.c', ['-sLINKABLE', '-Wno-deprecated', '--use-port=sdl3'], output_filename='a.out.js')
24802480

24812481
@requires_network
24822482
def test_sdl2_gfx_linkable(self):
24832483
# Same as above but for sdl2_gfx library
2484-
self.emcc(test_file('browser/test_sdl2_misc.c'), ['-Wl,-fatal-warnings', '-sLINKABLE', '-Wno-deprecated', '-sUSE_SDL_GFX=2'], output_filename='a.out.js')
2485-
self.emcc(test_file('browser/test_sdl2_misc.c'), ['-Wl,-fatal-warnings', '-sLINKABLE', '-Wno-deprecated', '--use-port=sdl2_gfx'], output_filename='a.out.js')
2484+
self.emcc('browser/test_sdl2_misc.c', ['-Wl,-fatal-warnings', '-sLINKABLE', '-Wno-deprecated', '-sUSE_SDL_GFX=2'], output_filename='a.out.js')
2485+
self.emcc('browser/test_sdl2_misc.c', ['-Wl,-fatal-warnings', '-sLINKABLE', '-Wno-deprecated', '--use-port=sdl2_gfx'], output_filename='a.out.js')
24862486

24872487
@requires_network
24882488
def test_libpng(self):
@@ -2565,7 +2565,7 @@ def test_freetype(self):
25652565
@requires_network
25662566
def test_freetype_with_pthreads(self):
25672567
# Verify that freetype supports compilation requiring pthreads
2568-
self.emcc(test_file('test_freetype.c'), ['-pthread', '-sUSE_FREETYPE'], output_filename='a.out.js')
2568+
self.emcc('test_freetype.c', ['-pthread', '-sUSE_FREETYPE'], output_filename='a.out.js')
25692569

25702570
@requires_network
25712571
def test_icu(self):
@@ -2575,18 +2575,18 @@ def test_icu(self):
25752575
@requires_network
25762576
def test_sdl2_ttf(self):
25772577
# This is a compile-only to test to verify that sdl2-ttf (and freetype and harfbuzz) are buildable.
2578-
self.emcc(test_file('browser/test_sdl2_ttf.c'), args=['-sUSE_SDL=2', '-sUSE_SDL_TTF=2'], output_filename='a.out.js')
2579-
self.emcc(test_file('browser/test_sdl2_ttf.c'), args=['--use-port=sdl2', '--use-port=sdl2_ttf'], output_filename='a.out.js')
2578+
self.emcc('browser/test_sdl2_ttf.c', args=['-sUSE_SDL=2', '-sUSE_SDL_TTF=2'], output_filename='a.out.js')
2579+
self.emcc('browser/test_sdl2_ttf.c', args=['--use-port=sdl2', '--use-port=sdl2_ttf'], output_filename='a.out.js')
25802580

25812581
@requires_network
25822582
def test_contrib_ports(self):
25832583
# Verify that contrib ports can be used (using the only contrib port available ATM, but can be replaced
25842584
# with a different contrib port when there is another one
2585-
self.emcc(test_file('other/test_contrib_ports.cpp'), ['--use-port=contrib.glfw3'])
2585+
self.emcc('other/test_contrib_ports.cpp', ['--use-port=contrib.glfw3'])
25862586

25872587
@requires_network
25882588
def test_remote_ports(self):
2589-
self.emcc(test_file('hello_world.c'), ['--use-port=emdawnwebgpu'])
2589+
self.emcc('hello_world.c', ['--use-port=emdawnwebgpu'])
25902590

25912591
@crossplatform
25922592
def test_external_ports_simple(self):
@@ -3534,7 +3534,7 @@ def test_embind_tsgen_ignore(self):
35343534
'-sSINGLE_FILE',
35353535
'-lembind', # Test duplicated link option.
35363536
]
3537-
self.emcc(test_file('other/embind_tsgen.cpp'), extra_args)
3537+
self.emcc('other/embind_tsgen.cpp', extra_args)
35383538
self.assertFileContents(test_file('other/embind_tsgen_ignore_1.d.ts'), read_file('embind_tsgen.d.ts'))
35393539
# Test these args separately since they conflict with arguments in the first test.
35403540
extra_args = ['-sMODULARIZE',
@@ -3543,24 +3543,24 @@ def test_embind_tsgen_ignore(self):
35433543
'-sEXPORT_ES6=1',
35443544
'-sASSERTIONS=0',
35453545
'-sSTRICT=1']
3546-
self.emcc(test_file('other/embind_tsgen.cpp'), extra_args)
3546+
self.emcc('other/embind_tsgen.cpp', extra_args)
35473547
self.assertFileContents(test_file('other/embind_tsgen_ignore_2.d.ts'), read_file('embind_tsgen.d.ts'))
35483548
# Also test this separately since it conflicts with other settings.
35493549
extra_args = ['-sWASM=0']
3550-
self.emcc(test_file('other/embind_tsgen.cpp'), extra_args)
3550+
self.emcc('other/embind_tsgen.cpp', extra_args)
35513551
self.assertFileContents(test_file('other/embind_tsgen_ignore_3.d.ts'), read_file('embind_tsgen.d.ts'))
35523552

35533553
extra_args = ['-fsanitize=undefined',
35543554
'-gsource-map']
3555-
self.emcc(test_file('other/embind_tsgen.cpp'), extra_args)
3555+
self.emcc('other/embind_tsgen.cpp', extra_args)
35563556
self.assertFileContents(test_file('other/embind_tsgen_ignore_3.d.ts'), read_file('embind_tsgen.d.ts'))
35573557

35583558
def test_embind_tsgen_worker_env(self):
35593559
self.cflags += ['-lembind', '--emit-tsd', 'embind_tsgen.d.ts']
35603560
# Passing -sWASM_WORKERS or -sPROXY_TO_WORKER requires the 'worker' environment
35613561
# at link time. Verify that TS binding generation still works in this case.
35623562
for flags in (['-sWASM_WORKERS'], ['-sPROXY_TO_WORKER', '-Wno-deprecated']):
3563-
self.emcc(test_file('other/embind_tsgen.cpp'), flags)
3563+
self.emcc('other/embind_tsgen.cpp', flags)
35643564
self.assertFileContents(test_file('other/embind_tsgen.d.ts'), read_file('embind_tsgen.d.ts'))
35653565

35663566
def test_embind_tsgen_dylink(self):
@@ -6665,7 +6665,7 @@ def test_modularize_run_dependency(self):
66656665
''')
66666666
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', '$addRunDependency,$removeRunDependency')
66676667
self.cflags += ['-sEXPORT_ES6', '-sMODULARIZE', '-sWASM_ASYNC_COMPILATION=0', '--pre-js=pre.js']
6668-
self.emcc(test_file('hello_world.c'), output_filename='hello_world.mjs')
6668+
self.emcc('hello_world.c', output_filename='hello_world.mjs')
66696669
self.assertContained('add-dep\nremove-dep\nhello, world!\ngot module\n', self.run_js('run.mjs'))
66706670

66716671
def test_modularize_instantiation_error(self):
@@ -7929,7 +7929,7 @@ def run(opts):
79297929
if malloc:
79307930
args += ['-sMALLOC=%s' % malloc]
79317931
print(args)
7932-
self.emcc(test_file('hello_libcxx.cpp'), args=args)
7932+
self.emcc('hello_libcxx.cpp', args=args)
79337933
sizes[name] = os.path.getsize('a.out.wasm')
79347934
print(sizes)
79357935
# dlmalloc is the default
@@ -14956,15 +14956,15 @@ def test_hello_world_argv(self):
1495614956
self.do_runf('hello_world_argv.c', 'hello, world! (1)')
1495714957

1495814958
def test_strict_closure(self):
14959-
self.emcc(test_file('hello_world.c'), ['-sSTRICT', '--closure=1'])
14959+
self.emcc('hello_world.c', ['-sSTRICT', '--closure=1'])
1496014960

1496114961
def test_closure_debug(self):
14962-
self.emcc(test_file('hello_world.c'), ['-sSTRICT', '--closure=1', '-g'])
14962+
self.emcc('hello_world.c', ['-sSTRICT', '--closure=1', '-g'])
1496314963
src = read_file('a.out.js')
1496414964
self.assertContained('$Module$$', src)
1496514965

1496614966
def test_arguments_global(self):
14967-
self.emcc(test_file('hello_world_argv.c'), ['-sENVIRONMENT=web', '-sSTRICT', '--closure=1', '-O2'])
14967+
self.emcc('hello_world_argv.c', ['-sENVIRONMENT=web', '-sSTRICT', '--closure=1', '-O2'])
1496814968

1496914969
@parameterized({
1497014970
'no_std_exp': (['-DEMMALLOC_NO_STD_EXPORTS'],),
@@ -15592,7 +15592,7 @@ def test_unsupported_min_version_when_unsupported_env(self, env):
1559215592
var MIN_SAFARI_VERSION = {{{ MIN_SAFARI_VERSION }}};
1559315593
var MIN_FIREFOX_VERSION = {{{ MIN_FIREFOX_VERSION }}};
1559415594
''')
15595-
self.emcc(test_file('hello_world.c'), [f'-sENVIRONMENT={env}', '--pre-js=pre.js'])
15595+
self.emcc('hello_world.c', [f'-sENVIRONMENT={env}', '--pre-js=pre.js'])
1559615596
src = read_file('a.out.js')
1559715597
unsupported = 0x7FFFFFFF
1559815598
self.assertContainedIf(f'var MIN_NODE_VERSION = {unsupported};', src, env == 'web')
@@ -15609,7 +15609,7 @@ def test_unsupported_min_version_when_unsupported_env(self, env):
1560915609
'wasm_workers': (['-sWASM_WORKERS'],),
1561015610
})
1561115611
def test_automatic_env_worker(self, env, args):
15612-
self.emcc(test_file('hello_world.c'), [f'-sENVIRONMENT={env}'] + args)
15612+
self.emcc('hello_world.c', [f'-sENVIRONMENT={env}'] + args)
1561315613

1561415614
def test_libcxx_errors(self):
1561515615
create_file('main.cpp', '''
@@ -15637,10 +15637,10 @@ def test_parsetools_make_removed_fs_assert(self):
1563715637

1563815638
removed_fs_assert_content = "IDBFS is no longer included by default"
1563915639

15640-
self.emcc(test_file('hello_world.c'), output_filename='hello_world.js')
15640+
self.emcc('hello_world.c', output_filename='hello_world.js')
1564115641
self.assertContained(removed_fs_assert_content, read_file('hello_world.js'))
1564215642

15643-
self.emcc(test_file('hello_world.c'), ['-lidbfs.js'], output_filename='hello_world.js')
15643+
self.emcc('hello_world.c', ['-lidbfs.js'], output_filename='hello_world.js')
1564415644
self.assertNotContained(removed_fs_assert_content, read_file('hello_world.js'))
1564515645

1564615646
@crossplatform

0 commit comments

Comments
 (0)