|
| 1 | +# Copyright 2026 The Emscripten Authors. All rights reserved. |
| 2 | +# Emscripten is available under two separate licenses, the MIT license and the |
| 3 | +# University of Illinois/NCSA Open Source License. Both these licenses can be |
| 4 | +# found in the LICENSE file. |
| 5 | + |
| 6 | +import os |
| 7 | +import shlex |
| 8 | +import subprocess |
| 9 | + |
| 10 | +from tools.shared import EMCC, PIPE |
| 11 | + |
| 12 | +from common import ( |
| 13 | + EMRUN, |
| 14 | + RunnerCore, |
| 15 | + path_from_root, |
| 16 | + read_file, |
| 17 | + test_file |
| 18 | +) |
| 19 | + |
| 20 | +from browser_common import ( |
| 21 | + BrowserCore, |
| 22 | + get_browser, |
| 23 | + has_browser |
| 24 | +) |
| 25 | + |
| 26 | +from test_browser import also_with_threads |
| 27 | + |
| 28 | +class emrun(RunnerCore): |
| 29 | + def test_emrun_info(self): |
| 30 | + if not has_browser(): |
| 31 | + self.skipTest('need a browser') |
| 32 | + result = self.run_process([EMRUN, '--system-info', '--browser_info'], stdout=PIPE).stdout |
| 33 | + assert 'CPU' in result |
| 34 | + assert 'Browser' in result |
| 35 | + assert 'Traceback' not in result |
| 36 | + |
| 37 | + result = self.run_process([EMRUN, '--list-browsers'], stdout=PIPE).stdout |
| 38 | + assert 'Traceback' not in result |
| 39 | + |
| 40 | + def test_no_browser(self): |
| 41 | + # Test --no-browser mode where we have to take care of launching the browser ourselves |
| 42 | + # and then killing emrun when we are done. |
| 43 | + if not has_browser(): |
| 44 | + self.skipTest('need a browser') |
| 45 | + |
| 46 | + self.run_process([EMCC, test_file('test_emrun.c'), '--emrun', '-o', 'hello_world.html']) |
| 47 | + proc = subprocess.Popen([EMRUN, '--no-browser', '.', '--port=3333'], stdout=PIPE) |
| 48 | + try: |
| 49 | + if get_browser(): |
| 50 | + url = 'http://localhost:3333/hello_world.html?argv0' |
| 51 | + print(f'Starting browser to {url}') |
| 52 | + BrowserCore.browser_open(url) |
| 53 | + try: |
| 54 | + while True: |
| 55 | + stdout = proc.stdout.read() |
| 56 | + if b'Dumping out file' in stdout: |
| 57 | + break |
| 58 | + finally: |
| 59 | + print('Terminating browser') |
| 60 | + BrowserCore.browser_terminate() |
| 61 | + finally: |
| 62 | + print('Terminating emrun server') |
| 63 | + proc.terminate() |
| 64 | + proc.wait() |
| 65 | + |
| 66 | + def test_program_arg_separator(self): |
| 67 | + # Verify that trying to pass argument to the page without the `--` separator will |
| 68 | + # generate an actionable error message |
| 69 | + err = self.expect_fail([EMRUN, '--foo']) |
| 70 | + self.assertContained('error: unrecognized arguments: --foo', err) |
| 71 | + self.assertContained('remember to add `--` between arguments', err) |
| 72 | + |
| 73 | + @also_with_threads |
| 74 | + def test_emrun(self): |
| 75 | + self.emcc('test_emrun.c', ['--emrun', '-o', 'test_emrun.html']) |
| 76 | + if not has_browser(): |
| 77 | + self.skipTest('need a browser') |
| 78 | + |
| 79 | + # We cannot run emrun from the temp directory the suite will clean up afterwards, since the |
| 80 | + # browser that is launched will have that directory as startup directory, and the browser will |
| 81 | + # not close as part of the test, pinning down the cwd on Windows and it wouldn't be possible to |
| 82 | + # delete it. Therefore switch away from that directory before launching. |
| 83 | + os.chdir(path_from_root()) |
| 84 | + |
| 85 | + # emrun tests may run in parallel processes, so each test case should use a unique port number |
| 86 | + # to avoid port address already in use errors. |
| 87 | + port = '6939' |
| 88 | + if '-pthread' in self.cflags: |
| 89 | + port = '6940' |
| 90 | + |
| 91 | + args_base = [EMRUN, '--timeout', '30', '--safe_firefox_profile', |
| 92 | + '--kill-exit', '--port', port, '--verbose', |
| 93 | + '--log-stdout', self.in_dir('stdout.txt'), |
| 94 | + '--log-stderr', self.in_dir('stderr.txt')] |
| 95 | + |
| 96 | + if get_browser() is not None: |
| 97 | + # If EMTEST_BROWSER carried command line arguments to pass to the browser, |
| 98 | + # (e.g. "firefox -profile /path/to/foo") those can't be passed via emrun, |
| 99 | + # so strip them out. |
| 100 | + browser_cmd = shlex.split(get_browser()) |
| 101 | + browser_path = browser_cmd[0] |
| 102 | + args_base += ['--browser', browser_path] |
| 103 | + if len(browser_cmd) > 1: |
| 104 | + browser_args = browser_cmd[1:] |
| 105 | + if 'firefox' in browser_path and ('-profile' in browser_args or '--profile' in browser_args): |
| 106 | + # emrun uses its own -profile, strip it out |
| 107 | + parser = argparse.ArgumentParser(add_help=False) # otherwise it throws with -headless |
| 108 | + parser.add_argument('-profile') |
| 109 | + parser.add_argument('--profile') |
| 110 | + browser_args = parser.parse_known_args(browser_args)[1] |
| 111 | + if browser_args: |
| 112 | + args_base += ['--browser_args', ' ' + ' '.join(browser_args)] |
| 113 | + |
| 114 | + for args in [ |
| 115 | + [], |
| 116 | + ['--port', '0'], |
| 117 | + ['--private_browsing'], |
| 118 | + ['--dump_out_directory', 'other dir/multiple'], |
| 119 | + ['--dump_out_directory=foo_bar'], |
| 120 | + ]: |
| 121 | + args = args_base + args + [self.in_dir('test_emrun.html'), '--', '1', '2', '--3', 'escaped space', 'with_underscore'] |
| 122 | + print(shlex.join(args)) |
| 123 | + proc = self.run_process(args, check=False) |
| 124 | + self.assertEqual(proc.returncode, 100) |
| 125 | + dump_dir = 'dump_out' |
| 126 | + if '--dump_out_directory' in args: |
| 127 | + dump_dir = 'other dir/multiple' |
| 128 | + elif '--dump_out_directory=foo_bar' in args: |
| 129 | + dump_dir = 'foo_bar' |
| 130 | + self.assertExists(self.in_dir(f'{dump_dir}/test.dat')) |
| 131 | + self.assertExists(self.in_dir(f'{dump_dir}/heap.dat')) |
| 132 | + self.assertExists(self.in_dir(f'{dump_dir}/nested/with space.dat')) |
| 133 | + stdout = read_file(self.in_dir('stdout.txt')) |
| 134 | + stderr = read_file(self.in_dir('stderr.txt')) |
| 135 | + self.assertContained('argc: 6', stdout) |
| 136 | + self.assertContained('argv[3]: --3', stdout) |
| 137 | + self.assertContained('argv[4]: escaped space', stdout) |
| 138 | + self.assertContained('argv[5]: with_underscore', stdout) |
| 139 | + self.assertContained('Hello, world!', stdout) |
| 140 | + self.assertContained('Testing ASCII characters: !"$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', stdout) |
| 141 | + self.assertContained('Testing char sequences: %20%21 ä', stdout) |
| 142 | + self.assertContained('hello, error stream!', stderr) |
0 commit comments