From 13496b9e115964130b55bbc64a98ce27f491e276 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 29 Oct 2025 18:21:53 -0700 Subject: [PATCH] ansible-test - Fix integration test code coverage (#86100) --- .../ansible-test-integration-coverage.yml | 2 ++ .../ns/col/plugins/module_utils/test_util.py | 3 +-- .../tests/integration/targets/hello/aliases | 1 + .../tests/integration/targets/hello/runme.sh | 5 ++++ .../tests/integration/targets/hello/world.py | 3 +++ .../ns/col/tests/unit/test_something.py | 5 ++++ .../targets/ansible-test-coverage/runme.sh | 27 ++++++++++++++++--- .../ansible_test/_internal/coverage_util.py | 6 ++--- 8 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/ansible-test-integration-coverage.yml create mode 100644 test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/aliases create mode 100755 test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/runme.sh create mode 100644 test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/world.py create mode 100644 test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/unit/test_something.py diff --git a/changelogs/fragments/ansible-test-integration-coverage.yml b/changelogs/fragments/ansible-test-integration-coverage.yml new file mode 100644 index 00000000000..a74b70f9263 --- /dev/null +++ b/changelogs/fragments/ansible-test-integration-coverage.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test - Restore code coverage reporting for Python code residing in integration tests. diff --git a/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/plugins/module_utils/test_util.py b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/plugins/module_utils/test_util.py index e8d6b91da59..a904c1a5729 100644 --- a/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/plugins/module_utils/test_util.py +++ b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/plugins/module_utils/test_util.py @@ -1,5 +1,4 @@ from __future__ import annotations -def test_coverage(): - pass +A_CONSTANT = True diff --git a/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/aliases b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/aliases new file mode 100644 index 00000000000..1af1cf90b6a --- /dev/null +++ b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/aliases @@ -0,0 +1 @@ +context/controller diff --git a/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/runme.sh b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/runme.sh new file mode 100755 index 00000000000..6b46f7c1140 --- /dev/null +++ b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/runme.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -eu + +python.py world.py diff --git a/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/world.py b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/world.py new file mode 100644 index 00000000000..e186d432e6b --- /dev/null +++ b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/integration/targets/hello/world.py @@ -0,0 +1,3 @@ +from __future__ import annotations + +print("Hello World!") diff --git a/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/unit/test_something.py b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/unit/test_something.py new file mode 100644 index 00000000000..f1d68c8ad7c --- /dev/null +++ b/test/integration/targets/ansible-test-coverage/ansible_collections/ns/col/tests/unit/test_something.py @@ -0,0 +1,5 @@ +from __future__ import annotations + + +def test_me() -> None: + pass diff --git a/test/integration/targets/ansible-test-coverage/runme.sh b/test/integration/targets/ansible-test-coverage/runme.sh index de5a4eb2cf8..9f1c10e8c44 100755 --- a/test/integration/targets/ansible-test-coverage/runme.sh +++ b/test/integration/targets/ansible-test-coverage/runme.sh @@ -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 diff --git a/test/lib/ansible_test/_internal/coverage_util.py b/test/lib/ansible_test/_internal/coverage_util.py index 6f2bd0a9402..c92aee476dd 100644 --- a/test/lib/ansible_test/_internal/coverage_util.py +++ b/test/lib/ansible_test/_internal/coverage_util.py @@ -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)