From efd8273203951371b80d9bf532dfe6172852f6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= Date: Tue, 20 Aug 2024 11:29:55 +0200 Subject: [PATCH 1/2] Check autotailor unit test dependencies We will run autotailor unit tests only if pytest is installed on the system. CMake will check for the presence of pytest and enable or disable the unit tests test accordingly. Fixes: #2153 --- CMakeLists.txt | 3 +++ cmake/FindPythonModule.cmake | 41 ++++++++++++++++++++++++++++++++++++ tests/utils/CMakeLists.txt | 10 +++++---- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 cmake/FindPythonModule.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index fcfa3c58d5..39930ddc98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,7 @@ include(CheckIncludeFile) include(CheckIncludeFiles) include(CheckCSourceCompiles) include(CMakeDependentOption) +include(FindPythonModule) # ---------- DEPENDENCIES @@ -127,6 +128,8 @@ find_package(PythonLibs 3) set(PREFERRED_PYTHON_PATH "${PYTHON_EXECUTABLE}" CACHE PATH "Path to preferred Python") set(PYTHON3_PATH "${PYTHON_EXECUTABLE}" CACHE PATH "Path to Python3") +find_python_module(pytest) + find_package(RPM) if(RPM_FOUND) check_library_exists("${RPM_LIBRARY}" rpmReadConfigFiles "" HAVE_RPMREADCONFIGFILES) diff --git a/cmake/FindPythonModule.cmake b/cmake/FindPythonModule.cmake new file mode 100644 index 0000000000..aae9412eba --- /dev/null +++ b/cmake/FindPythonModule.cmake @@ -0,0 +1,41 @@ +# Find if a Python module is installed +# Found at http://www.cmake.org/pipermail/cmake/2011-January/041666.html +# To use do: find_python_module(PyQt4 REQUIRED) + +# Keep filename as is +# lint_cmake: -convention/filename, -package/stdargs + +include(FindPackageHandleStandardArgs) + +function(find_python_module module) + string(TOUPPER ${module} module_upper) + if(NOT PY_${module_upper}) + if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED") + set(PY_${module}_FIND_REQUIRED TRUE) + endif() + if($ENV{SSG_USE_PIP_PACKAGES}) + execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" + "import platform; print(''.join('python'+platform.python_version()[:-2]))" + RESULT_VARIABLE _python_version_status + OUTPUT_VARIABLE _python_version + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT ${_python_version_status}) + set(ENV{PYTHONPATH} "/usr/local/lib/${_python_version}/site-packages:/usr/local/lib64/${_python_version}/site-packages") + endif() + endif() + # A module's location is usually a directory, but for binary modules + # it's a .so file. + execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" + "import re, ${module}; print(re.compile('/__init__.py.*').sub('',${module}.__file__))" + RESULT_VARIABLE _${module}_status + OUTPUT_VARIABLE _${module}_location + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT _${module}_status) + set(PY_${module_upper} ${_${module}_location} CACHE STRING + "Location of Python module ${module}") + endif() + endif() + find_package_handle_standard_args(PY_${module} DEFAULT_MSG PY_${module_upper}) +endfunction() diff --git a/tests/utils/CMakeLists.txt b/tests/utils/CMakeLists.txt index b7eb6aa115..b740ee6f55 100644 --- a/tests/utils/CMakeLists.txt +++ b/tests/utils/CMakeLists.txt @@ -1,7 +1,9 @@ add_oscap_test("autotailor_integration_test.sh") add_oscap_test("test_utils_args.sh") -add_test( - NAME "autotailor-unit-tests" - COMMAND ${PYTHON_EXECUTABLE} -m pytest -v "${CMAKE_CURRENT_SOURCE_DIR}/test_autotailor.py" -) +if(PY_PYTEST) + add_test( + NAME "autotailor-unit-tests" + COMMAND ${PYTHON_EXECUTABLE} -m pytest -v "${CMAKE_CURRENT_SOURCE_DIR}/test_autotailor.py" + ) +endif() From 7576d136ea7c54d06de187f34543b5b336d82ac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cern=C3=BD?= Date: Thu, 29 Aug 2024 09:47:13 +0200 Subject: [PATCH 2/2] Add a status message about pytest to Testing section --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 39930ddc98..0d97186721 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -475,6 +475,7 @@ message(STATUS "Testing:") message(STATUS "tests: ${ENABLE_TESTS}") message(STATUS "valgrind: ${ENABLE_VALGRIND}") message(STATUS "MITRE: ${ENABLE_MITRE}") +message(STATUS "pytest: ${PY_PYTEST}") message(STATUS " ") message(STATUS "Documentation:")