-
Notifications
You must be signed in to change notification settings - Fork 14
Fix Python coverage target environment and availability #318
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
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d23108e
Fix Python coverage to include plugins/python/python directory
Copilot b0e47fb
Add comprehensive documentation explaining the Python coverage fix
Copilot 3e131e4
Apply markdownlint fixes
github-actions[bot] de5c790
Apply cmake-format fixes
github-actions[bot] 7b17195
Remove hard-wired path
greenc-FNAL 7d247d6
Remove unwanted whitespace
greenc-FNAL f01476b
Minor whitespace fix
greenc-FNAL 0fb3312
Address unresolved review comments
Copilot 6c6a738
Fix duplicate coverage-python target and environment setup
Copilot edbdc06
Update documentation to explain environment setup fix
Copilot a6d91cd
Apply markdownlint fixes
github-actions[bot] 8292e5e
Move coverage-python target outside HAS_CPPYY block
Copilot a6008c3
Apply cmake-format fixes
github-actions[bot] 95a08d1
Exclude test files from Python coverage reports
Copilot 9dfc9aa
Remove unneeded file per https://github.com/Framework-R-D/phlex/pull/…
greenc-FNAL 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| # Python Coverage Fix: Including plugins/python/python/phlex/__init__.py | ||
|
|
||
| ## Problem Statement | ||
|
|
||
| The file `plugins/python/python/phlex/__init__.py` was not appearing in Python test coverage reports, despite being imported and used by the test suite (specifically by `test/python/unit_test_variant.py`). | ||
|
|
||
| ## Root Cause Analysis | ||
|
|
||
| The Python coverage configuration had multiple issues: | ||
|
|
||
| 1. __Coverage Scope__ - Only monitoring the test directory: | ||
| - The pytest command used `--cov=${CMAKE_CURRENT_SOURCE_DIR}` which only covers the `test/python` directory | ||
| - Missing: Coverage for the `phlex` module located in `plugins/python/python/phlex/` | ||
|
|
||
| 2. __Duplicate Targets__ - Two `coverage-python` targets were defined: | ||
| - One in `test/python/CMakeLists.txt` (correctly configured with test environment) | ||
| - One in `Modules/private/CreateCoverageTargets.cmake` (duplicate, missing proper environment) | ||
| - The duplicate was overriding the correct one | ||
|
|
||
| 3. __Environment Setup__ - Tests require proper PYTHONPATH: | ||
| - Need test directory, plugins directory, Python site-packages | ||
| - The `coverage-python` target wasn't explicitly setting environment variables | ||
| - Tests failed because they couldn't import required modules (cppyy, numpy, etc.) | ||
|
|
||
| 4. __In `test/python/.coveragerc`__: | ||
| - The `source` setting only included `.` (the current test directory) | ||
| - Did not include the path to the plugins directory | ||
|
|
||
| ## The Fix | ||
|
|
||
| ### Changes Made | ||
|
|
||
| 1. __test/python/CMakeLists.txt__: | ||
|
|
||
| Added `--cov` flag for the plugins directory: | ||
|
|
||
| ```cmake | ||
| --cov=${CMAKE_CURRENT_SOURCE_DIR} | ||
| --cov=${PROJECT_SOURCE_DIR}/plugins/python/python # Added this line | ||
| ``` | ||
|
|
||
| Updated `coverage-python` target to use proper test environment: | ||
|
|
||
| ```cmake | ||
| add_custom_target( | ||
| coverage-python | ||
| COMMAND | ||
| ${CMAKE_COMMAND} -E env PYTHONPATH=${TEST_PYTHONPATH} | ||
| PHLEX_INSTALL=${PYTHON_TEST_PHLEX_INSTALL} ${PYTEST_COMMAND} | ||
| WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
| COMMENT "Running Python coverage report" | ||
| ) | ||
| ``` | ||
|
|
||
| 2. __Modules/private/CreateCoverageTargets.cmake__: | ||
|
|
||
| Removed the duplicate `coverage-python` target that was conflicting with the properly configured one in `test/python/CMakeLists.txt`. | ||
|
|
||
| 3. __test/python/.coveragerc__: | ||
|
|
||
| ```ini | ||
| [run] | ||
| source = | ||
| . | ||
| ../../plugins/python/python # Added this line | ||
| ``` | ||
|
|
||
| ## How pytest-cov Works | ||
|
|
||
| pytest-cov uses the `--cov` flag to specify which directories/packages to monitor for coverage: | ||
|
|
||
| - Multiple `--cov` flags can be specified to monitor multiple locations | ||
| - Each `--cov` flag adds a path to the coverage measurement scope | ||
| - The `.coveragerc` file's `source` setting works in conjunction with the `--cov` flags | ||
|
|
||
| ## Environment Setup | ||
|
|
||
| The `TEST_PYTHONPATH` variable in `test/python/CMakeLists.txt` includes: | ||
|
|
||
| - `${CMAKE_CURRENT_SOURCE_DIR}` - the test directory | ||
| - `${PROJECT_SOURCE_DIR}/plugins/python/python` - the phlex package source | ||
| - `${Python_SITELIB}` and `${Python_SITEARCH}` - Python site-packages (for cppyy, numpy, etc.) | ||
| - `$ENV{PYTHONPATH}` - any existing PYTHONPATH from the environment | ||
|
|
||
| This comprehensive PYTHONPATH is required for tests to: | ||
|
|
||
| 1. Import the `phlex` module from `plugins/python/python/phlex/__init__.py` | ||
| 2. Import test dependencies like `cppyy`, `numpy`, `pytest` | ||
| 3. Find any user-installed packages | ||
|
|
||
| The `coverage-python` target explicitly sets these environment variables so that the coverage run has the same environment as the regular test run. | ||
|
|
||
| ## Testing the Fix | ||
|
|
||
| To verify this fix works: | ||
|
|
||
| 1. __Build with coverage enabled__: | ||
|
|
||
| ```bash | ||
| cd /home/runner/work/phlex/phlex | ||
| . scripts/setup-env.sh | ||
| cmake --preset coverage-clang # or coverage-gcc | ||
| ninja -C build | ||
| ``` | ||
|
|
||
| 2. __Run Python tests with coverage__: | ||
|
|
||
| ```bash | ||
| cd build | ||
| cmake --build . --target coverage-python | ||
| ``` | ||
|
|
||
| 3. __Check the coverage report__: | ||
|
|
||
| ```bash | ||
| # View the XML report | ||
| grep "plugins/python/python/phlex/__init__.py" coverage-python.xml | ||
|
|
||
| # Or view the HTML report | ||
| xdg-open coverage-python-html/index.html | ||
| ``` | ||
|
|
||
| 4. __Expected Result__: | ||
| - The file `plugins/python/python/phlex/__init__.py` should now appear in coverage reports | ||
| - Coverage metrics should show which lines of the `Variant` class are executed during tests | ||
|
|
||
| ## Why This Matters | ||
|
|
||
| The `phlex` Python package provides the `Variant` class, which is essential for: | ||
|
|
||
| - Wrapping Python callables with custom type annotations | ||
| - Allowing the same function to be registered multiple times with different types | ||
| - Supporting C++ template-like behavior in Python code | ||
|
|
||
| Without proper coverage tracking, we couldn't verify that: | ||
|
|
||
| - All code paths in the `Variant` class are tested | ||
| - Error handling works correctly | ||
| - Edge cases are covered | ||
|
|
||
| ## Verification in CI | ||
|
|
||
| The GitHub Actions workflow (`.github/workflows/coverage.yaml`) will now: | ||
|
|
||
| 1. Run tests with coverage collection including the plugins directory | ||
| 2. Generate `coverage-python.xml` with the plugins included | ||
| 3. Upload to Codecov with both C++ and Python coverage data | ||
|
|
||
| The `codecov.yml` configuration doesn't explicitly ignore the `plugins` directory, so the data will be properly processed and displayed in coverage reports. | ||
|
|
||
| ## Related Files | ||
|
|
||
| - __Test file using the module__: `test/python/unit_test_variant.py` (imports `from phlex import Variant`) | ||
| - __Source module__: `plugins/python/python/phlex/__init__.py` (contains the `Variant` class) | ||
| - __Coverage workflow__: `.github/workflows/coverage.yaml` (CI coverage execution) | ||
| - __Codecov config__: `codecov.yml` (coverage reporting configuration) |
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 |
|---|---|---|
| @@ -1,12 +1,23 @@ | ||
| [run] | ||
| # Include non-test Python modules for coverage measurement | ||
| source = . | ||
| source = | ||
| ../../plugins/python/python | ||
| omit = | ||
| test_*.py | ||
| **/test_*.py | ||
| unit_test_*.py | ||
| **/unit_test_*.py | ||
| # Omit all files in test directories | ||
| */test/* | ||
| */tests/* | ||
|
|
||
| [report] | ||
| # Exclude test files from coverage reports | ||
| omit = | ||
| test_*.py | ||
| **/test_*.py | ||
| unit_test_*.py | ||
| **/unit_test_*.py | ||
| # Omit all files in test directories | ||
| */test/* | ||
| */tests/* |
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
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.