Skip to content

Commit dac89f0

Browse files
authored
[test] Add @with_both_compilers decorator (#20751)
I'm not sure how useful it is to run all these test twice, but this at least allows is to easily track how common this pattern is.
1 parent 8a7ed56 commit dac89f0

File tree

1 file changed

+58
-43
lines changed

1 file changed

+58
-43
lines changed

test/test_other.py

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,18 @@ def decorated(self):
8080
return decorated
8181

8282

83+
def with_both_compilers(f):
84+
assert callable(f)
85+
86+
f._parameterize = {'': (EMCC,),
87+
'emxx': (EMXX,)}
88+
return f
89+
90+
8391
def also_with_wasmfs(f):
92+
assert callable(f)
93+
94+
@wraps(f)
8495
def metafunc(self, wasmfs):
8596
if wasmfs:
8697
self.set_setting('WASMFS')
@@ -107,6 +118,9 @@ def metafunc(self, backend):
107118

108119

109120
def also_with_wasmfs_all_backends(f):
121+
assert callable(f)
122+
123+
@wraps(f)
110124
def metafunc(self, backend):
111125
if backend:
112126
self.set_setting('WASMFS')
@@ -243,20 +257,20 @@ def parse_wasm(self, filename):
243257
# This needs to work because many tools run `emcc -v` internally and it should
244258
# always work even if the user has `EMCC_CFLAGS` set.
245259
@with_env_modify({'EMCC_CFLAGS': '-should -be -ignored'})
260+
@with_both_compilers
246261
@crossplatform
247-
def test_emcc_v(self):
248-
for compiler in [EMCC, EMXX]:
249-
# -v, without input files
250-
proc = self.run_process([compiler, '-v'], stdout=PIPE, stderr=PIPE)
251-
self.assertEqual(proc.stdout, '')
252-
# assert that the emcc message comes first. We had a bug where the sub-process output
253-
# from clang would be flushed to stderr first.
254-
self.assertContained('emcc (Emscripten gcc/clang-like replacement', proc.stderr)
255-
self.assertTrue(proc.stderr.startswith('emcc (Emscripten gcc/clang-like replacement'))
256-
self.assertContained('clang version ', proc.stderr)
257-
self.assertContained('GNU', proc.stderr)
258-
self.assertContained('Target: wasm32-unknown-emscripten', proc.stderr)
259-
self.assertNotContained('this is dangerous', proc.stderr)
262+
def test_emcc_v(self, compiler):
263+
# -v, without input files
264+
proc = self.run_process([compiler, '-v'], stdout=PIPE, stderr=PIPE)
265+
self.assertEqual(proc.stdout, '')
266+
# assert that the emcc message comes first. We had a bug where the sub-process output
267+
# from clang would be flushed to stderr first.
268+
self.assertContained('emcc (Emscripten gcc/clang-like replacement', proc.stderr)
269+
self.assertTrue(proc.stderr.startswith('emcc (Emscripten gcc/clang-like replacement'))
270+
self.assertContained('clang version ', proc.stderr)
271+
self.assertContained('GNU', proc.stderr)
272+
self.assertContained('Target: wasm32-unknown-emscripten', proc.stderr)
273+
self.assertNotContained('this is dangerous', proc.stderr)
260274

261275
def test_log_subcommands(self):
262276
# `-v` when combined with other arguments will trace the subcommands
@@ -281,16 +295,16 @@ def test_emcc_check(self):
281295
proc = self.run_process([EMCC, '--check'], stdout=PIPE, stderr=PIPE)
282296
self.assertContained('Running sanity checks', proc.stderr)
283297

284-
def test_emcc_generate_config(self):
298+
@with_both_compilers
299+
def test_emcc_generate_config(self, compiler):
285300
config_path = './emscripten_config'
286301
with env_modify({'EM_CONFIG': config_path}):
287-
for compiler in [EMCC, EMXX]:
288-
self.assertNotExists(config_path)
289-
self.run_process([compiler, '--generate-config'])
290-
self.assertExists(config_path)
291-
config_contents = read_file(config_path)
292-
self.assertContained('LLVM_ROOT', config_contents)
293-
os.remove(config_path)
302+
self.assertNotExists(config_path)
303+
self.run_process([compiler, '--generate-config'])
304+
self.assertExists(config_path)
305+
config_contents = read_file(config_path)
306+
self.assertContained('LLVM_ROOT', config_contents)
307+
os.remove(config_path)
294308

295309
@parameterized({
296310
'': ([],),
@@ -385,7 +399,8 @@ def test_emcc_out_file(self):
385399

386400
@parameterized({
387401
'c': [EMCC, '.c'],
388-
'cxx': [EMXX, '.cpp']})
402+
'cxx': [EMXX, '.cpp'],
403+
})
389404
def test_emcc_basics(self, compiler, suffix):
390405
# emcc src.cpp ==> writes a.out.js and a.out.wasm
391406
self.run_process([compiler, test_file('hello_world' + suffix)])
@@ -1050,23 +1065,23 @@ def verify_includes(stderr):
10501065
err = self.run_process([EMXX, test_file('hello_world.cpp'), '-v'], stderr=PIPE).stderr
10511066
verify_includes(err)
10521067

1053-
def test_failure_error_code(self):
1054-
for compiler in [EMCC, EMXX]:
1055-
# Test that if one file is missing from the build, then emcc shouldn't succeed, and shouldn't produce an output file.
1056-
self.expect_fail([compiler, test_file('hello_world.c'), 'this_file_is_missing.c', '-o', 'out.js'])
1057-
self.assertFalse(os.path.exists('out.js'))
1068+
@with_both_compilers
1069+
def test_failure_error_code(self, compiler):
1070+
# Test that if one file is missing from the build, then emcc shouldn't succeed, and shouldn't produce an output file.
1071+
self.expect_fail([compiler, test_file('hello_world.c'), 'this_file_is_missing.c', '-o', 'out.js'])
1072+
self.assertFalse(os.path.exists('out.js'))
10581073

1059-
def test_failure_modularize_and_catch_rejection(self):
1060-
for compiler in [EMCC, EMXX]:
1061-
# Test that if sMODULARIZE and sNODEJS_CATCH_REJECTION are both enabled, then emcc shouldn't succeed, and shouldn't produce an output file.
1062-
self.expect_fail([compiler, test_file('hello_world.c'), '-sMODULARIZE', '-sNODEJS_CATCH_REJECTION', '-o', 'out.js'])
1063-
self.assertFalse(os.path.exists('out.js'))
1074+
@with_both_compilers
1075+
def test_failure_modularize_and_catch_rejection(self, compiler):
1076+
# Test that if sMODULARIZE and sNODEJS_CATCH_REJECTION are both enabled, then emcc shouldn't succeed, and shouldn't produce an output file.
1077+
self.expect_fail([compiler, test_file('hello_world.c'), '-sMODULARIZE', '-sNODEJS_CATCH_REJECTION', '-o', 'out.js'])
1078+
self.assertFalse(os.path.exists('out.js'))
10641079

1065-
def test_failure_modularize_and_catch_exit(self):
1066-
for compiler in [EMCC, EMXX]:
1067-
# Test that if sMODULARIZE and sNODEJS_CATCH_EXIT are both enabled, then emcc shouldn't succeed, and shouldn't produce an output file.
1068-
self.expect_fail([compiler, test_file('hello_world.c'), '-sMODULARIZE', '-sNODEJS_CATCH_EXIT', '-o', 'out.js'])
1069-
self.assertFalse(os.path.exists('out.js'))
1080+
@with_both_compilers
1081+
def test_failure_modularize_and_catch_exit(self, compiler):
1082+
# Test that if sMODULARIZE and sNODEJS_CATCH_EXIT are both enabled, then emcc shouldn't succeed, and shouldn't produce an output file.
1083+
self.expect_fail([compiler, test_file('hello_world.c'), '-sMODULARIZE', '-sNODEJS_CATCH_EXIT', '-o', 'out.js'])
1084+
self.assertFalse(os.path.exists('out.js'))
10701085

10711086
def test_use_cxx(self):
10721087
create_file('empty_file', ' ')
@@ -1075,18 +1090,18 @@ def test_use_cxx(self):
10751090
dash_xcpp = self.run_process([EMCC, '-v', '-xc++', 'empty_file'], stderr=PIPE).stderr
10761091
self.assertContained('-x c++', dash_xcpp)
10771092

1078-
def test_cxx11(self):
1093+
@with_both_compilers
1094+
def test_cxx11(self, compiler):
10791095
for std in ['-std=c++11', '--std=c++11']:
1080-
for compiler in [EMCC, EMXX]:
1081-
self.run_process([compiler, std, test_file('hello_cxx11.cpp')])
1096+
self.run_process([compiler, std, test_file('hello_cxx11.cpp')])
10821097

10831098
# Regression test for issue #4522: Incorrect CC vs CXX detection
1084-
def test_incorrect_c_detection(self):
1099+
@with_both_compilers
1100+
def test_incorrect_c_detection(self, compiler):
10851101
# This auto-detection only works for the compile phase.
10861102
# For linking you need to use `em++` or pass `-x c++`
10871103
create_file('test.c', 'foo\n')
1088-
for compiler in [EMCC, EMXX]:
1089-
self.run_process([compiler, '-c', '-lembind', '--embed-file', 'test.c', test_file('hello_world.cpp')])
1104+
self.run_process([compiler, '-c', '-lembind', '--embed-file', 'test.c', test_file('hello_world.cpp')])
10901105

10911106
def test_odd_suffixes(self):
10921107
for suffix in ['CPP', 'c++', 'C++', 'cxx', 'CXX', 'cc', 'CC']:

0 commit comments

Comments
 (0)