Skip to content

Commit c2b7fd8

Browse files
Copilotgreenc-FNALgithub-actions[bot]Copilot
authored
Fix Python coverage target environment and availability (#318)
* Fix Python coverage to include plugins/python/python directory * Fix duplicate coverage-python target and environment setup * Update documentation to explain environment setup fix * Move coverage-python target outside HAS_CPPYY block * Exclude test files from Python coverage reports --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: greenc-FNAL <2372949+greenc-FNAL@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Chris Green <greenc@fnal.gov> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d703e4c commit c2b7fd8

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

Modules/private/CreateCoverageTargets.cmake

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -552,39 +552,8 @@ function(_create_coverage_targets_impl)
552552
COMMENT "Cleaning coverage data files (C++ and Python)"
553553
)
554554

555-
# Add Python coverage target if pytest-cov is available
556-
if(Python_FOUND)
557-
execute_process(
558-
COMMAND ${Python_EXECUTABLE} -c "import pytest_cov"
559-
RESULT_VARIABLE PYTEST_COV_CHECK
560-
OUTPUT_QUIET
561-
ERROR_QUIET
562-
)
563-
if(PYTEST_COV_CHECK EQUAL 0)
564-
add_custom_target(
565-
coverage-python
566-
COMMAND
567-
${CMAKE_COMMAND} -E echo
568-
"[Coverage] Generating Python coverage report using pytest-cov..."
569-
COMMAND
570-
${CMAKE_COMMAND} -E env PYTHONPATH=${PROJECT_SOURCE_DIR}/test/python
571-
PHLEX_INSTALL=${PROJECT_SOURCE_DIR} ${Python_EXECUTABLE} -m pytest
572-
${PROJECT_SOURCE_DIR}/test/python/test_phlex.py --cov=${PROJECT_SOURCE_DIR}/test/python
573-
--cov-report=term-missing --cov-report=xml:${CMAKE_BINARY_DIR}/coverage-python.xml
574-
--cov-report=html:${CMAKE_BINARY_DIR}/coverage-python-html
575-
--cov-config=${PROJECT_SOURCE_DIR}/test/python/.coveragerc
576-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
577-
COMMENT "Generating Python coverage report"
578-
VERBATIM
579-
)
580-
message(STATUS "Added 'coverage-python' target for Python test coverage (pytest-cov)")
581-
else()
582-
message(
583-
STATUS
584-
"pytest-cov not found; Python coverage target not available. Install with: pip install pytest-cov"
585-
)
586-
endif()
587-
endif()
555+
# Note: The coverage-python target is defined in test/python/CMakeLists.txt
556+
# where it can use the proper test environment setup (PYTHONPATH, etc.)
588557

589558
message(
590559
STATUS

test/python/.coveragerc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
[run]
22
# Include non-test Python modules for coverage measurement
3-
source = .
3+
source =
4+
../../plugins/python/python
45
omit =
56
test_*.py
67
**/test_*.py
8+
unit_test_*.py
9+
**/unit_test_*.py
10+
# Omit all files in test directories
11+
*/test/*
12+
*/tests/*
713

814
[report]
915
# Exclude test files from coverage reports
1016
omit =
1117
test_*.py
1218
**/test_*.py
19+
unit_test_*.py
20+
**/unit_test_*.py
21+
# Omit all files in test directories
22+
*/test/*
23+
*/tests/*

test/python/CMakeLists.txt

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ if(HAS_CPPYY)
7878
-m
7979
pytest
8080
--cov=${CMAKE_CURRENT_SOURCE_DIR}
81+
--cov=${PROJECT_SOURCE_DIR}/plugins/python/python
8182
--cov-report=term-missing
8283
--cov-report=xml:${CMAKE_BINARY_DIR}/coverage-python.xml
8384
--cov-report=html:${CMAKE_BINARY_DIR}/coverage-python-html
@@ -96,15 +97,6 @@ if(HAS_CPPYY)
9697
add_test(NAME py:phlex COMMAND ${PYTEST_COMMAND} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
9798

9899
set_property(TEST py:phlex PROPERTY ENVIRONMENT "PHLEX_INSTALL=${PYTHON_TEST_PHLEX_INSTALL}")
99-
100-
if(HAS_PYTEST_COV)
101-
add_custom_target(
102-
coverage-python
103-
COMMAND ${PYTEST_COMMAND}
104-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
105-
COMMENT "Running Python coverage report"
106-
)
107-
endif()
108100
endif()
109101

110102
set(ACTIVE_PY_CPHLEX_TESTS "")
@@ -261,3 +253,34 @@ add_test(
261253
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/unit_test_variant.py
262254
)
263255
set_tests_properties(py:unit_variant PROPERTIES ENVIRONMENT "${PYTHON_TEST_ENVIRONMENT}")
256+
257+
# Python coverage target
258+
if(HAS_PYTEST_COV AND ENABLE_COVERAGE)
259+
if(HAS_CPPYY)
260+
# When cppyy is available, use the full pytest command
261+
add_custom_target(
262+
coverage-python
263+
COMMAND
264+
${CMAKE_COMMAND} -E env PYTHONPATH=${TEST_PYTHONPATH}
265+
PHLEX_INSTALL=${PYTHON_TEST_PHLEX_INSTALL} ${PYTEST_COMMAND}
266+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
267+
COMMENT "Running Python coverage report"
268+
)
269+
else()
270+
# When cppyy is not available, run a simpler pytest command
271+
# for standalone Python tests (like unit_test_variant.py)
272+
set(PYTHON_TEST_PHLEX_INSTALL_FALLBACK ${CMAKE_SOURCE_DIR})
273+
add_custom_target(
274+
coverage-python
275+
COMMAND
276+
${CMAKE_COMMAND} -E env PYTHONPATH=${TEST_PYTHONPATH}
277+
PHLEX_INSTALL=${PYTHON_TEST_PHLEX_INSTALL_FALLBACK} ${Python_EXECUTABLE} -m pytest
278+
--cov=${CMAKE_CURRENT_SOURCE_DIR} --cov=${PROJECT_SOURCE_DIR}/plugins/python/python
279+
--cov-report=term-missing --cov-report=xml:${CMAKE_BINARY_DIR}/coverage-python.xml
280+
--cov-report=html:${CMAKE_BINARY_DIR}/coverage-python-html
281+
--cov-config=${CMAKE_CURRENT_SOURCE_DIR}/.coveragerc unit_test_variant.py
282+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
283+
COMMENT "Running Python coverage report (standalone tests only)"
284+
)
285+
endif()
286+
endif()

0 commit comments

Comments
 (0)