pytest-cmake 0.8.4__tar.gz → 0.11.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: pytest-cmake
3
- Version: 0.8.4
3
+ Version: 0.11.2
4
4
  Summary: Provide CMake module for Pytest
5
5
  Project-URL: Documentation, https://python-cmake.github.io/pytest-cmake
6
6
  Project-URL: Repository, https://github.com/python-cmake/pytest-cmake
@@ -3,9 +3,9 @@
3
3
  # This module defines the following imported targets:
4
4
  # Pytest::Pytest
5
5
  #
6
- # It also exposes the 'pytest_discover_tests' function which adds ctest
7
- # for each pytest tests. The "BUNDLE_PYTHON_TESTS" environment variable
8
- # can be used to run all discovered tests all together.
6
+ # It also exposes the 'pytest_discover_tests' function, which adds CTest
7
+ # test for each Pytest test. The "BUNDLE_PYTHON_TESTS" environment variable
8
+ # can be used to run all discovered tests together.
9
9
  #
10
10
  # Usage:
11
11
  # find_package(Pytest)
@@ -53,14 +53,15 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
53
53
  PROPERTIES
54
54
  IMPORTED_LOCATION "${PYTEST_EXECUTABLE}")
55
55
 
56
+ # Function to discover pytest tests and add them to CTest.
56
57
  function(pytest_discover_tests NAME)
57
58
  cmake_parse_arguments(
58
- PARSE_ARGV 1 "" ""
59
- "WORKING_DIRECTORY;TRIM_FROM_NAME;BUNDLE_TESTS"
60
- "LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;DEPENDS"
59
+ PARSE_ARGV 1 "" "STRIP_PARAM_BRACKETS;INCLUDE_FILE_PATH;BUNDLE_TESTS"
60
+ "WORKING_DIRECTORY;TRIM_FROM_NAME;TRIM_FROM_FULL_NAME"
61
+ "LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;PROPERTIES;DEPENDS"
61
62
  )
62
63
 
63
- # Identify library path environment name depending on the platform.
64
+ # Set platform-specific library path environment variable.
64
65
  if (CMAKE_SYSTEM_NAME STREQUAL Windows)
65
66
  set(LIBRARY_ENV_NAME PATH)
66
67
  elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
@@ -69,11 +70,11 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
69
70
  set(LIBRARY_ENV_NAME LD_LIBRARY_PATH)
70
71
  endif()
71
72
 
72
- # Sanitize all paths for CMake.
73
+ # Convert paths to CMake-friendly format.
73
74
  cmake_path(CONVERT "$ENV{${LIBRARY_ENV_NAME}}" TO_CMAKE_PATH_LIST LIBRARY_PATH)
74
75
  cmake_path(CONVERT "$ENV{PYTHONPATH}" TO_CMAKE_PATH_LIST PYTHON_PATH)
75
76
 
76
- # Prepend input path to environment variables.
77
+ # Prepend specified paths to the library and Python paths.
77
78
  if (_LIBRARY_PATH_PREPEND)
78
79
  list(REVERSE _LIBRARY_PATH_PREPEND)
79
80
  foreach (_path ${_LIBRARY_PATH_PREPEND})
@@ -88,7 +89,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
88
89
  endforeach()
89
90
  endif()
90
91
 
91
- # Default working directory to current build path if none is provided.
92
+ # Set default working directory if none is specified.
92
93
  if (NOT _WORKING_DIRECTORY)
93
94
  set(_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
94
95
  endif()
@@ -100,20 +101,13 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
100
101
  set(_BUNDLE_TESTS $ENV{BUNDLE_PYTHON_TESTS})
101
102
  endif()
102
103
 
103
- # Serialize environment if necessary.
104
- set(ENCODED_ENVIRONMENT "")
105
- foreach(env ${_ENVIRONMENT})
106
- string(REPLACE [[\]] [\\]] env ${env})
107
- string(REPLACE [[;]] [\\;]] env ${env})
108
- list(APPEND ENCODED_ENVIRONMENT ${env})
109
- endforeach()
110
-
104
+ # Define file paths for generated CMake include files.
111
105
  set(_include_file "${CMAKE_CURRENT_BINARY_DIR}/${NAME}_include.cmake")
112
106
  set(_tests_file "${CMAKE_CURRENT_BINARY_DIR}/${NAME}_tests.cmake")
113
107
 
114
- add_custom_target(
115
- ${NAME} ALL VERBATIM
116
- BYPRODUCTS "${_tests_file}"
108
+ add_custom_command(
109
+ VERBATIM
110
+ OUTPUT "${_tests_file}"
117
111
  DEPENDS ${_DEPENDS}
118
112
  COMMAND ${CMAKE_COMMAND}
119
113
  -D "PYTEST_EXECUTABLE=${PYTEST_EXECUTABLE}"
@@ -123,12 +117,18 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
123
117
  -D "LIBRARY_PATH=${LIBRARY_PATH}"
124
118
  -D "PYTHON_PATH=${PYTHON_PATH}"
125
119
  -D "TRIM_FROM_NAME=${_TRIM_FROM_NAME}"
120
+ -D "TRIM_FROM_FULL_NAME=${_TRIM_FROM_FULL_NAME}"
121
+ -D "STRIP_PARAM_BRACKETS=${_STRIP_PARAM_BRACKETS}"
122
+ -D "INCLUDE_FILE_PATH=${_INCLUDE_FILE_PATH}"
126
123
  -D "WORKING_DIRECTORY=${_WORKING_DIRECTORY}"
127
- -D "ENVIRONMENT=${ENCODED_ENVIRONMENT}"
128
- -D "PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}"
124
+ -D "ENVIRONMENT=${_ENVIRONMENT}"
125
+ -D "TEST_PROPERTIES=${_PROPERTIES}"
129
126
  -D "CTEST_FILE=${_tests_file}"
130
127
  -P "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PytestAddTests.cmake")
131
128
 
129
+ # Create a custom target to run the tests.
130
+ add_custom_target(${NAME} ALL DEPENDS ${_tests_file})
131
+
132
132
  file(WRITE "${_include_file}"
133
133
  "if(EXISTS \"${_tests_file}\")\n"
134
134
  " include(\"${_tests_file}\")\n"
@@ -137,7 +137,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
137
137
  "endif()\n"
138
138
  )
139
139
 
140
- # Add discovered tests to directory TEST_INCLUDE_FILES
140
+ # Register the include file to be processed for tests.
141
141
  set_property(DIRECTORY
142
142
  APPEND PROPERTY TEST_INCLUDE_FILES "${_include_file}")
143
143
 
@@ -0,0 +1,148 @@
1
+ # Wrapper used to create individual CTest tests from Pytest tests.
2
+ cmake_minimum_required(VERSION 3.20...3.30)
3
+
4
+ if(CMAKE_SCRIPT_MODE_FILE)
5
+
6
+ # Initialize content for the CMake test file.
7
+ set(_content "")
8
+
9
+ # Convert library and Python paths to native format.
10
+ cmake_path(CONVERT "${LIBRARY_PATH}" TO_NATIVE_PATH_LIST LIBRARY_PATH)
11
+ cmake_path(CONVERT "${PYTHON_PATH}" TO_NATIVE_PATH_LIST PYTHON_PATH)
12
+
13
+ # Serialize path values separated by semicolons (required on Windows).
14
+ string(REPLACE [[;]] [[\\;]] LIBRARY_PATH "${LIBRARY_PATH}")
15
+ string(REPLACE [[;]] [[\\;]] PYTHON_PATH "${PYTHON_PATH}")
16
+
17
+ # Set up the encoded environment with required paths.
18
+ set(ENCODED_ENVIRONMENT
19
+ "${LIBRARY_ENV_NAME}=${LIBRARY_PATH}"
20
+ "PYTHONPATH=${PYTHON_PATH}"
21
+ )
22
+
23
+ # Serialize additional environment variables if any are provided.
24
+ foreach(env ${ENVIRONMENT})
25
+ string(REPLACE [[;]] [[\\;]] env "${env}")
26
+ list(APPEND ENCODED_ENVIRONMENT "${env}")
27
+ endforeach()
28
+
29
+ # Macro to create individual tests with optional test properties.
30
+ macro(create_test NAME IDENTIFIER)
31
+ string(APPEND _content
32
+ "add_test([==[${NAME}]==] \"${PYTEST_EXECUTABLE}\" [==[${IDENTIFIER}]==])\n"
33
+ )
34
+
35
+ # Prepare the properties for the test, including the environment settings.
36
+ set(args "PROPERTIES ENVIRONMENT [==[${ENCODED_ENVIRONMENT}]==]")
37
+
38
+ # Append any additional properties, escaping complex characters if necessary.
39
+ foreach(property ${TEST_PROPERTIES})
40
+ if(property MATCHES "[^-./:a-zA-Z0-9_]")
41
+ string(APPEND args " [==[${property}]==]")
42
+ else()
43
+ string(APPEND args " ${property}")
44
+ endif()
45
+ endforeach()
46
+
47
+ # Append the test properties to the content.
48
+ string(APPEND _content "set_tests_properties([==[${NAME}]==] ${args})\n")
49
+ endmacro()
50
+
51
+ # If tests are bundled together, create a single test group.
52
+ if (BUNDLE_TESTS)
53
+ create_test("\${TEST_GROUP_NAME}" "\${WORKING_DIRECTORY}")
54
+
55
+ else()
56
+ # Set environment variables for collecting tests.
57
+ set(ENV{${LIBRARY_ENV_NAME}} "${LIBRARY_PATH}")
58
+ set(ENV{PYTHONPATH} "${PYTHON_PATH}")
59
+ set(ENV{PYTHONWARNINGS} "ignore")
60
+
61
+ # Collect tests.
62
+ execute_process(
63
+ COMMAND "${PYTEST_EXECUTABLE}"
64
+ --collect-only -q
65
+ --rootdir=${WORKING_DIRECTORY} .
66
+ OUTPUT_VARIABLE _output_lines
67
+ ERROR_VARIABLE _output_lines
68
+ OUTPUT_STRIP_TRAILING_WHITESPACE
69
+ WORKING_DIRECTORY ${WORKING_DIRECTORY}
70
+ )
71
+
72
+ # Check for errors during test collection.
73
+ string(REGEX MATCH "=+ ERRORS =+(.*)" _error "${_output_lines}")
74
+
75
+ if (_error)
76
+ message(${_error})
77
+ message(FATAL_ERROR "An error occurred during the collection of Python tests.")
78
+ endif()
79
+
80
+ # Convert the collected output into a list of lines.
81
+ string(REPLACE [[;]] [[\;]] _output_lines "${_output_lines}")
82
+ string(REPLACE "\n" ";" _output_lines "${_output_lines}")
83
+
84
+ # Regex pattern to identify pytest test identifiers.
85
+ set(test_pattern "([^:]+)\.py(::([^:]+))?::([^:]+)")
86
+
87
+ # Iterate through each line to identify and process tests.
88
+ foreach(line ${_output_lines})
89
+ string(REGEX MATCHALL ${test_pattern} matching "${line}")
90
+
91
+ # Skip lines that are not identified as tests.
92
+ if (NOT matching)
93
+ continue()
94
+ endif()
95
+
96
+ # Extract file, class, and function names from the test pattern.
97
+ set(_file ${CMAKE_MATCH_1})
98
+ set(_class ${CMAKE_MATCH_3})
99
+ set(_func ${CMAKE_MATCH_4})
100
+
101
+ # Optionally trim parts of the class or function name.
102
+ if (TRIM_FROM_NAME)
103
+ string(REGEX REPLACE "${TRIM_FROM_NAME}" "" _class "${_class}")
104
+ string(REGEX REPLACE "${TRIM_FROM_NAME}" "" _func "${_func}")
105
+ endif()
106
+
107
+ # Form the test name using class and function.
108
+ if (_class)
109
+ set(test_name "${_class}.${_func}")
110
+ else()
111
+ set(test_name "${_func}")
112
+ endif()
113
+
114
+ # Optionally strip parameter brackets from the test name.
115
+ if (STRIP_PARAM_BRACKETS)
116
+ string(REGEX REPLACE "\\[(.+)\\]$" ".\\1" test_name "${test_name}")
117
+ endif()
118
+
119
+ # Optionally include the file path in the test name.
120
+ if (INCLUDE_FILE_PATH)
121
+ cmake_path(CONVERT "${_file}" TO_CMAKE_PATH_LIST _file)
122
+ string(REGEX REPLACE "/" "." _file "${_file}")
123
+ set(test_name "${_file}.${test_name}")
124
+ endif()
125
+
126
+ # Optionally trim parts of the full test name.
127
+ if (TRIM_FROM_FULL_NAME)
128
+ string(REGEX REPLACE "${TRIM_FROM_FULL_NAME}" "" test_name "${test_name}")
129
+ endif()
130
+
131
+ # Prefix the test name with the test group name.
132
+ set(test_name "${TEST_GROUP_NAME}.${test_name}")
133
+ set(test_case "${WORKING_DIRECTORY}/${line}")
134
+
135
+ # Create the test for CTest.
136
+ create_test("\${test_name}" "\${test_case}")
137
+ endforeach()
138
+
139
+ # Warn if no tests were discovered.
140
+ if(NOT _content)
141
+ message(WARNING "No Python tests have been discovered.")
142
+ endif()
143
+ endif()
144
+
145
+ # Write the generated test content to the specified CTest file.
146
+ file(WRITE ${CTEST_FILE} ${_content})
147
+
148
+ endif()
@@ -9,7 +9,7 @@ backend-path = ["."]
9
9
 
10
10
  [project]
11
11
  name = "pytest-cmake"
12
- version = "0.8.4"
12
+ version = "0.11.2"
13
13
  description = "Provide CMake module for Pytest"
14
14
  readme = "README.md"
15
15
  requires-python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, <4"
@@ -60,7 +60,7 @@ class CreateCmakeConfig(install):
60
60
 
61
61
  setup(
62
62
  name="pytest-cmake",
63
- version="0.8.4",
63
+ version="0.11.2",
64
64
  data_files=[
65
65
  (
66
66
  "share/Pytest/cmake",
@@ -1,137 +0,0 @@
1
- # Wrapper used to create individual CTest tests from Pytest tests.
2
- cmake_minimum_required(VERSION 3.20...3.30)
3
-
4
- if(CMAKE_SCRIPT_MODE_FILE)
5
-
6
- # Set Cmake test file to execute each test.
7
- set(_content "")
8
-
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)
20
-
21
- if (BUNDLE_TESTS)
22
- string(APPEND _content
23
- "add_test(\n"
24
- " \"${TEST_GROUP_NAME}\"\n"
25
- " \"${PYTEST_EXECUTABLE}\" \"${WORKING_DIRECTORY}\"\n"
26
- ")\n"
27
- "set_tests_properties(\n"
28
- " \"${TEST_GROUP_NAME}\" PROPERTIES\n"
29
- " ENVIRONMENT \"${LIBRARY_ENV_NAME}=${LIBRARY_PATH}\"\n"
30
- ")\n"
31
- "set_tests_properties(\n"
32
- " \"${TEST_GROUP_NAME}\"\n"
33
- " APPEND PROPERTIES\n"
34
- " ENVIRONMENT \"PYTHONPATH=${PYTHON_PATH}\"\n"
35
- ")\n"
36
- )
37
-
38
- foreach(env ${ENVIRONMENT})
39
- string(APPEND _content
40
- "set_tests_properties(\n"
41
- " \"${TEST_GROUP_NAME}\"\n"
42
- " APPEND PROPERTIES\n"
43
- " ENVIRONMENT ${env}\n"
44
- ")\n"
45
- )
46
- endforeach()
47
-
48
- else()
49
- # Set environment for collecting tests.
50
- set(ENV{${LIBRARY_ENV_NAME}} "${LIBRARY_PATH}")
51
- set(ENV{PYTHONPATH} "${PYTHON_PATH}")
52
- set(ENV{PYTHONWARNINGS} "ignore")
53
-
54
- execute_process(
55
- COMMAND "${PYTEST_EXECUTABLE}"
56
- --collect-only -q
57
- --rootdir=${WORKING_DIRECTORY} .
58
- OUTPUT_VARIABLE _output_lines
59
- ERROR_VARIABLE _output_lines
60
- OUTPUT_STRIP_TRAILING_WHITESPACE
61
- WORKING_DIRECTORY ${WORKING_DIRECTORY}
62
- )
63
-
64
- string(REGEX MATCH "=+ ERRORS =+(.*)" _error ${_output_lines})
65
-
66
- if (_error)
67
- message(${_error})
68
- message(FATAL_ERROR "An error occurred during the collection of Python tests.")
69
- endif()
70
-
71
- # Convert output into list.
72
- string(REPLACE [[;]] [[\;]] _output_lines "${_output_lines}")
73
- string(REPLACE "\n" ";" _output_lines "${_output_lines}")
74
-
75
- set(test_pattern "([^:]+)(::([^:]+))?::([^:]+)")
76
-
77
- foreach (line ${_output_lines})
78
- string(REGEX MATCHALL ${test_pattern} matching "${line}")
79
-
80
- # Ignore lines not identified as a test.
81
- if (NOT matching)
82
- continue()
83
- endif()
84
-
85
- set(_class ${CMAKE_MATCH_3})
86
- set(_func ${CMAKE_MATCH_4})
87
-
88
- if (TRIM_FROM_NAME)
89
- set(pattern "${TRIM_FROM_NAME}")
90
- string(REGEX REPLACE "${pattern}" "" _class "${_class}")
91
- string(REGEX REPLACE "${pattern}" "" _func "${_func}")
92
- endif()
93
-
94
- if (_class)
95
- set(test_name "${_class}.${_func}")
96
- else()
97
- set(test_name "${_func}")
98
- endif()
99
-
100
- set(test_name "${TEST_GROUP_NAME}.${test_name}")
101
- set(test_case "${WORKING_DIRECTORY}/${line}")
102
-
103
- string(APPEND _content
104
- "add_test(\n"
105
- " \"${test_name}\"\n"
106
- " \"${PYTEST_EXECUTABLE}\" \"${test_case}\"\n"
107
- ")\n"
108
- "set_tests_properties(\n"
109
- " \"${test_name}\" PROPERTIES\n"
110
- " ENVIRONMENT \"${LIBRARY_ENV_NAME}=${LIBRARY_PATH}\"\n"
111
- ")\n"
112
- "set_tests_properties(\n"
113
- " \"${test_name}\"\n"
114
- " APPEND PROPERTIES\n"
115
- " ENVIRONMENT \"PYTHONPATH=${PYTHON_PATH}\"\n"
116
- ")\n"
117
- )
118
-
119
- foreach(env ${ENVIRONMENT})
120
- string(APPEND _content
121
- "set_tests_properties(\n"
122
- " \"${test_name}\"\n"
123
- " APPEND PROPERTIES\n"
124
- " ENVIRONMENT ${env}\n"
125
- ")\n"
126
- )
127
- endforeach()
128
-
129
- endforeach()
130
-
131
- if(NOT _content)
132
- message(WARNING "No Python tests have been discovered.")
133
- endif()
134
- endif()
135
-
136
- file(WRITE ${CTEST_FILE} ${_content})
137
- endif()
File without changes
File without changes
File without changes