diff options
author | Heejin Ahn <aheejin@gmail.com> | 2019-11-20 16:37:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-20 16:37:09 -0800 |
commit | 21888c253f775047bcfac28e8110abdcad9d6bcb (patch) | |
tree | 18b7cf5d6b76a554401d7edb3601fe7be9ae8739 /scripts | |
parent | f17f8f927c96c2e6ec25b4c2dbb8b9d70f7af9b7 (diff) | |
download | binaryen-21888c253f775047bcfac28e8110abdcad9d6bcb.tar.gz binaryen-21888c253f775047bcfac28e8110abdcad9d6bcb.tar.bz2 binaryen-21888c253f775047bcfac28e8110abdcad9d6bcb.zip |
Simplify test scripts (NFC) (#2457)
This makes test scripts simpler by reducing loop depths and extracting
repeating code into methods or variables.
- `get_tests` returns a list of tests with specified extensions. This
includes files with a full path rather than just file names.
- Reduces loop depths by using early exits and `get_tests`.
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/test/asm2wasm.py | 24 | ||||
-rwxr-xr-x | scripts/test/lld.py | 6 | ||||
-rw-r--r-- | scripts/test/shared.py | 24 | ||||
-rwxr-xr-x | scripts/test/wasm2js.py | 38 |
4 files changed, 45 insertions, 47 deletions
diff --git a/scripts/test/asm2wasm.py b/scripts/test/asm2wasm.py index 356a3d85c..662daf387 100755 --- a/scripts/test/asm2wasm.py +++ b/scripts/test/asm2wasm.py @@ -20,19 +20,18 @@ import subprocess from .support import run_command from .shared import ( ASM2WASM, WASM_OPT, binary_format_check, delete_from_orbit, - fail_with_error, options, tests, fail_if_not_identical_to_file + fail_with_error, options, fail_if_not_identical_to_file, get_tests ) def test_asm2wasm(): print('[ checking asm2wasm testcases... ]\n') - for asm in tests: - if not asm.endswith('.asm.js'): - continue + for asm in get_tests(options.binaryen_test, ['.asm.js']): + basename = os.path.basename(asm) for precise in [0, 1, 2]: for opts in [1, 0]: - cmd = ASM2WASM + [os.path.join(options.binaryen_test, asm)] + cmd = ASM2WASM + [asm] if 'threads' in asm: cmd += ['--enable-threads'] wasm = asm.replace('.asm.js', '.fromasm') @@ -48,22 +47,21 @@ def test_asm2wasm(): cmd += ['-O0'] # test that -O0 does nothing else: cmd += ['-O'] - if 'debugInfo' in asm: + if 'debugInfo' in basename: cmd += ['-g'] - if 'noffi' in asm: + if 'noffi' in basename: cmd += ['--no-legalize-javascript-ffi'] if precise and opts: # test mem init importing - open('a.mem', 'w').write(asm) + open('a.mem', 'w').write(basename) cmd += ['--mem-init=a.mem'] - if asm[0] == 'e': + if basename[0] == 'e': cmd += ['--mem-base=1024'] - if '4GB' in asm: + if '4GB' in basename: cmd += ['--mem-max=4294967296'] - if 'i64' in asm or 'wasm-only' in asm or 'noffi' in asm: + if 'i64' in basename or 'wasm-only' in basename or 'noffi' in basename: cmd += ['--wasm-only'] - wasm = os.path.join(options.binaryen_test, wasm) - print('..', asm, wasm) + print('..', basename, os.path.basename(wasm)) def do_asm2wasm_test(): actual = run_command(cmd) diff --git a/scripts/test/lld.py b/scripts/test/lld.py index 7372698cd..41db2baa2 100755 --- a/scripts/test/lld.py +++ b/scripts/test/lld.py @@ -16,7 +16,7 @@ import os from .support import run_command from .shared import ( - fail_with_error, files_with_pattern, options, + fail_with_error, get_test_dir, get_tests, WASM_EMSCRIPTEN_FINALIZE, fail_if_not_identical_to_file ) @@ -35,7 +35,7 @@ def args_for_finalize(filename): def test_wasm_emscripten_finalize(): print('\n[ checking wasm-emscripten-finalize testcases... ]\n') - for wast_path in files_with_pattern(options.binaryen_test, 'lld', '*.wast'): + for wast_path in get_tests(get_test_dir('lld'), ['.wast']): print('..', wast_path) is_passive = '.passive.' in wast_path mem_file = wast_path + '.mem' @@ -69,7 +69,7 @@ def test_wasm_emscripten_finalize(): def update_lld_tests(): print('\n[ updatring wasm-emscripten-finalize testcases... ]\n') - for wast_path in files_with_pattern(options.binaryen_test, 'lld', '*.wast'): + for wast_path in get_tests(get_test_dir('lld'), ['.wast']): print('..', wast_path) is_passive = '.passive.' in wast_path mem_file = wast_path + '.mem' diff --git a/scripts/test/shared.py b/scripts/test/shared.py index f8d575656..bcaffe0b8 100644 --- a/scripts/test/shared.py +++ b/scripts/test/shared.py @@ -373,10 +373,22 @@ def fail_if_not_identical_to_file(actual, expected_file): fail_if_not_identical(actual, f.read(), fromfile=expected_file) -if len(requested) == 0: - tests = sorted(os.listdir(os.path.join(options.binaryen_test))) -else: - tests = requested[:] +def get_test_dir(name): + """Returns the test directory located at BINARYEN_ROOT/test/[name].""" + return os.path.join(options.binaryen_test, name) + + +def get_tests(test_dir, extensions=[]): + """Returns the list of test files in a given directory. 'extensions' is a + list of file extensions. If 'extensions' is empty, returns all files. + """ + tests = [] + if not extensions: + tests += glob.glob(os.path.join(test_dir, '*')) + for ext in extensions: + tests += glob.glob(os.path.join(test_dir, '*' + ext)) + return sorted(tests) + if not options.interpreter: warn('no interpreter provided (did not test spec interpreter validation)') @@ -457,10 +469,6 @@ def minify_check(wast, verify_final_result=True): os.unlink('b.wast') -def files_with_pattern(*path_pattern): - return sorted(glob.glob(os.path.join(*path_pattern))) - - # run a check with BINARYEN_PASS_DEBUG set, to do full validation def with_pass_debug(check): old_pass_debug = os.environ.get('BINARYEN_PASS_DEBUG') diff --git a/scripts/test/wasm2js.py b/scripts/test/wasm2js.py index 5e30ae0d7..f5bbb4199 100755 --- a/scripts/test/wasm2js.py +++ b/scripts/test/wasm2js.py @@ -19,17 +19,13 @@ import os from .support import run_command, split_wast, write_wast from .shared import ( WASM2JS, MOZJS, NODEJS, fail_if_not_identical, options, - fail_if_not_identical_to_file, with_pass_debug + fail_if_not_identical_to_file, with_pass_debug, get_test_dir, get_tests, ) -tests = sorted(os.listdir(os.path.join(options.binaryen_test))) -spec_dir = os.path.join(options.binaryen_test, 'spec') -spec_tests = [os.path.join(spec_dir, t) - for t in sorted(os.listdir(spec_dir)) - if '.fail' not in t] -wasm2js_dir = os.path.join(options.binaryen_test, 'wasm2js') -extra_wasm2js_tests = [os.path.join(wasm2js_dir, t) for t in - sorted(os.listdir(wasm2js_dir))] +tests = get_tests(options.binaryen_test) +spec_tests = get_tests(get_test_dir('spec'), ['.wast']) +spec_tests = [t for t in spec_tests if '.fail' not in t] +wasm2js_tests = get_tests(get_test_dir('wasm2js'), ['.wast']) assert_tests = ['wasm2js.wast.asserts'] # These tests exercise functionality not supported by wasm2js wasm2js_blacklist = ['empty_imported_table.wast'] @@ -37,24 +33,20 @@ wasm2js_blacklist = ['empty_imported_table.wast'] def test_wasm2js_output(): for opt in (0, 1): - for wasm in tests + spec_tests + extra_wasm2js_tests: - if not wasm.endswith('.wast'): - continue - basename = os.path.basename(wasm) + for t in tests + spec_tests + wasm2js_tests: + basename = os.path.basename(t) if basename in wasm2js_blacklist: continue asm = basename.replace('.wast', '.2asm.js') - expected_file = os.path.join(wasm2js_dir, asm) + expected_file = os.path.join(get_test_dir('wasm2js'), asm) if opt: expected_file += '.opt' if not os.path.exists(expected_file): continue - print('..', wasm) - - t = os.path.join(options.binaryen_test, wasm) + print('..', os.path.basename(t)) all_out = [] @@ -64,7 +56,7 @@ def test_wasm2js_output(): cmd = WASM2JS + ['split.wast', '-all'] if opt: cmd += ['-O'] - if 'emscripten' in wasm: + if 'emscripten' in t: cmd += ['--emscripten'] out = run_command(cmd) all_out.append(out) @@ -109,7 +101,7 @@ def test_asserts_output(): asserts_expected_file = os.path.join(options.binaryen_test, asserts) traps_expected_file = os.path.join(options.binaryen_test, traps) - wasm = os.path.join(wasm2js_dir, wasm) + wasm = os.path.join(get_test_dir('wasm2js'), wasm) cmd = WASM2JS + [wasm, '--allow-asserts', '-all'] out = run_command(cmd) fail_if_not_identical_to_file(out, asserts_expected_file) @@ -129,7 +121,7 @@ def update_wasm2js_tests(): print('\n[ checking wasm2js ]\n') for opt in (0, 1): - for wasm in tests + spec_tests + extra_wasm2js_tests: + for wasm in tests + spec_tests + wasm2js_tests: if not wasm.endswith('.wast'): continue @@ -137,7 +129,7 @@ def update_wasm2js_tests(): continue asm = os.path.basename(wasm).replace('.wast', '.2asm.js') - expected_file = os.path.join(wasm2js_dir, asm) + expected_file = os.path.join(get_test_dir('wasm2js', asm)) if opt: expected_file += '.opt' @@ -145,7 +137,7 @@ def update_wasm2js_tests(): # exists - only some work so far. the tests in extra are in # the test/wasm2js dir and so are specific to wasm2js, and # we run all of those. - if wasm not in extra_wasm2js_tests and not os.path.exists(expected_file): + if wasm not in wasm2js_tests and not os.path.exists(expected_file): continue print('..', wasm) @@ -176,7 +168,7 @@ def update_wasm2js_tests(): asserts_expected_file = os.path.join(options.binaryen_test, asserts) traps_expected_file = os.path.join(options.binaryen_test, traps) - cmd = WASM2JS + [os.path.join(wasm2js_dir, wasm), '--allow-asserts', '-all'] + cmd = WASM2JS + [os.path.join(get_test_dir('wasm2js'), wasm), '--allow-asserts', '-all'] out = run_command(cmd) with open(asserts_expected_file, 'w') as o: o.write(out) |