Skip to content
Permalink
Browse files

revive python tests on windows (followup afcf567)

  • Loading branch information
jef-n committed Jan 5, 2016
1 parent 17ed9d1 commit e961a52c6ac8303ebdbce9d5a847b56e0832b120
Showing with 50 additions and 24 deletions.
  1. +50 −24 cmake/UsePythonTest.cmake
@@ -1,4 +1,10 @@
# Add a python test from a python file
# Add a python test from a python file # One cannot simply do:
# SET(ENV{PYTHONPATH} ${LIBRARY_OUTPUT_PATH})
# SET(my_test "from test_mymodule import *\;test_mymodule()")
# ADD_TEST(PYTHON-TEST-MYMODULE python -c ${my_test})
# Since cmake is only transmitting the ADD_TEST line to ctest thus you are loosing
# the env var. The only way to store the env var is to physically write in the cmake script
# whatever PYTHONPATH you want and then add the test as 'cmake -P python_test.cmake'
#
# Usage:
# ADD_PYTHON_TEST(PYTHON-TEST test.py)
@@ -19,34 +25,54 @@ MARK_AS_ADVANCED(PYTHON_EXECUTABLE)

MACRO(ADD_PYTHON_TEST TESTNAME FILENAME)
GET_SOURCE_FILE_PROPERTY(loc ${FILENAME} LOCATION)
SET(EXTRA_ENVIRONMENT ${ARGN})
GET_SOURCE_FILE_PROPERTY(pyenv ${FILENAME} PYTHONPATH)

IF(CMAKE_CONFIGURATION_TYPES)
ADD_TEST(NAME ${TESTNAME} COMMAND ${PYTHON_EXECUTABLE} ${loc} -D CMAKE_BUILD_TYPE=$<CONFIGURATION>)
ELSE(CMAKE_CONFIGURATION_TYPES)
ADD_TEST(NAME ${TESTNAME} COMMAND ${PYTHON_EXECUTABLE} ${loc})
ENDIF(CMAKE_CONFIGURATION_TYPES)

SET(UNIX_TEST_PYTHONPATH "${QGIS_OUTPUT_DIRECTORY}/python:${QGIS_OUTPUT_DIRECTORY}/python/plugins:${CMAKE_SOURCE_DIR}/tests/src/python:$ENV{PYTHONPATH}")
IF(WIN32)
# Separate Windows PATHs with semicolons
STRING(REGEX REPLACE ":" "\\\;" TEST_PYTHONPATH ${UNIX_TEST_PYTHONPATH})
SET(TEST_QGIS_PREFIX_PATH "${QGIS_OUTPUT_DIRECTORY}/bin/${CMAKE_BUILD_TYPE}")
SET(TEST_PATH "${QGIS_OUTPUT_DIRECTORY}/bin/${CMAKE_BUILD_TYPE};$ENV{PATH}")
SET(ENVIRONMENT_VARS "PYTHONPATH=${TEST_PYTHONPATH}" "QGIS_PREFIX_PATH=${TEST_QGIS_PREFIX_PATH}" "PATH=${TEST_PATH}")
STRING(REGEX REPLACE ":" " " wo_semicolon "${ARGN}")
ELSE(WIN32)
SET(TEST_PYTHONPATH ${UNIX_TEST_PYTHONPATH})
SET(TEST_QGIS_PREFIX_PATH "${QGIS_OUTPUT_DIRECTORY}")
SET(TEST_LD_LIBRARY_PATH "${QGIS_OUTPUT_DIRECTORY}/lib:$ENV{LD_LIBRARY_PATH}")
SET(ENVIRONMENT_VARS "PYTHONPATH=${TEST_PYTHONPATH}" "QGIS_PREFIX_PATH=${TEST_QGIS_PREFIX_PATH}" "LD_LIBRARY_PATH=${TEST_LD_LIBRARY_PATH}")
STRING(REGEX REPLACE ";" " " wo_semicolon "${ARGN}")
ENDIF(WIN32)
SET(TEST_ENVIRONMENT_VARS ${ENVIRONMENT_VARS} ${EXTRA_ENVIRONMENT})

SET_PROPERTY(TEST ${TESTNAME} PROPERTY ENVIRONMENT ${TEST_ENVIRONMENT_VARS})
# For Debug
#FOREACH(arg ${TEST_ENVIRONMENT_VARS})
# MESSAGE("Test ENV: ${arg}")
#ENDFOREACH()
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake "
IF(WIN32)
SET(ENV{QGIS_PREFIX_PATH} \"${QGIS_OUTPUT_DIRECTORY}/bin/\${CMAKE_BUILD_TYPE}\")
SET(ENV{PATH} \"${QGIS_OUTPUT_DIRECTORY}/bin/\${CMAKE_BUILD_TYPE};\$ENV{PATH}\")
SET(ENV{PYTHONPATH} \"${QGIS_OUTPUT_DIRECTORY}/python/;${QGIS_OUTPUT_DIRECTORY}/python/plugins;${CMAKE_SOURCE_DIR}/tests/src/python;\$ENV{PYTHONPATH}\")
MESSAGE(\"PATH:\$ENV{PATH}\")
ELSE(WIN32)
SET(ENV{QGIS_PREFIX_PATH} \"${QGIS_OUTPUT_DIRECTORY}\")
SET(ENV{LD_LIBRARY_PATH} \"${pyenv}:${QGIS_OUTPUT_DIRECTORY}/lib:\$ENV{LD_LIBRARY_PATH}\")
SET(ENV{PYTHONPATH} \"${QGIS_OUTPUT_DIRECTORY}/python/:${QGIS_OUTPUT_DIRECTORY}/python/plugins;${CMAKE_SOURCE_DIR}/tests/src/python:\$ENV{PYTHONPATH}\")
MESSAGE(\"LD_LIBRARY_PATH:\$ENV{LD_LIBRARY_PATH}\")
ENDIF(WIN32)
")

FOREACH(_in ${ARGN})
STRING(REGEX MATCH "^([^=]+)=(.*)$" _out ${_in})
MESSAGE(STATUS "ENV: SET(ENV{${CMAKE_MATCH_1}} \"${CMAKE_MATCH_2}\")")
FILE(APPEND ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake "
SET(ENV{${CMAKE_MATCH_1}} \"${CMAKE_MATCH_2}\")
")
ENDFOREACH(_in)

FILE(APPEND ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake "
MESSAGE(\"PYTHONPATH:\$ENV{PYTHONPATH}\")
MESSAGE(STATUS \"Running ${PYTHON_EXECUTABLE} ${loc} ${wo_semicolon}\")
EXECUTE_PROCESS(
COMMAND ${PYTHON_EXECUTABLE} ${loc} ${wo_semicolon}
RESULT_VARIABLE import_res
)
# Pass the output back to ctest
IF(import_res)
MESSAGE(FATAL_ERROR \"Test failed: \${import_res}\")
ENDIF(import_res)
"
)
IF(CMAKE_CONFIGURATION_TYPES)
ADD_TEST(NAME ${TESTNAME} COMMAND ${CMAKE_COMMAND} -D CMAKE_BUILD_TYPE=$<CONFIGURATION> -P ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake)
ELSE(CMAKE_CONFIGURATION_TYPES)
ADD_TEST(NAME ${TESTNAME} COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake)
ENDIF(CMAKE_CONFIGURATION_TYPES)
ENDMACRO(ADD_PYTHON_TEST)

# Byte compile recursively a directory (DIRNAME)

2 comments on commit e961a52

@nyalldawson

This comment has been minimized.

Copy link
Contributor

@nyalldawson nyalldawson replied Jan 6, 2016

@jef-n Travis linux builds are down at the moment, but this has broken the OSX builds

@michaelkirk

This comment has been minimized.

Copy link
Contributor

@michaelkirk michaelkirk replied Jan 9, 2016

Shoot. I wish I'd had some feedback on #2541 and #2590 when I asked for help from a windows dev to verify my changes. I really do find the previous version easier to understand. Too bad it didn't work =P

Please sign in to comment.
You can’t perform that action at this time.