Skip to content

Commit e94989d

Browse files
authored
Automatically report exit status in browser tests (#12928)
This means that tests don't necessarily need to call REPORT_RESULT. I'm going to be taking advantage of this in #12923. Also, don't initialize EXIT_STATUS to zero, but leave it as undefined. It seems useful to be able to tell the diference between a program that exits with a zero status and one that has not in fact exited yet.
1 parent 9e03037 commit e94989d

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

tests/browser_reporting.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var hasModule = typeof Module === 'object' && Module;
2+
13
/** @param {boolean=} sync
24
@param {number=} port */
35
function reportResultToServer(result, sync, port) {
@@ -7,9 +9,7 @@ function reportResultToServer(result, sync, port) {
79
reportErrorToServer("excessive reported results, sending " + result + ", test will fail");
810
}
911
reportResultToServer.reported = true;
10-
1112
var xhr = new XMLHttpRequest();
12-
var hasModule = typeof Module === 'object' && Module;
1313
if (hasModule && Module['pageThrewException']) result = 12345;
1414
xhr.open('GET', 'http://localhost:' + port + '/report_result?' + result, !sync);
1515
xhr.send();
@@ -36,3 +36,9 @@ if (typeof window === 'object' && window) {
3636
xhr.send();
3737
});
3838
}
39+
40+
if (hasModule) {
41+
Module['onExit'] = function(status) {
42+
maybeReportResultToServer('exit:' + status);
43+
}
44+
}

tests/runner.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,22 +1484,26 @@ def reftest(self, expected, manually_trigger=False):
14841484
}
14851485
''' % (reporting.read(), basename, int(manually_trigger)))
14861486

1487-
def compile_btest(self, args):
1487+
def compile_btest(self, args, reporting=True):
14881488
# add in support for reporting results. this adds an include a header so testcases can
14891489
# use REPORT_RESULT, and also adds a cpp file to be compiled alongside the testcase, which
14901490
# contains the implementation of REPORT_RESULT (we can't just include that implementation in
14911491
# the header as there may be multiple files being compiled here).
1492-
args += ['-s', 'IN_TEST_HARNESS=1', '-DEMTEST_PORT_NUMBER=%d' % self.port,
1493-
'-I', path_from_root('tests'),
1494-
'-include', path_from_root('tests', 'report_result.h'),
1495-
path_from_root('tests', 'report_result.cpp')]
1496-
self.run_process([EMCC] + args + ['--pre-js', path_from_root('tests', 'browser_reporting.js')])
1492+
args += ['-s', 'IN_TEST_HARNESS=1']
1493+
if reporting:
1494+
args += ['-DEMTEST_PORT_NUMBER=%d' % self.port,
1495+
'-I', path_from_root('tests'),
1496+
'-include', path_from_root('tests', 'report_result.h'),
1497+
path_from_root('tests', 'report_result.cpp'),
1498+
'--pre-js', path_from_root('tests', 'browser_reporting.js')]
1499+
self.run_process([EMCC] + args)
14971500

14981501
def btest(self, filename, expected=None, reference=None, force_c=False,
14991502
reference_slack=0, manual_reference=False, post_build=None,
15001503
args=None, message='.', also_proxied=False,
15011504
url_suffix='', timeout=None, also_asmjs=False,
1502-
manually_trigger_reftest=False, extra_tries=1):
1505+
manually_trigger_reftest=False, extra_tries=1,
1506+
reporting=True):
15031507
assert expected or reference, 'a btest must either expect an output, or have a reference image'
15041508
if args is None:
15051509
args = []
@@ -1523,7 +1527,7 @@ def btest(self, filename, expected=None, reference=None, force_c=False,
15231527
args = [filepath, '-o', outfile] + args
15241528
# print('all args:', args)
15251529
try_delete(outfile)
1526-
self.compile_btest(args)
1530+
self.compile_btest(args, reporting)
15271531
self.assertExists(outfile)
15281532
if post_build:
15291533
post_build()

tests/test_browser.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,15 +2400,17 @@ def test_runtime_misuse(self):
24002400
]:
24012401
for mode in [[], ['-s', 'WASM=0']]:
24022402
print('\n', filename, extra_args, mode)
2403+
24032404
print('mem init, so async, call too early')
24042405
create_test_file('post.js', post_prep + post_test + post_hook)
2405-
self.btest(filename, expected='600', args=['--post-js', 'post.js', '--memory-init-file', '1', '-s', 'EXIT_RUNTIME=1'] + extra_args + mode)
2406+
self.btest(filename, expected='600', args=['--post-js', 'post.js', '--memory-init-file', '1', '-s', 'EXIT_RUNTIME=1'] + extra_args + mode, reporting=False)
24062407
print('sync startup, call too late')
24072408
create_test_file('post.js', post_prep + 'Module.postRun.push(function() { ' + post_test + ' });' + post_hook)
2408-
self.btest(filename, expected=str(second_code), args=['--post-js', 'post.js', '-s', 'EXIT_RUNTIME=1'] + extra_args + mode)
2409+
self.btest(filename, expected=str(second_code), args=['--post-js', 'post.js', '-s', 'EXIT_RUNTIME=1'] + extra_args + mode, reporting=False)
2410+
24092411
print('sync, runtime still alive, so all good')
24102412
create_test_file('post.js', post_prep + 'expected_ok = true; Module.postRun.push(function() { ' + post_test + ' });' + post_hook)
2411-
self.btest(filename, expected='606', args=['--post-js', 'post.js'] + extra_args + mode)
2413+
self.btest(filename, expected='606', args=['--post-js', 'post.js'] + extra_args + mode, reporting=False)
24122414

24132415
def test_cwrap_early(self):
24142416
self.btest(os.path.join('browser', 'cwrap_early.cpp'), args=['-O2', '-s', 'ASSERTIONS=1', '--pre-js', path_from_root('tests', 'browser', 'cwrap_early.js'), '-s', 'EXTRA_EXPORTED_RUNTIME_METHODS=["cwrap"]'], expected='0')
@@ -4539,14 +4541,13 @@ def test_base64_atob_fallback(self):
45394541
#include <stdio.h>
45404542
#include <emscripten.h>
45414543
int main() {
4542-
REPORT_RESULT(0);
45434544
return 0;
45444545
}
45454546
''')
45464547
# generate a dummy file
45474548
create_test_file('dummy_file', 'dummy')
45484549
# compile the code with the modularize feature and the preload-file option enabled
4549-
self.compile_btest(['test.c', '-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME="Foo"', '--preload-file', 'dummy_file', '-s', 'SINGLE_FILE=1'])
4550+
self.compile_btest(['test.c', '-s', 'EXIT_RUNTIME', '-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME="Foo"', '--preload-file', 'dummy_file', '-s', 'SINGLE_FILE=1'])
45504551
create_test_file('a.html', '''
45514552
<script>
45524553
atob = undefined;
@@ -4557,7 +4558,7 @@ def test_base64_atob_fallback(self):
45574558
var foo = Foo();
45584559
</script>
45594560
''')
4560-
self.run_browser('a.html', '...', '/report_result?0')
4561+
self.run_browser('a.html', '...', '/report_result?exit:0')
45614562

45624563
# Tests that SINGLE_FILE works as intended in generated HTML (with and without Worker)
45634564
def test_single_file_html(self):

0 commit comments

Comments
 (0)