Skip to content

Commit dc0efce

Browse files
committed
Improve path handling and serialization.
1 parent ed21822 commit dc0efce

5 files changed

Lines changed: 54 additions & 42 deletions

File tree

.github/workflows/test.yml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: tests
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [ main, dev ]
66

77
pull_request:
88
branches: [ main ]
@@ -16,17 +16,16 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
os: [ "ubuntu", "macos", "windows" ]
20-
cmake: [ "3.20", "3.29" ]
2119
pytest: [ "7", "8" ]
20+
cmake: [ "3.20", "3.29" ]
21+
os: [ "ubuntu", "macos", "windows" ]
2222
python: [ "3.8", "3.11", "3.12" ]
2323
bundled: [ false, true ]
2424

2525
name: |
26-
Pytest v${{ matrix.pytest }}
27-
| CMake v${{ matrix.cmake }}
28-
| ${{ matrix.os }}-py${{ matrix.python }}
29-
| bundled: ${{ matrix.bundled }}
26+
v${{ matrix.pytest }}-${{ matrix.cmake }}
27+
[${{ matrix.os }}-py${{ matrix.python }}]
28+
${{ matrix.bundled && '(bundled)' || '' }}
3029
3130
runs-on: "${{ matrix.os }}-latest"
3231

@@ -64,17 +63,12 @@ jobs:
6463
run: pip install . pytest==${{ matrix.pytest }}.*
6564

6665
- name: Build Example
67-
shell: bash
6866
run: |
6967
cmake --version
70-
cmake \
71-
-DCMAKE_MODULE_PATH:FILEPATH="$(pwd)" \
72-
-S ./example \
73-
-B ./build
68+
cmake -D "CMAKE_MODULE_PATH:FILEPATH=${{ github.workspace }}" -S ./example -B ./build
7469
cmake --build ./build --config Release
7570
7671
- name: Run Tests
77-
shell: bash
7872
working-directory: build
79-
run: ctest -VV
73+
run: ctest -VV -C Release
8074

cmake/FindPytest.cmake

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,41 +60,35 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
6060
"LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;DEPENDS"
6161
)
6262

63-
# Set library path depending on the platform.
63+
# Identify library path environment name depending on the platform.
6464
if (CMAKE_SYSTEM_NAME STREQUAL Windows)
65-
set(LIB_ENV_PATH PATH)
66-
set(_env_sep "\\\;")
65+
set(LIBRARY_ENV_NAME PATH)
6766
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
68-
set(LIB_ENV_PATH DYLD_LIBRARY_PATH)
69-
set(_env_sep ":")
67+
set(LIBRARY_ENV_NAME DYLD_LIBRARY_PATH)
7068
else()
71-
set(LIB_ENV_PATH LD_LIBRARY_PATH)
72-
set(_env_sep ":")
69+
set(LIBRARY_ENV_NAME LD_LIBRARY_PATH)
7370
endif()
7471

75-
# Convert all paths into cmake paths.
76-
cmake_path(CONVERT "$ENV{${LIB_ENV_PATH}}" TO_CMAKE_PATH_LIST libpath)
77-
cmake_path(CONVERT "$ENV{PYTHONPATH}" TO_CMAKE_PATH_LIST pythonpath)
72+
# Sanitize all paths for CMake.
73+
cmake_path(CONVERT "$ENV{${LIBRARY_ENV_NAME}}" TO_CMAKE_PATH_LIST LIBRARY_PATH)
74+
cmake_path(CONVERT "$ENV{PYTHONPATH}" TO_CMAKE_PATH_LIST PYTHON_PATH)
7875

79-
# Prepend input path to environment variables
76+
# Prepend input path to environment variables.
8077
if (_LIBRARY_PATH_PREPEND)
8178
list(REVERSE _LIBRARY_PATH_PREPEND)
8279
foreach (_path ${_LIBRARY_PATH_PREPEND})
83-
set(libpath "${_path}" "${libpath}")
80+
set(LIBRARY_PATH "${_path}" "${LIBRARY_PATH}")
8481
endforeach()
8582
endif()
8683

8784
if (_PYTHON_PATH_PREPEND)
8885
list(REVERSE _PYTHON_PATH_PREPEND)
8986
foreach (_path ${_PYTHON_PATH_PREPEND})
90-
set(pythonpath "${_path}" "${pythonpath}")
87+
set(PYTHON_PATH "${_path}" "${PYTHON_PATH}")
9188
endforeach()
9289
endif()
9390

94-
# Convert list into string.
95-
list(JOIN libpath "${_env_sep}" libpath)
96-
list(JOIN pythonpath "${_env_sep}" pythonpath)
97-
91+
# Default working directory to current build path if none is provided.
9892
if (NOT _WORKING_DIRECTORY)
9993
set(_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
10094
endif()
@@ -116,9 +110,9 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
116110
-D "PYTEST_EXECUTABLE=${PYTEST_EXECUTABLE}"
117111
-D "TEST_GROUP_NAME=${NAME}"
118112
-D "BUNDLE_TESTS=${_BUNDLE_TESTS}"
119-
-D "LIB_ENV_PATH=${LIB_ENV_PATH}"
120-
-D "LIBRARY_PATH=${libpath}"
121-
-D "PYTHON_PATH=${pythonpath}"
113+
-D "LIBRARY_ENV_NAME=${LIBRARY_ENV_NAME}"
114+
-D "LIBRARY_PATH=${LIBRARY_PATH}"
115+
-D "PYTHON_PATH=${PYTHON_PATH}"
122116
-D "TRIM_FROM_NAME=${_TRIM_FROM_NAME}"
123117
-D "WORKING_DIRECTORY=${_WORKING_DIRECTORY}"
124118
-D "ENVIRONMENT=${_ENVIRONMENT}"

cmake/PytestAddTests.cmake

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ if(CMAKE_SCRIPT_MODE_FILE)
66
# Set Cmake test file to execute each test.
77
set(_content "")
88

9-
# Ensure that list environment variables are
10-
# represented as a string on Windows.
11-
string(REPLACE [[;]] [[\;]] LIBRARY_PATH "${LIBRARY_PATH}")
12-
string(REPLACE [[;]] [[\;]] PYTHON_PATH "${PYTHON_PATH}")
9+
cmake_path(CONVERT "${LIBRARY_PATH}" TO_NATIVE_PATH_LIST LIBRARY_PATH)
10+
cmake_path(CONVERT "${PYTHON_PATH}" TO_NATIVE_PATH_LIST PYTHON_PATH)
11+
12+
# Serialize path values separated by semicolons (required on Windows).
13+
macro(encode_value VARIABLE_NAME)
14+
string(REPLACE [[\]] [[\\]] ${VARIABLE_NAME} "${${VARIABLE_NAME}}")
15+
string(REPLACE [[;]] [[\\;]] ${VARIABLE_NAME} "${${VARIABLE_NAME}}")
16+
endmacro()
17+
18+
encode_value(LIBRARY_PATH)
19+
encode_value(PYTHON_PATH)
1320

1421
if (BUNDLE_TESTS)
1522
string(APPEND _content
@@ -19,7 +26,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
1926
")\n"
2027
"set_tests_properties(\n"
2128
" \"${TEST_GROUP_NAME}\" PROPERTIES\n"
22-
" ENVIRONMENT \"${LIB_ENV_PATH}=${LIBRARY_PATH}\"\n"
29+
" ENVIRONMENT \"${LIBRARY_ENV_NAME}=${LIBRARY_PATH}\"\n"
2330
")\n"
2431
"set_tests_properties(\n"
2532
" \"${TEST_GROUP_NAME}\"\n"
@@ -29,6 +36,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
2936
)
3037

3138
foreach(env ${ENVIRONMENT})
39+
encode_value(env)
3240
string(APPEND _content
3341
"set_tests_properties(\n"
3442
" \"${TEST_GROUP_NAME}\"\n"
@@ -40,7 +48,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
4048

4149
else()
4250
# Set environment for collecting tests.
43-
set(ENV{${LIB_ENV_PATH}} "${LIBRARY_PATH}")
51+
set(ENV{${LIBRARY_ENV_NAME}} "${LIBRARY_PATH}")
4452
set(ENV{PYTHONPATH} "${PYTHON_PATH}")
4553
set(ENV{PYTHONWARNINGS} "ignore")
4654

@@ -99,7 +107,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
99107
")\n"
100108
"set_tests_properties(\n"
101109
" \"${test_name}\" PROPERTIES\n"
102-
" ENVIRONMENT \"${LIB_ENV_PATH}=${LIBRARY_PATH}\"\n"
110+
" ENVIRONMENT \"${LIBRARY_ENV_NAME}=${LIBRARY_PATH}\"\n"
103111
")\n"
104112
"set_tests_properties(\n"
105113
" \"${test_name}\"\n"
@@ -109,6 +117,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
109117
)
110118

111119
foreach(env ${ENVIRONMENT})
120+
encode_value(env)
112121
string(APPEND _content
113122
"set_tests_properties(\n"
114123
" \"${test_name}\"\n"

doc/release/release_notes.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@
44
Release Notes
55
*************
66

7+
.. release:: Upcoming
8+
9+
.. change:: changed
10+
11+
Updated CMake script to ensure that environment variables are
12+
preserving the Windows-style path syntax when running the tests.
13+
14+
.. seealso:: https://github.com/buddly27/pytest-cmake/issues/22
15+
16+
.. change:: changed
17+
18+
Improve tests.
19+
720
.. release:: 0.5.2
821
:date: 2024-05-06
922

10-
.. change:: fixed
23+
.. change:: fixed
1124

1225
Updated test collection logic to ensure that the 'rootdir' is a
1326
real path. Previously, running the tests from a symlinked directory

example/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
pytest_discover_tests(
22
PythonTest
33
LIBRARY_PATH_PREPEND
4+
$ENV{RUNNER_TEMP}
45
$<TARGET_FILE_DIR:example>
56
PYTHON_PATH_PREPEND
7+
$ENV{RUNNER_TEMP}
68
$<TARGET_FILE_DIR:example>
79
TRIM_FROM_NAME "^test_"
810
DEPENDS example

0 commit comments

Comments
 (0)