diff options
author | Marc Chevrier <marc.chevrier@gmail.com> | 2019-04-01 11:19:54 +0200 |
---|---|---|
committer | Marc Chevrier <marc.chevrier@gmail.com> | 2019-04-03 09:16:17 +0200 |
commit | 8a2f62cc18ece0ebfed0ff6abf53d419d43d2fa1 (patch) | |
tree | 465c79b4ce0f366fe40a00e1be9e965f3c04af4d /Tests/FindPython | |
parent | 5ad73b608d4fc7cc6b30017159b800802a503b0c (diff) | |
download | cmake-8a2f62cc18ece0ebfed0ff6abf53d419d43d2fa1.tar.gz |
FindPython*: Add capability to control virtual env handling.
Fixes: #19097
Diffstat (limited to 'Tests/FindPython')
-rw-r--r-- | Tests/FindPython/CMakeLists.txt | 11 | ||||
-rw-r--r-- | Tests/FindPython/VirtualEnv/CMakeLists.txt | 42 | ||||
-rw-r--r-- | Tests/FindPython/VirtualEnv/VirtualEnvDefault.cmake | 6 | ||||
-rw-r--r-- | Tests/FindPython/VirtualEnv/VirtualEnvOnly.cmake | 16 | ||||
-rw-r--r-- | Tests/FindPython/VirtualEnv/VirtualEnvStandard.cmake | 7 |
5 files changed, 82 insertions, 0 deletions
diff --git a/Tests/FindPython/CMakeLists.txt b/Tests/FindPython/CMakeLists.txt index 38211a4bb5..d6f50e7b10 100644 --- a/Tests/FindPython/CMakeLists.txt +++ b/Tests/FindPython/CMakeLists.txt @@ -68,6 +68,17 @@ if(CMake_TEST_FindPython) --build-options ${build_options} --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION> ) + + add_test(NAME FindPython.VirtualEnv COMMAND + ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindPython/VirtualEnv" + "${CMake_BINARY_DIR}/Tests/FindPython/VirtualEnv" + ${build_generator_args} + --build-project TestVirtualEnv + --build-options ${build_options} + --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION> + ) endif() if(CMake_TEST_FindPython_NumPy) diff --git a/Tests/FindPython/VirtualEnv/CMakeLists.txt b/Tests/FindPython/VirtualEnv/CMakeLists.txt new file mode 100644 index 0000000000..64ba201861 --- /dev/null +++ b/Tests/FindPython/VirtualEnv/CMakeLists.txt @@ -0,0 +1,42 @@ +cmake_minimum_required(VERSION 3.1) + +project(TestVirtualEnv LANGUAGES NONE) + +include(CTest) + +find_package(Python3 REQUIRED COMPONENTS Interpreter) +if (NOT Python3_FOUND) + message (FATAL_ERROR "Fail to found Python 3") +endif() + +set (Python3_VIRTUAL_ENV "${CMAKE_CURRENT_BINARY_DIR}/py3venv") + +execute_process (COMMAND "${Python3_EXECUTABLE}" -m venv "${Python3_VIRTUAL_ENV}" + RESULT_VARIABLE result + OUTPUT_VARIABLE outputs + ERROR_VARIABLE outputs) +if (result) + message (FATAL_ERROR "Fail to create virtual environment: ${outputs}") +endif() + +add_test(NAME FindPython3.VirtualEnvDefault + COMMAND "${CMAKE_COMMAND}" -E env --unset=PYTHONHOME + "VIRTUAL_ENV=${Python3_VIRTUAL_ENV}" + "${CMAKE_COMMAND}" "-DPYTHON3_VIRTUAL_ENV=${Python3_VIRTUAL_ENV}" + -P "${CMAKE_CURRENT_LIST_DIR}/VirtualEnvDefault.cmake") + +add_test(NAME FindPython3.VirtualEnvOnly + COMMAND "${CMAKE_COMMAND}" -E env --unset=PYTHONHOME + "VIRTUAL_ENV=${Python3_VIRTUAL_ENV}" + "${CMAKE_COMMAND}" "-DPYTHON3_VIRTUAL_ENV=${Python3_VIRTUAL_ENV}" + -P "${CMAKE_CURRENT_LIST_DIR}/VirtualEnvOnly.cmake") +add_test(NAME FindPython3.UnsetVirtualEnvOnly + COMMAND "${CMAKE_COMMAND}" -E env --unset=PYTHONHOME + --unset=VIRTUAL_ENV + "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_LIST_DIR}/VirtualEnvOnly.cmake") + +add_test(NAME FindPython3.VirtualEnvStandard + COMMAND "${CMAKE_COMMAND}" -E env --unset=PYTHONHOME + "VIRTUAL_ENV=${Python3_VIRTUAL_ENV}" + "${CMAKE_COMMAND}" "-DPYTHON3_VIRTUAL_ENV=${Python3_VIRTUAL_ENV}" + -P "${CMAKE_CURRENT_LIST_DIR}/VirtualEnvStandard.cmake") diff --git a/Tests/FindPython/VirtualEnv/VirtualEnvDefault.cmake b/Tests/FindPython/VirtualEnv/VirtualEnvDefault.cmake new file mode 100644 index 0000000000..020ecacd9d --- /dev/null +++ b/Tests/FindPython/VirtualEnv/VirtualEnvDefault.cmake @@ -0,0 +1,6 @@ + +find_package (Python3 REQUIRED) + +if (NOT Python3_EXECUTABLE MATCHES "^${PYTHON3_VIRTUAL_ENV}/.+") + message (FATAL_ERROR "Fail to use virtual environment") +endif() diff --git a/Tests/FindPython/VirtualEnv/VirtualEnvOnly.cmake b/Tests/FindPython/VirtualEnv/VirtualEnvOnly.cmake new file mode 100644 index 0000000000..29a49249cb --- /dev/null +++ b/Tests/FindPython/VirtualEnv/VirtualEnvOnly.cmake @@ -0,0 +1,16 @@ + +# +# Virtual environment is defined for python3 +# Trying to find a python2 using only virtual environment +# It is expecting to fail if a virtual environment is active and to success otherwise. +# +set (Python2_FIND_VIRTUALENV ONLY) +find_package (Python2 QUIET) + +if (PYTHON3_VIRTUAL_ENV AND Python2_FOUND) + message (FATAL_ERROR "Python2 unexpectedly found.") +endif() + +if (NOT PYTHON3_VIRTUAL_ENV AND NOT Python2_FOUND) + message (FATAL_ERROR "Fail to find Python2.") +endif() diff --git a/Tests/FindPython/VirtualEnv/VirtualEnvStandard.cmake b/Tests/FindPython/VirtualEnv/VirtualEnvStandard.cmake new file mode 100644 index 0000000000..89f27d8da2 --- /dev/null +++ b/Tests/FindPython/VirtualEnv/VirtualEnvStandard.cmake @@ -0,0 +1,7 @@ + +set (Python3_FIND_VIRTUALENV STANDARD) +find_package (Python3 REQUIRED) + +if (Python3_EXECUTABLE MATCHES "^${PYTHON3_VIRTUAL_ENV}/.+") + message (FATAL_ERROR "Python3 virtual env unexpectedly found.") +endif() |