From 3bc89ee9082a38f0ecaa27792ade33d71863cb52 Mon Sep 17 00:00:00 2001 From: Michael Baentsch Date: Sat, 28 Aug 2021 20:39:25 +0200 Subject: [PATCH] enabling flexible build dir name in testing (#1080) --- tests/CMakeLists.txt | 2 +- tests/helpers.py | 21 ++++++++++++++++----- tests/test_mem.py | 4 ++-- tests/test_namespace.py | 6 +++--- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f7697e8eef..ceed39cb07 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -112,7 +112,7 @@ add_definitions(-DOQS_COMPILE_OPTIONS="[${OQS_COMPILE_OPTIONS}]") # for DLL builds. add_custom_target( run_tests - COMMAND ${PYTHON3_EXEC} -m pytest --verbose --numprocesses=auto + COMMAND ${CMAKE_COMMAND} -E env OQS_BUILD_DIR=${CMAKE_BINARY_DIR} ${PYTHON3_EXEC} -m pytest --verbose --numprocesses=auto WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} DEPENDS oqs example_kem kat_kem test_kem example_sig kat_sig test_sig test_sig_mem test_kem_mem ${UNIX_TESTS} USES_TERMINAL) diff --git a/tests/helpers.py b/tests/helpers.py index 372019fb6d..a867f9c877 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -64,7 +64,7 @@ def is_kem_enabled_by_name(name): symbol = kem_symbol break if symbol == None: return False - header = os.path.join('build', 'include', 'oqs', 'oqsconfig.h') + header = os.path.join(get_current_build_dir_name(), 'include', 'oqs', 'oqsconfig.h') with open(header) as fh: for line in fh: if line.startswith("#define OQS_ENABLE_KEM_"): @@ -97,7 +97,7 @@ def is_sig_enabled_by_name(name): symbol = sig_symbol break if symbol == None: return False - header = os.path.join('build', 'include', 'oqs', 'oqsconfig.h') + header = os.path.join(get_current_build_dir_name(), 'include', 'oqs', 'oqsconfig.h') with open(header) as fh: for line in fh: if line.startswith("#define OQS_ENABLE_SIG_"): @@ -118,12 +118,23 @@ def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper +# So far, build dir name has been hard coded to "build". +# This function makes it dependent on the availability of the environment variable OQS_BUILD_DIR: +# OQS_BUILD_DIR must be below current working dir; if not, behave as before +# If OQS_BUILD_DIR is not set, behave as before, returning hard-coded build name +def get_current_build_dir_name(): + if 'OQS_BUILD_DIR' in os.environ: + # assure this is within current dir: + if os.environ['OQS_BUILD_DIR'].startswith(os.getcwd()): + return os.environ['OQS_BUILD_DIR'][len(os.getcwd())+1:] + return 'build' + def path_to_executable(program_name): path = "." if sys.platform.startswith("win"): if 'APPVEYOR_BUILD_FOLDER' not in os.environ: os.environ['APPVEYOR_BUILD_FOLDER'] = "." path = os.path.join(path, os.environ['APPVEYOR_BUILD_FOLDER']) - path = os.path.join(path, "build", "tests") + path = os.path.join(path, get_current_build_dir_name(), "tests") for executable in [ os.path.join(path, program_name), os.path.join(path, program_name + ".EXE"), @@ -134,7 +145,7 @@ def path_to_executable(program_name): def available_use_options_by_name(): enabled_use_options = [] - with open(os.path.join('build', 'include', 'oqs', 'oqsconfig.h')) as fh: + with open(os.path.join(get_current_build_dir_name(), 'include', 'oqs', 'oqsconfig.h')) as fh: for line in fh: if line.startswith("#define OQS_USE_"): option_name = line.split(' ')[1][len("OQS_USE_"):].strip('\n') @@ -167,7 +178,7 @@ def test_requires_valgrind_version_at_least(x,y,z): @functools.lru_cache() def test_requires_build_options(*options): enabled = {opt : False for opt in options} - with open(os.path.join('build', 'include', 'oqs', 'oqsconfig.h')) as fh: + with open(os.path.join(get_current_build_dir_name(), 'include', 'oqs', 'oqsconfig.h')) as fh: for line in fh: opt = line.split(' ')[1] if line.startswith('#define ') else None if opt in options: diff --git a/tests/test_mem.py b/tests/test_mem.py index cd922135d1..b9530b036f 100644 --- a/tests/test_mem.py +++ b/tests/test_mem.py @@ -10,7 +10,7 @@ def test_mem_kem(kem_name): if not(helpers.is_kem_enabled_by_name(kem_name)): pytest.skip('Not enabled') - Path('build/mem-benchmark').mkdir(parents=True, exist_ok=True) + Path(helpers.get_current_build_dir_name()+'/mem-benchmark').mkdir(parents=True, exist_ok=True) for i in range(3): helpers.run_subprocess([helpers.path_to_executable('test_kem_mem'), kem_name, str(i)]) @@ -21,7 +21,7 @@ def test_mem_sig(sig_name): if not(helpers.is_sig_enabled_by_name(sig_name)): pytest.skip('Not enabled') - Path('build/mem-benchmark').mkdir(parents=True, exist_ok=True) + Path(helpers.get_current_build_dir_name()+'/mem-benchmark').mkdir(parents=True, exist_ok=True) for i in range(3): helpers.run_subprocess([helpers.path_to_executable('test_sig_mem'), sig_name, str(i)]) diff --git a/tests/test_namespace.py b/tests/test_namespace.py index ec25629533..9a3fb9e6f7 100644 --- a/tests/test_namespace.py +++ b/tests/test_namespace.py @@ -11,12 +11,12 @@ @helpers.filtered_test @pytest.mark.skipif(sys.platform.startswith("win"), reason="Not needed on Windows") def test_namespace(): - liboqs = glob.glob('build/lib/liboqs.*')[0] - if liboqs == 'build/lib/liboqs.dylib': + liboqs = glob.glob(helpers.get_current_build_dir_name()+'/lib/liboqs.*')[0] + if liboqs == helpers.get_current_build_dir_name()+'/lib/liboqs.dylib': out = helpers.run_subprocess( ['nm', '-g', liboqs] ) - elif liboqs == 'build/lib/liboqs.so': + elif liboqs == helpers.get_current_build_dir_name()+'/lib/liboqs.so': out = helpers.run_subprocess( ['nm', '-D', liboqs] )