From 4abdff5187669dfcfbda52ae596c4e02144ad23a Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Tue, 16 Oct 2018 15:31:00 +0200 Subject: [PATCH] Add test_pywasmcross.py --- .circleci/config.yml | 6 +++- pyodide_build/pywasmcross.py | 13 +++++--- test/pyodide_build/test_pywasmcross.py | 44 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 test/pyodide_build/test_pywasmcross.py diff --git a/.circleci/config.yml b/.circleci/config.yml index a416318a1..5b203d7d3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -73,10 +73,14 @@ jobs: <<: *defaults steps: - checkout + - run: + name: deps + command: | + sudo pip install pytest-cov - run: name: test command: | - pytest test -v -k 'not (chrome or firefox)' + pytest test -v -k 'not (chrome or firefox)' --cov=pyodide_build --cov=pyodide benchmark: <<: *defaults diff --git a/pyodide_build/pywasmcross.py b/pyodide_build/pywasmcross.py index c7bb21388..49767d118 100755 --- a/pyodide_build/pywasmcross.py +++ b/pyodide_build/pywasmcross.py @@ -105,7 +105,7 @@ def capture_compile(args): sys.exit(result.returncode) -def handle_command(line, args): +def handle_command(line, args, pretend=False): # This is a special case to skip the compilation tests in numpy that aren't # actually part of the build for arg in line: @@ -156,9 +156,10 @@ def handle_command(line, args): print(' '.join(new_args)) - result = subprocess.run(new_args) - if result.returncode != 0: - sys.exit(result.returncode) + if not pretend: + result = subprocess.run(new_args) + if result.returncode != 0: + sys.exit(result.returncode) # Emscripten .so files shouldn't have the native platform slug if shared: @@ -169,7 +170,9 @@ def handle_command(line, args): if renamed.endswith(ext): renamed = renamed[:-len(ext)] + '.so' break - os.rename(output, renamed) + if not pretend: + os.rename(output, renamed) + return new_args def replay_compile(args): diff --git a/test/pyodide_build/test_pywasmcross.py b/test/pyodide_build/test_pywasmcross.py new file mode 100644 index 000000000..d02aeadc0 --- /dev/null +++ b/test/pyodide_build/test_pywasmcross.py @@ -0,0 +1,44 @@ +from collections import namedtuple +from pathlib import Path +import sys + +sys.path.append(str(Path(__file__).parents[2])) + +from pyodide_build.pywasmcross import handle_command # noqa: E402 + + +def _args_wrapper(func): + """Convert function to take as input / return a string instead of a + list of arguments + + Also sets pretend=True + """ + def _inner(line, *pargs): + args = line.split() + res = func(args, *pargs, pretend=True) + if hasattr(res, '__len__'): + return ' '.join(res) + else: + return res + return _inner + + +handle_command_wrap = _args_wrapper(handle_command) +# TODO: add f2c here + + +def test_handle_command(): + Args = namedtuple('args', ['cflags', 'ldflags']) + args = Args(cflags='', ldflags='') + assert handle_command_wrap('gcc -print-multiarch', args) is None + assert handle_command_wrap('gcc test.c', args) == 'emcc test.c' + assert handle_command_wrap('gcc -shared -c test.o -o test.so', args) == \ + 'emcc -shared -c test.bc -o test.wasm' + + # check ldflags injection + args = Args(cflags='', ldflags='-lm') + assert handle_command_wrap('gcc -shared -c test.o -o test.so', args) == \ + 'emcc -lm -shared -c test.bc -o test.wasm' + + # compilation checks in numpy + assert handle_command_wrap('gcc /usr/file.c', args) is None