Skip to content

Commit 77fc701

Browse files
committed
Support specifying tests with extension.
1 parent 7fe46ac commit 77fc701

File tree

2 files changed

+90
-63
lines changed

2 files changed

+90
-63
lines changed

.github/CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ tests with these targets:
159159
* `test_cmake_build`: Install / subdirectory tests
160160

161161
If you want to build just a subset of tests, use
162-
`-DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp"`. If this is
163-
empty, all tests will be built.
162+
`-DPYBIND11_TEST_OVERRIDE="test_callbacks;test_pickling"`. If this is
163+
empty, all tests will be built. Tests are specified without an extension if they need both a .py and
164+
.cpp file.
164165

165166
You may also pass flags to the `pytest` target by editing `tests/pytest.ini` or
166167
by using the `PYTEST_ADDOPTS` environment variable

tests/CMakeLists.txt

Lines changed: 87 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ endif()
1919
# Only needed for CMake < 3.5 support
2020
include(CMakeParseArguments)
2121

22-
# Filter out items; print an optional message if any items filtered
22+
# Filter out items; print an optional message if any items filtered. This ignores extensions.
2323
#
2424
# Usage:
2525
# pybind11_filter_tests(LISTNAME file1.cpp file2.cpp ... MESSAGE "")
2626
#
2727
macro(pybind11_filter_tests LISTNAME)
2828
cmake_parse_arguments(ARG "" "MESSAGE" "" ${ARGN})
2929
set(PYBIND11_FILTER_TESTS_FOUND OFF)
30+
# Make a list of the test without any extensions, for easier filtering.
31+
set(_TMP_ACTUAL_LIST "${${LISTNAME}};") # enforce ';' at the end to allow matching last item.
32+
string(REGEX REPLACE "\\.[^.]*;" ";" LIST_WITHOUT_EXTENSIONS "${_TMP_ACTUAL_LIST}")
3033
foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS)
31-
list(FIND ${LISTNAME} ${filename} _FILE_FOUND)
34+
string(REGEX REPLACE "\\.[^.]*$" "" filename_no_ext ${filename})
35+
# Search in the list without extensions.
36+
list(FIND LIST_WITHOUT_EXTENSIONS ${filename_no_ext} _FILE_FOUND)
3237
if(_FILE_FOUND GREATER -1)
33-
list(REMOVE_AT ${LISTNAME} ${_FILE_FOUND})
38+
list(REMOVE_AT ${LISTNAME} ${_FILE_FOUND}) # And remove from the list with extensions.
3439
set(PYBIND11_FILTER_TESTS_FOUND ON)
3540
endif()
3641
endforeach()
@@ -104,62 +109,66 @@ if(PYBIND11_CUDA_TESTS)
104109
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
105110
endif()
106111

107-
# Full set of test files (you can override these; see below)
112+
# Full set of test files (you can override these; see below, overrides ignore extension)
113+
# Any test that has no extension is both .py and .cpp, so 'foo' will add 'foo.cpp' and 'foo.py'.
114+
# Any test that has an extension is exclusively that and handled as such.
108115
set(PYBIND11_TEST_FILES
109-
test_async.cpp
110-
test_buffers.cpp
111-
test_builtin_casters.cpp
112-
test_call_policies.cpp
113-
test_callbacks.cpp
114-
test_chrono.cpp
115-
test_class.cpp
116-
test_class_sh_basic.cpp
117-
test_class_sh_disowning.cpp
118-
test_class_sh_disowning_mi.cpp
119-
test_class_sh_factory_constructors.cpp
120-
test_class_sh_inheritance.cpp
121-
test_class_sh_shared_ptr_copy_move.cpp
122-
test_class_sh_trampoline_basic.cpp
123-
test_class_sh_trampoline_self_life_support.cpp
124-
test_class_sh_trampoline_shared_from_this.cpp
125-
test_class_sh_trampoline_shared_ptr_cpp_arg.cpp
126-
test_class_sh_trampoline_unique_ptr.cpp
127-
test_class_sh_unique_ptr_member.cpp
128-
test_class_sh_virtual_py_cpp_mix.cpp
129-
test_classh_mock.cpp
130-
test_const_name.cpp
131-
test_constants_and_functions.cpp
132-
test_copy_move.cpp
133-
test_custom_type_casters.cpp
134-
test_custom_type_setup.cpp
135-
test_docstring_options.cpp
136-
test_eigen.cpp
137-
test_enum.cpp
138-
test_eval.cpp
139-
test_exceptions.cpp
140-
test_factory_constructors.cpp
141-
test_gil_scoped.cpp
142-
test_iostream.cpp
143-
test_kwargs_and_defaults.cpp
144-
test_local_bindings.cpp
145-
test_methods_and_attributes.cpp
146-
test_modules.cpp
147-
test_multiple_inheritance.cpp
148-
test_numpy_array.cpp
149-
test_numpy_dtypes.cpp
150-
test_numpy_vectorize.cpp
151-
test_opaque_types.cpp
152-
test_operator_overloading.cpp
153-
test_pickling.cpp
154-
test_pytypes.cpp
155-
test_sequences_and_iterators.cpp
156-
test_smart_ptr.cpp
157-
test_stl.cpp
158-
test_stl_binders.cpp
159-
test_tagbased_polymorphic.cpp
160-
test_thread.cpp
161-
test_union.cpp
162-
test_virtual_functions.cpp)
116+
test_async
117+
test_buffers
118+
test_builtin_casters
119+
test_call_policies
120+
test_callbacks
121+
test_chrono
122+
test_class
123+
test_class_sh_basic
124+
test_class_sh_disowning
125+
test_class_sh_disowning_mi
126+
test_class_sh_factory_constructors
127+
test_class_sh_inheritance
128+
test_class_sh_shared_ptr_copy_move
129+
test_class_sh_trampoline_basic
130+
test_class_sh_trampoline_self_life_support
131+
test_class_sh_trampoline_shared_from_this
132+
test_class_sh_trampoline_shared_ptr_cpp_arg
133+
test_class_sh_trampoline_unique_ptr
134+
test_class_sh_unique_ptr_member
135+
test_class_sh_virtual_py_cpp_mix
136+
test_classh_mock
137+
test_const_name
138+
test_constants_and_functions
139+
test_copy_move
140+
test_custom_type_casters
141+
test_custom_type_setup
142+
test_docstring_options
143+
test_eigen
144+
test_enum
145+
test_eval
146+
test_exceptions
147+
test_factory_constructors
148+
test_gil_scoped
149+
test_iostream
150+
test_kwargs_and_defaults
151+
test_local_bindings
152+
test_methods_and_attributes
153+
test_modules
154+
test_multiple_inheritance
155+
test_numpy_array
156+
test_numpy_dtypes
157+
test_numpy_vectorize
158+
test_opaque_types
159+
test_operator_overloading
160+
test_pickling
161+
test_pytypes
162+
test_sequences_and_iterators
163+
test_smart_ptr
164+
test_stl
165+
test_stl_binders
166+
test_tagbased_polymorphic
167+
test_thread
168+
test_union
169+
test_virtual_functions
170+
test_class_sh_module_local.py
171+
)
163172

164173
# Invoking cmake with something like:
165174
# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp" ..
@@ -188,8 +197,25 @@ if(PYBIND11_CUDA_TESTS)
188197
"Skipping test_constants_and_functions due to incompatible exception specifications")
189198
endif()
190199

191-
string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
192-
list(APPEND PYBIND11_PYTEST_FILES test_class_sh_module_local.py)
200+
# Now that the test filtering is complete, we need to split the list into the test for PYTEST
201+
# and the list for the cpp targets.
202+
set(PYBIND11_CPPTEST_FILES "")
203+
set(PYBIND11_PYTEST_FILES "")
204+
205+
foreach(test_name ${PYBIND11_TEST_FILES})
206+
if(test_name MATCHES "\\.py$") # Ends in .py, purely python test.
207+
list(APPEND PYBIND11_PYTEST_FILES ${test_name})
208+
elseif(test_name MATCHES "\\.cpp$") # Ends in .cpp, purely cpp test.
209+
list(APPEND PYBIND11_CPPTEST_FILES ${test_name})
210+
elseif(NOT test_name MATCHES "\\.") # No extension specified, assume both, add extension.
211+
list(APPEND PYBIND11_PYTEST_FILES ${test_name}.py)
212+
list(APPEND PYBIND11_CPPTEST_FILES ${test_name}.cpp)
213+
else()
214+
message(WARNING "Unhanded test extension in test: ${test_name}")
215+
endif()
216+
endforeach()
217+
set(PYBIND11_TEST_FILES ${PYBIND11_CPPTEST_FILES})
218+
message("${PYBIND11_TEST_FILES}")
193219
list(SORT PYBIND11_PYTEST_FILES)
194220

195221
# Contains the set of test files that require pybind11_cross_module_tests to be
@@ -384,7 +410,7 @@ set(test_targets pybind11_tests)
384410
# Check if any tests need extra targets by iterating through the mappings registered.
385411
foreach(i ${PYBIND11_TEST_EXTRA_TARGETS})
386412
foreach(needle ${PYBIND11_TEST_EXTRA_TARGETS_NEEDLES_${i}})
387-
if(${needle} IN_LIST PYBIND11_PYTEST_FILES)
413+
if(needle IN_LIST PYBIND11_PYTEST_FILES)
388414
# Add all the additional targets to the test list. List join in newer cmake.
389415
foreach(extra_target ${PYBIND11_TEST_EXTRA_TARGETS_ADDITION_${i}})
390416
list(APPEND test_targets ${extra_target})

0 commit comments

Comments
 (0)