From e3c9379e7e7fdf1c4298f1b9bbaff5b372c65580 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 2 Oct 2024 15:12:45 -0700 Subject: [PATCH] Refactor pre/postRun tests. NFC --- test/test_other.py | 69 +++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/test/test_other.py b/test/test_other.py index 7680a736e649..67d6bf597077 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -2719,7 +2719,12 @@ def test_get_proc_address_error_message(self): err = self.expect_fail([EMCC, '-sGL_ENABLE_GET_PROC_ADDRESS=0', test_file('other/test_GetProcAddress_LEGACY_GL_EMULATION.c')]) self.assertContained('error: linker: Undefined symbol: SDL_GL_GetProcAddress(). Please pass -sGL_ENABLE_GET_PROC_ADDRESS at link time to link in SDL_GL_GetProcAddress().', err) - def test_prepost(self): + @parameterized({ + '': (False, False), + 'no_initial_run': (True, False), + 'run_dep': (False, True), + }) + def test_prepost(self, no_initial_run, run_dep): create_file('main.c', ''' #include int main() { @@ -2748,29 +2753,27 @@ def test_prepost(self): self.assertNotContained('post-run\n', output) # noInitialRun prevents run - for no_initial_run, run_dep in ((0, 0), (1, 0), (0, 1)): - print(no_initial_run, run_dep) - args = ['-sWASM_ASYNC_COMPILATION=0', '-sEXPORTED_RUNTIME_METHODS=callMain'] - if no_initial_run: - args += ['-sINVOKE_RUN=0'] - if run_dep: - create_file('pre.js', 'Module.preRun = () => addRunDependency("test");') - create_file('post.js', 'removeRunDependency("test");') - args += ['--pre-js', 'pre.js', '--post-js', 'post.js'] - - self.run_process([EMCC, 'main.c'] + args) - output = self.run_js('a.out.js') - self.assertContainedIf('hello from main', output, not no_initial_run) + args = ['-sWASM_ASYNC_COMPILATION=0', '-sEXPORTED_RUNTIME_METHODS=callMain'] + if no_initial_run: + args += ['-sINVOKE_RUN=0'] + if run_dep: + create_file('pre.js', 'Module.preRun = () => addRunDependency("test");') + create_file('post.js', 'removeRunDependency("test");') + args += ['--pre-js', 'pre.js', '--post-js', 'post.js'] - if no_initial_run: - # Calling main later should still work, filesystem etc. must be set up. - print('call main later') - src = read_file('a.out.js') - src += '\nout("callMain -> " + Module.callMain());\n' - create_file('a.out.js', src) - self.assertContained('hello from main\ncallMain -> 0\n', self.run_js('a.out.js')) + self.run_process([EMCC, 'main.c'] + args) + output = self.run_js('a.out.js') + self.assertContainedIf('hello from main', output, not no_initial_run) - # Use postInit + if no_initial_run: + # Calling main later should still work, filesystem etc. must be set up. + print('call main later') + src = read_file('a.out.js') + src += '\nout("callMain -> " + Module.callMain());\n' + create_file('a.out.js', src) + self.assertContained('hello from main\ncallMain -> 0\n', self.run_js('a.out.js')) + + def test_preinit(self): create_file('pre.js', ''' var Module = { preRun: () => out('pre-run'), @@ -2778,38 +2781,24 @@ def test_prepost(self): preInit: () => out('pre-init') }; ''') - self.do_runf('main.c', - 'pre-init\npre-run\nhello from main\npost-run\n', + self.do_runf(test_file('hello_world.c'), + 'pre-init\npre-run\nhello, world!\npost-run\n', emcc_args=['--pre-js', 'pre.js']) def test_prepost2(self): - create_file('main.c', ''' - #include - int main() { - printf("hello from main\\n"); - return 0; - } - ''') create_file('pre.js', 'Module.preRun = () => out("pre-run");') create_file('pre2.js', 'Module.postRun = () => out("post-run");') - self.do_runf('main.c', 'pre-run\nhello from main\npost-run\n', + self.do_runf(test_file('hello_world.c'), 'pre-run\nhello, world!\npost-run\n', emcc_args=['--pre-js', 'pre.js', '--pre-js', 'pre2.js']) def test_prepre(self): - create_file('main.c', ''' - #include - int main() { - printf("hello from main\\n"); - return 0; - } - ''') create_file('pre.js', ''' Module.preRun = [() => out('pre-run')]; ''') create_file('pre2.js', ''' Module.preRun.push(() => out('prepre')); ''') - self.do_runf('main.c', 'prepre\npre-run\nhello from main\n', + self.do_runf(test_file('hello_world.c'), 'prepre\npre-run\nhello, world!\n', emcc_args=['--pre-js', 'pre.js', '--pre-js', 'pre2.js']) def test_extern_prepost(self):