Skip to content

Commit 8c0d94f

Browse files
authored
[test] Convert with_pass_debug to a python context manager. NFC (#8337)
1 parent 5d74c9c commit 8c0d94f

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

check.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ def run_wasm_dis_tests():
7777
shared.fail_if_not_identical_to_file(actual, t + '.fromBinary')
7878

7979
# also verify there are no validation errors
80-
def check():
80+
with shared.with_pass_debug():
8181
cmd = shared.WASM_OPT + [t, '-all', '-q']
8282
support.run_command(cmd)
8383

84-
shared.with_pass_debug(check)
85-
8684

8785
def run_crash_tests():
8886
print("\n[ checking we don't crash on tricky inputs... ]\n")
@@ -392,34 +390,30 @@ def run_unittest():
392390
raise Exception("unittest failed")
393391

394392

393+
@shared.with_pass_debug()
395394
def run_lit():
396-
def run():
397-
lit_script = os.path.join(shared.options.binaryen_bin, 'binaryen-lit')
398-
lit_tests = os.path.join(shared.options.binaryen_root, 'test', 'lit')
399-
# lit expects to be run as its own executable
400-
cmd = [sys.executable, lit_script, lit_tests, '-vv']
401-
result = subprocess.run(cmd)
402-
if result.returncode != 0:
403-
shared.num_failures += 1
404-
if shared.options.abort_on_first_failure and shared.num_failures:
405-
raise Exception("lit test failed")
406-
407-
shared.with_pass_debug(run)
395+
lit_script = os.path.join(shared.options.binaryen_bin, 'binaryen-lit')
396+
lit_tests = os.path.join(shared.options.binaryen_root, 'test', 'lit')
397+
# lit expects to be run as its own executable
398+
cmd = [sys.executable, lit_script, lit_tests, '-vv']
399+
result = subprocess.run(cmd)
400+
if result.returncode != 0:
401+
shared.num_failures += 1
402+
if shared.options.abort_on_first_failure and shared.num_failures:
403+
raise Exception("lit test failed")
408404

409405

406+
@shared.with_pass_debug()
410407
def run_gtest():
411-
def run():
412-
gtest = os.path.join(shared.options.binaryen_bin, 'binaryen-unittests')
413-
if not os.path.isfile(gtest):
414-
shared.warn('gtest binary not found - skipping tests')
415-
else:
416-
result = subprocess.run(gtest)
417-
if result.returncode != 0:
418-
shared.num_failures += 1
419-
if shared.options.abort_on_first_failure and shared.num_failures:
420-
raise Exception("gtest test failed")
421-
422-
shared.with_pass_debug(run)
408+
gtest = os.path.join(shared.options.binaryen_bin, 'binaryen-unittests')
409+
if not os.path.isfile(gtest):
410+
shared.warn('gtest binary not found - skipping tests')
411+
else:
412+
result = subprocess.run(gtest)
413+
if result.returncode != 0:
414+
shared.num_failures += 1
415+
if shared.options.abort_on_first_failure and shared.num_failures:
416+
raise Exception("gtest test failed")
423417

424418

425419
TEST_SUITES = OrderedDict([

scripts/test/shared.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import stat
2323
import subprocess
2424
import sys
25+
from contextlib import contextmanager
2526
from pathlib import Path
2627

2728
# The C++ standard whose features are required to build Binaryen.
@@ -514,12 +515,12 @@ def binary_format_check(wast, verify_final_result=True, wasm_as_args=['-g'],
514515
return disassembled_file
515516

516517

517-
# run a check with BINARYEN_PASS_DEBUG set, to do full validation
518-
def with_pass_debug(check):
518+
@contextmanager
519+
def with_pass_debug():
519520
old_pass_debug = os.environ.get('BINARYEN_PASS_DEBUG')
521+
os.environ['BINARYEN_PASS_DEBUG'] = '1'
520522
try:
521-
os.environ['BINARYEN_PASS_DEBUG'] = '1'
522-
check()
523+
yield
523524
finally:
524525
if old_pass_debug is not None:
525526
os.environ['BINARYEN_PASS_DEBUG'] = old_pass_debug

scripts/test/wasm2js.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def test_wasm2js_output():
104104
cmd += ['--allow-asserts']
105105
js = support.run_command(cmd)
106106
# also verify it passes pass-debug verifications
107-
shared.with_pass_debug(lambda: support.run_command(cmd, stderr=subprocess.PIPE))
107+
with shared.with_pass_debug():
108+
support.run_command(cmd, stderr=subprocess.PIPE)
108109

109110
open('a.2asm.asserts.mjs', 'w').write(js)
110111

scripts/test/wasm_opt.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ def test_wasm_opt():
7272
shared.fail_if_not_contained(actual, debugged)
7373

7474
# also check pass-debug mode
75-
def check():
75+
with shared.with_pass_debug():
7676
# ignore stderr, as the pass-debug output is very verbose in CI
7777
pass_debug = support.run_command(cmd, stderr=subprocess.PIPE)
7878
shared.fail_if_not_identical(curr, pass_debug)
79-
shared.with_pass_debug(check)
8079

8180
expected_file = os.path.join(shared.get_test_dir('passes'), base + ('.bin' if binary else '') + '.txt')
8281
shared.fail_if_not_identical_to_file(actual, expected_file)

0 commit comments

Comments
 (0)