ansible-test - Fix integration test code coverage (#86100)

This commit is contained in:
Matt Clay
2025-10-29 18:21:53 -07:00
committed by GitHub
parent f2a4d6de12
commit 13496b9e11
8 changed files with 44 additions and 8 deletions

View File

@@ -0,0 +1,2 @@
bugfixes:
- ansible-test - Restore code coverage reporting for Python code residing in integration tests.

View File

@@ -1,5 +1,4 @@
from __future__ import annotations
def test_coverage():
pass
A_CONSTANT = True

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eu
python.py world.py

View File

@@ -0,0 +1,3 @@
from __future__ import annotations
print("Hello World!")

View File

@@ -0,0 +1,5 @@
from __future__ import annotations
def test_me() -> None:
pass

View File

@@ -7,10 +7,31 @@ set -x
# common args for all tests
common=(--venv --color --truncate 0 "${@}")
# run a lightweight test that generates code coverge output
ansible-test sanity --test import "${common[@]}" --coverage
# run a lightweight test that generates code coverage output
ansible-test sanity --test import "${common[@]}" --coverage --python "${ANSIBLE_TEST_PYTHON_VERSION}"
# run an integration test that generates code coverage for a Python file within the integration test
ansible-test integration "${common[@]}" --coverage
# run a unit test that generates code coverage using a venv
ansible-test units "${common[@]}" --coverage --python "${ANSIBLE_TEST_PYTHON_VERSION}" --venv
# report on code coverage in all supported formats
ansible-test coverage report "${common[@]}"
ansible-test coverage report "${common[@]}" | tee coverage.report
ansible-test coverage html "${common[@]}"
ansible-test coverage xml "${common[@]}"
# ensure import test coverage was collected
grep '^plugins/module_utils/test_util.py .* 100%$' coverage.report
# ensure integration test coverage was collected
grep '^tests/integration/targets/hello/world.py .* 100%$' coverage.report
# ensure unit test coverage was collected
grep '^tests/unit/test_something.py .* 100%$' coverage.report
# ensure tests/output/ (from --venv) does not appear in the coverage report (except in the report filename)
if grep "^tests/output/" coverage.report; then
echo "unexpected coverage output: tests/output/"
exit 1
fi

View File

@@ -261,7 +261,7 @@ omit =
*/pyshared/*
*/pytest
*/AnsiballZ_*.py
*/test/results/*
*/test/results/.tmp/delegation/*
"""
coverage_config = coverage_config.lstrip()
@@ -279,8 +279,8 @@ def generate_collection_coverage_config() -> str:
]
omit_patterns = [
# {base}/ansible_collections/{ns}/{col}/tests/output/*
os.path.join(data_context().content.root, data_context().content.results_path, '*'),
# {base}/ansible_collections/{ns}/{col}/tests/output/.tmp/delegation/*
os.path.join(data_context().content.root, data_context().content.results_path, '.tmp/delegation/*'),
]
include = textwrap.indent('\n'.join(include_patterns), ' ' * 4)