From 06045d6e95ca146d59f8ea8f9f99e79d9192a117 Mon Sep 17 00:00:00 2001 From: Kai Kratz Date: Thu, 10 Oct 2024 08:52:43 +0200 Subject: [PATCH] Add pytest tests to cmake Tests can be executed similar to test binaries from build folder by calling wrapper shellscript `run_pytest.sh`. This wrapper forwards all arguments to pytest and exports the requried environment variables to locations as found during the cmake configure process. --- CMakeLists.txt | 50 ++++++++++++++++++++++++++++++++++++++++-- cmake/run_pytest.sh.in | 8 +++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100755 cmake/run_pytest.sh.in diff --git a/CMakeLists.txt b/CMakeLists.txt index dcd46af..cec9923 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ if (HAVE_GRIBJUMP_LOCAL_EXTRACT) set(GRIBJUMP_HAVE_FDB 1) ecbuild_find_package( NAME eccodes VERSION 2.32.1 REQUIRED ) - + ### AEC # Override eccodes' aec with our own: we need a newer version. unset(AEC_INCLUDE_DIRS CACHE) @@ -46,6 +46,10 @@ if (HAVE_GRIBJUMP_LOCAL_EXTRACT) endif() +ecbuild_add_option( FEATURE PYTHON_API_TESTS + DEFAULT ON + DESCRIPTION "Will execute python tests against pygribjump / libgribjump") + ######################################################################################################################## # contents @@ -53,7 +57,7 @@ include(cmake/compiler_warnings.cmake) # optionally handle compiler specific war set( gribjump_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_BINARY_DIR}/src ) -include_directories( +include_directories( ${AEC_INCLUDE_DIRS} ${gribjump_INCLUDE_DIRS} ${eckit_INCLUDE_DIRS} @@ -70,6 +74,48 @@ if (HAVE_GRIBJUMP_LOCAL_EXTRACT) add_subdirectory( tests ) endif() +if(HAVE_PYTHON_API_TESTS) + ecbuild_find_package( NAME Python + VERSION 3.10 + COMPONENTS Interpreter + REQUIRED ) + + set(FDB5_DIR "${fdb5_DIR}/../../..") + configure_file( + ${CMAKE_SOURCE_DIR}/cmake/run_pytest.sh.in + ${CMAKE_BINARY_DIR}/run_pytest.sh + @ONLY + ) + + function(find_python_library python_lib) + execute_process( + COMMAND + ${Python_EXECUTABLE} "-c" "import ${python_lib}; print(${python_lib}.__version__)" + RESULT_VARIABLE status + OUTPUT_VARIABLE python_lib_version + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT status) + message("Found Python module ${python_lib} ${python_lib_version}") + else() + message(FATAL_ERROR "Did not find package ${python_lib}") + endif() + endfunction() + find_python_library(cffi) + find_python_library(numpy) + find_python_library(pytest) + find_python_library(setuptools) + find_python_library(findlibs) + + add_test( + NAME run_pytests + COMMAND ${CMAKE_BINARY_DIR}/run_pytest.sh -vv -s + --basetemp=${CMAKE_BINARY_DIR}/pytest-out + ) + +endif() + ######################################################################################################################## # PLUGIN # Write and install the manifest file for the plugin diff --git a/cmake/run_pytest.sh.in b/cmake/run_pytest.sh.in new file mode 100755 index 0000000..57d06b9 --- /dev/null +++ b/cmake/run_pytest.sh.in @@ -0,0 +1,8 @@ +#! /usr/bin/env bash + +export GRIBJUMP_HOME="@CMAKE_BINARY_DIR@" +export FDB5_DIR="@FDB5_DIR@" +export PYTHONPATH="${PYTHONPATH}:@CMAKE_SOURCE_DIR@/pygribjump/src" + +@Python_EXECUTABLE@ -m pytest @CMAKE_SOURCE_DIR@ --basetemp="@CMAKE_BINARY_DIR@/test-results" "$@" +