-
Notifications
You must be signed in to change notification settings - Fork 82
Test cases for different use cases of generated files #1193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
henryiii
merged 20 commits into
scikit-build:main
from
eirrgang:eirrgang-editable-test-cases
Feb 2, 2026
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
02e5c98
Establish a new test structure for different resource layouts.
eirrgang aba425e
Check that a compiled binary can be used via ctypes
eirrgang f38dfaa
Check other generated file use cases.
eirrgang b9ca129
style: pre-commit fixes
pre-commit-ci[bot] 5892807
Apply suggestions from code review
eirrgang a6516ee
Use updated fixtures
eirrgang ef87072
style: pre-commit fixes
pre-commit-ci[bot] 51ffe09
Need to install the test package in the isolated venv
eirrgang 385bc42
Address ruff check ARG001
eirrgang a4c3564
`files()` requires an argument before Python 3.12.
eirrgang ab67de0
style: pre-commit fixes
pre-commit-ci[bot] f1f4975
Properly mark the test cases that are expected to fail.
eirrgang 21e91f7
style: pre-commit fixes
pre-commit-ci[bot] 5c8b5ba
Explicitly export `func()` in case it is necessary.
eirrgang bc744d7
Address some Python 3.9 quirks.
eirrgang 2f26f10
style: pre-commit fixes
pre-commit-ci[bot] a28d04a
Support CMake<3.23
eirrgang 3ba1159
Fix syntax error.
eirrgang a429a72
Resolve a review thread.
eirrgang b881e46
Provide more information on failed import.
eirrgang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| cmake_minimum_required(VERSION 3.15) | ||
|
|
||
| project( | ||
| ${SKBUILD_PROJECT_NAME} | ||
| LANGUAGES CXX | ||
| VERSION 1.2.3) | ||
|
|
||
| # Generate files at config time (configure_file) and at build time | ||
| # (add_custom_command) Note that bundling a generated file with sdist is out of | ||
| # scope for now. Note: cmake_generated/nested1/generated.py should try to open | ||
| # both generated and static files. | ||
| configure_file(src/cmake_generated/nested1/generated.py.in generated.py) | ||
| # We always expect the install phase to run, so the build tree layout can be | ||
| # different than the package layout. | ||
| install(FILES ${CMAKE_CURRENT_BINARY_DIR}/generated.py | ||
| DESTINATION ${SKBUILD_PROJECT_NAME}/nested1) | ||
|
|
||
| file( | ||
| GENERATE | ||
| OUTPUT configured_file | ||
| CONTENT "value written by cmake file generation") | ||
| install(FILES ${CMAKE_CURRENT_BINARY_DIR}/configured_file | ||
| DESTINATION ${SKBUILD_PROJECT_NAME}) | ||
|
|
||
| set(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/generated_data") | ||
| set(FILE_CONTENT "value written by cmake custom_command") | ||
| set(GENERATE_SCRIPT "file(WRITE \"${OUTPUT_FILE}\" \"${FILE_CONTENT}\")") | ||
| add_custom_command( | ||
| OUTPUT "${OUTPUT_FILE}" | ||
| COMMAND "${CMAKE_COMMAND}" -P | ||
| "${CMAKE_CURRENT_BINARY_DIR}/generate_file.cmake" | ||
| DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/generate_file.cmake" | ||
| COMMENT "Generating ${OUTPUT_FILE} using CMake scripting at build time" | ||
| VERBATIM) | ||
| file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/generate_file.cmake" | ||
| "${GENERATE_SCRIPT}") | ||
| add_custom_target(generate_file ALL DEPENDS "${OUTPUT_FILE}") | ||
| install(FILES "${OUTPUT_FILE}" DESTINATION ${SKBUILD_PROJECT_NAME}/namespace1) | ||
|
|
||
| add_library(pkg MODULE src/cmake_generated/pkg.cpp) | ||
| include(GenerateExportHeader) | ||
| generate_export_header(pkg) | ||
| target_include_directories(pkg PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
|
||
| if(NOT WIN32) | ||
| # Explicitly set the bundle extension to .so | ||
| set_target_properties(pkg PROPERTIES SUFFIX ".so") | ||
| endif() | ||
|
|
||
| # Set the library name to "pkg", regardless of the OS convention. | ||
| set_target_properties(pkg PROPERTIES PREFIX "") | ||
| install(TARGETS pkg DESTINATION ${SKBUILD_PROJECT_NAME}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| [build-system] | ||
| requires = ["scikit-build-core"] | ||
| build-backend = "scikit_build_core.build" | ||
|
|
||
| [project] | ||
| name = "cmake_generated" | ||
| dynamic = ["version"] | ||
|
|
||
| [tool.scikit-build] | ||
| # Bundling a generated file in the sdist is not supported at this time. | ||
| # sdist.cmake = false | ||
| wheel.license-files = [] | ||
| wheel.exclude = ["**.cpp", "**.in"] | ||
|
|
||
| [tool.scikit-build.metadata.version] | ||
| provider = "scikit_build_core.metadata.regex" | ||
| input = "CMakeLists.txt" | ||
| regex = 'project\([^)]+ VERSION (?P<value>[0-9.]+)' | ||
|
|
||
| [[tool.scikit-build.generate]] | ||
| path = "cmake_generated/_version.py" | ||
| template = ''' | ||
| __version__ = "${version}" | ||
| ''' |
75 changes: 75 additions & 0 deletions
75
tests/packages/cmake_generated/src/cmake_generated/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Package that includes several non-Python non-module files. | ||
|
|
||
| Support some test cases aimed at testing our ability to find generated files | ||
| and static package data files in editable installations. | ||
|
|
||
| We are exercising the importlib machinery to find files that | ||
| are generated in different phases of the build and in different parts of | ||
| the package layout to check that the redirection works correctly in an | ||
| editable installation. | ||
|
|
||
| The test package includes raw data files and shared object libraries that | ||
| are accessed via `ctypes`. | ||
|
|
||
| We test files (generated and static) | ||
|
|
||
| * at the top level of the package, | ||
| * in subpackages, and | ||
| * in a namespace package. | ||
|
|
||
| We test access | ||
|
|
||
| * from modules at the same level as the files, | ||
| * one level above and below, and | ||
| * from parallel subpackages. | ||
| """ | ||
|
|
||
| import ctypes | ||
| import sys | ||
| from importlib.resources import as_file, files, read_text | ||
|
|
||
| try: | ||
| from ._version import __version__ # type: ignore[import-not-found] | ||
| except ImportError: | ||
| __version__ = None | ||
|
|
||
|
|
||
| def get_static_data(): | ||
| return read_text("cmake_generated", "static_data").rstrip() | ||
|
|
||
|
|
||
| def get_configured_data(): | ||
| return files("cmake_generated").joinpath("configured_file").read_text().rstrip() | ||
|
|
||
|
|
||
| def get_namespace_static_data(): | ||
| if sys.version_info[0:2] == (3, 9): | ||
| return ( | ||
| files("cmake_generated") | ||
| .joinpath("namespace1/static_data") | ||
| .read_text() | ||
| .rstrip() | ||
| ) | ||
| # (except in Python 3.9) read_text is able to handle a namespace subpackage directly, though `files()` is not. | ||
| return read_text("cmake_generated.namespace1", "static_data").rstrip() | ||
|
|
||
|
|
||
| def get_namespace_generated_data(): | ||
| # Note that `files("cmake_generated.namespace1")` doesn't work. | ||
| # Ref https://github.com/python/importlib_resources/issues/262 | ||
| return ( | ||
| files("cmake_generated") | ||
| .joinpath("namespace1/generated_data") | ||
| .read_text() | ||
| .rstrip() | ||
| ) | ||
LecrisUT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def ctypes_function(): | ||
| if sys.platform == "win32": | ||
| lib_suffix = "dll" | ||
| else: | ||
| lib_suffix = "so" | ||
| with as_file(files("cmake_generated").joinpath(f"pkg.{lib_suffix}")) as lib_path: | ||
| lib = ctypes.cdll.LoadLibrary(str(lib_path)) | ||
| return lib.func | ||
1 change: 1 addition & 0 deletions
1
tests/packages/cmake_generated/src/cmake_generated/namespace1/static_data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| static value in namespace package |
5 changes: 5 additions & 0 deletions
5
tests/packages/cmake_generated/src/cmake_generated/nested1/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from importlib.resources import read_text | ||
|
|
||
|
|
||
| def get_static_data(): | ||
| return read_text("cmake_generated.nested1", "static_data").rstrip() |
43 changes: 43 additions & 0 deletions
43
tests/packages/cmake_generated/src/cmake_generated/nested1/generated.py.in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| """Try to open both generated and static files from various parts of the package.""" | ||
| import sys | ||
| from importlib.resources import files, read_text | ||
| from types import ModuleType | ||
|
|
||
| from .. import __version__ | ||
|
|
||
| try: | ||
| from .. import nested2 | ||
| except ImportError as e: | ||
| nested2 = None | ||
| import_error = e.msg | ||
| else: | ||
| import_error = None | ||
|
|
||
| def cmake_generated_static_data(): | ||
| return read_text("cmake_generated", "static_data").rstrip() | ||
|
|
||
| def cmake_generated_nested_static_data(): | ||
| return files("cmake_generated.nested1").joinpath("static_data").read_text().rstrip() | ||
|
|
||
| def cmake_generated_namespace_static_data(): | ||
| # Note that `files("cmake_generated.namespace1")` doesn't work. | ||
| # Ref https://github.com/python/importlib_resources/issues/262 | ||
| return files("cmake_generated").joinpath("namespace1/static_data").read_text().rstrip() | ||
|
|
||
| def get_configured_data(): | ||
| return files("cmake_generated").joinpath("configured_file").read_text().rstrip() | ||
|
|
||
| def cmake_generated_namespace_generated_data(): | ||
| if sys.version_info[0:2] == (3, 9): | ||
| return files("cmake_generated").joinpath("namespace1/generated_data").read_text().rstrip() | ||
| else: | ||
| # (except in Python 3.9) read_text is able to handle a namespace subpackage directly, though `files()` is not. | ||
| return read_text("cmake_generated.namespace1","generated_data").rstrip() | ||
|
|
||
| nested_data = "success" | ||
|
|
||
| def nested2_check(): | ||
| if import_error is not None: | ||
| return import_error | ||
| assert isinstance(nested2, ModuleType) | ||
| return "success" |
1 change: 1 addition & 0 deletions
1
tests/packages/cmake_generated/src/cmake_generated/nested1/static_data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| static value in subpackage 1 |
5 changes: 5 additions & 0 deletions
5
tests/packages/cmake_generated/src/cmake_generated/nested2/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| def nested1_generated_check(): | ||
| # noinspection PyUnresolvedReferences | ||
| from ..nested1.generated import nested_data # type: ignore[import-not-found] | ||
|
|
||
| return nested_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #include "pkg_export.h" | ||
|
|
||
| extern "C" PKG_EXPORT int func() {return 42;} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| static value in top-level package |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.