pytest-cmake 0.10.0__tar.gz → 0.11.1__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
1
  Metadata-Version: 2.3
2
2
  Name: pytest-cmake
3
- Version: 0.10.0
3
+ Version: 0.11.1
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
59
  PARSE_ARGV 1 "" "STRIP_PARAM_BRACKETS;INCLUDE_FILE_PATH;BUNDLE_TESTS"
59
60
  "WORKING_DIRECTORY;TRIM_FROM_NAME;TRIM_FROM_FULL_NAME"
60
- "LIBRARY_PATH_PREPEND;PYTHON_PATH_PREPEND;ENVIRONMENT;DEPENDS"
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}"
@@ -127,10 +121,14 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
127
121
  -D "STRIP_PARAM_BRACKETS=${_STRIP_PARAM_BRACKETS}"
128
122
  -D "INCLUDE_FILE_PATH=${_INCLUDE_FILE_PATH}"
129
123
  -D "WORKING_DIRECTORY=${_WORKING_DIRECTORY}"
130
- -D "ENVIRONMENT=${ENCODED_ENVIRONMENT}"
124
+ -D "ENVIRONMENT=${_ENVIRONMENT}"
125
+ -D "TEST_PROPERTIES=${_PROPERTIES}"
131
126
  -D "CTEST_FILE=${_tests_file}"
132
127
  -P "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PytestAddTests.cmake")
133
128
 
129
+ # Create a custom target to run the tests.
130
+ add_custom_target(${NAME} ALL DEPENDS ${_tests_file})
131
+
134
132
  file(WRITE "${_include_file}"
135
133
  "if(EXISTS \"${_tests_file}\")\n"
136
134
  " include(\"${_tests_file}\")\n"
@@ -139,7 +137,7 @@ if (Pytest_FOUND AND NOT TARGET Pytest::Pytest)
139
137
  "endif()\n"
140
138
  )
141
139
 
142
- # Add discovered tests to directory TEST_INCLUDE_FILES
140
+ # Register the include file to be processed for tests.
143
141
  set_property(DIRECTORY
144
142
  APPEND PROPERTY TEST_INCLUDE_FILES "${_include_file}")
145
143
 
@@ -3,54 +3,62 @@ cmake_minimum_required(VERSION 3.20...3.30)
3
3
 
4
4
  if(CMAKE_SCRIPT_MODE_FILE)
5
5
 
6
- # Set Cmake test file to execute each test.
6
+ # Initialize content for the CMake test file.
7
7
  set(_content "")
8
8
 
9
+ # Convert library and Python paths to native format.
9
10
  cmake_path(CONVERT "${LIBRARY_PATH}" TO_NATIVE_PATH_LIST LIBRARY_PATH)
10
11
  cmake_path(CONVERT "${PYTHON_PATH}" TO_NATIVE_PATH_LIST PYTHON_PATH)
11
12
 
12
13
  # 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)
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)
22
31
  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"
32
+ "add_test(\"${NAME}\" \"${PYTEST_EXECUTABLE}\" \"${IDENTIFIER}\")\n"
36
33
  )
37
34
 
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
- )
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()
46
45
  endforeach()
47
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
+
48
55
  else()
49
- # Set environment for collecting tests.
56
+ # Set environment variables for collecting tests.
50
57
  set(ENV{${LIBRARY_ENV_NAME}} "${LIBRARY_PATH}")
51
58
  set(ENV{PYTHONPATH} "${PYTHON_PATH}")
52
59
  set(ENV{PYTHONWARNINGS} "ignore")
53
60
 
61
+ # Collect tests.
54
62
  execute_process(
55
63
  COMMAND "${PYTEST_EXECUTABLE}"
56
64
  --collect-only -q
@@ -61,6 +69,7 @@ if(CMAKE_SCRIPT_MODE_FILE)
61
69
  WORKING_DIRECTORY ${WORKING_DIRECTORY}
62
70
  )
63
71
 
72
+ # Check for errors during test collection.
64
73
  string(REGEX MATCH "=+ ERRORS =+(.*)" _error "${_output_lines}")
65
74
 
66
75
  if (_error)
@@ -68,84 +77,72 @@ if(CMAKE_SCRIPT_MODE_FILE)
68
77
  message(FATAL_ERROR "An error occurred during the collection of Python tests.")
69
78
  endif()
70
79
 
71
- # Convert output into list.
80
+ # Convert the collected output into a list of lines.
72
81
  string(REPLACE [[;]] [[\;]] _output_lines "${_output_lines}")
73
82
  string(REPLACE "\n" ";" _output_lines "${_output_lines}")
74
83
 
84
+ # Regex pattern to identify pytest test identifiers.
75
85
  set(test_pattern "([^:]+)\.py(::([^:]+))?::([^:]+)")
76
86
 
77
- foreach (line ${_output_lines})
87
+ # Iterate through each line to identify and process tests.
88
+ foreach(line ${_output_lines})
78
89
  string(REGEX MATCHALL ${test_pattern} matching "${line}")
79
90
 
80
- # Ignore lines not identified as a test.
91
+ # Skip lines that are not identified as tests.
81
92
  if (NOT matching)
82
93
  continue()
83
94
  endif()
84
95
 
96
+ # Extract file, class, and function names from the test pattern.
85
97
  set(_file ${CMAKE_MATCH_1})
86
98
  set(_class ${CMAKE_MATCH_3})
87
99
  set(_func ${CMAKE_MATCH_4})
88
100
 
101
+ # Optionally trim parts of the class or function name.
89
102
  if (TRIM_FROM_NAME)
90
103
  string(REGEX REPLACE "${TRIM_FROM_NAME}" "" _class "${_class}")
91
104
  string(REGEX REPLACE "${TRIM_FROM_NAME}" "" _func "${_func}")
92
105
  endif()
93
106
 
107
+ # Form the test name using class and function.
94
108
  if (_class)
95
109
  set(test_name "${_class}.${_func}")
96
110
  else()
97
111
  set(test_name "${_func}")
98
112
  endif()
99
113
 
114
+ # Optionally strip parameter brackets from the test name.
100
115
  if (STRIP_PARAM_BRACKETS)
101
116
  string(REGEX REPLACE "\\[(.+)\\]$" ".\\1" test_name "${test_name}")
102
117
  endif()
103
118
 
119
+ # Optionally include the file path in the test name.
104
120
  if (INCLUDE_FILE_PATH)
105
121
  cmake_path(CONVERT "${_file}" TO_CMAKE_PATH_LIST _file)
106
122
  string(REGEX REPLACE "/" "." _file "${_file}")
107
123
  set(test_name "${_file}.${test_name}")
108
124
  endif()
109
125
 
126
+ # Optionally trim parts of the full test name.
110
127
  if (TRIM_FROM_FULL_NAME)
111
128
  string(REGEX REPLACE "${TRIM_FROM_FULL_NAME}" "" test_name "${test_name}")
112
129
  endif()
113
130
 
131
+ # Prefix the test name with the test group name.
114
132
  set(test_name "${TEST_GROUP_NAME}.${test_name}")
115
133
  set(test_case "${WORKING_DIRECTORY}/${line}")
116
134
 
117
- string(APPEND _content
118
- "add_test(\n"
119
- " \"${test_name}\"\n"
120
- " \"${PYTEST_EXECUTABLE}\" \"${test_case}\"\n"
121
- ")\n"
122
- "set_tests_properties(\n"
123
- " \"${test_name}\" PROPERTIES\n"
124
- " ENVIRONMENT \"${LIBRARY_ENV_NAME}=${LIBRARY_PATH}\"\n"
125
- ")\n"
126
- "set_tests_properties(\n"
127
- " \"${test_name}\"\n"
128
- " APPEND PROPERTIES\n"
129
- " ENVIRONMENT \"PYTHONPATH=${PYTHON_PATH}\"\n"
130
- ")\n"
131
- )
132
-
133
- foreach(env ${ENVIRONMENT})
134
- string(APPEND _content
135
- "set_tests_properties(\n"
136
- " \"${test_name}\"\n"
137
- " APPEND PROPERTIES\n"
138
- " ENVIRONMENT ${env}\n"
139
- ")\n"
140
- )
141
- endforeach()
142
-
135
+ # Create the test for CTest.
136
+ create_test("${test_name}" "${test_case}")
143
137
  endforeach()
144
138
 
139
+ # Warn if no tests were discovered.
145
140
  if(NOT _content)
146
141
  message(WARNING "No Python tests have been discovered.")
147
142
  endif()
148
143
  endif()
149
144
 
145
+ # Write the generated test content to the specified CTest file.
150
146
  file(WRITE ${CTEST_FILE} ${_content})
147
+
151
148
  endif()
@@ -9,7 +9,7 @@ backend-path = ["."]
9
9
 
10
10
  [project]
11
11
  name = "pytest-cmake"
12
- version = "0.10.0"
12
+ version = "0.11.1"
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.10.0",
63
+ version="0.11.1",
64
64
  data_files=[
65
65
  (
66
66
  "share/Pytest/cmake",
File without changes
File without changes
File without changes