blob: 93566323d753288f4cd178a4a3dc126865a94a51 [file]
load("//python:py_library.bzl", "py_library")
load("//python:pytest_test.bzl", "pytest_test")
load("//tests/support:py_reconfig.bzl", "py_reconfig_binary")
load("//tests/support:support.bzl", "NOT_WINDOWS", "SUPPORTS_BZLMOD")
_PYPI_PYTHON_VERSION = "3.14"
py_library(
name = "lib",
srcs = ["lib.py"],
)
# The program that is built with coverage enabled. It is run by the
# `coverage_collection_test` below, which sets the same environment variables
# that `bazel coverage` would set.
py_reconfig_binary(
name = "instrumented",
srcs = ["instrumented.py"],
collect_code_coverage = True,
main = "instrumented.py",
# The bundled coverage tool is configured only for this version, see the
# `MODULE.bazel` file.
python_version = "3.12.16",
tags = ["manual"],
# The bundled coverage tool is not supported on Windows and it is only
# configured with bzlmod.
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = [":lib"],
)
pytest_test(
name = "coverage_collection_test",
srcs = ["coverage_collection_test.py"],
data = [":instrumented"],
env = {
"INSTRUMENTED": "$(rlocationpath :instrumented)",
},
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = ["//python/runfiles"],
)
# The programs below use `coverage` from the `@dev_pip` hub (see
# `MODULE.bazel`) and a Python version for which the toolchain has no coverage
# tool, so coverage collection only works when `--coverage` is set.
# Uses `@dev_pip//coverage` via the `coverage` flag.
py_reconfig_binary(
name = "instrumented_pypi",
srcs = ["instrumented.py"],
collect_code_coverage = True,
config_settings = {
"//python/config_settings:coverage": "@dev_pip//coverage",
},
main = "instrumented.py",
python_version = _PYPI_PYTHON_VERSION,
tags = ["manual"],
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = [":lib"],
)
pytest_test(
name = "coverage_from_pypi_test",
srcs = ["coverage_collection_test.py"],
data = [":instrumented_pypi"],
env = {
"INSTRUMENTED": "$(rlocationpath :instrumented_pypi)",
},
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = ["//python/runfiles"],
)
# Like `instrumented_pypi`, but `@dev_pip//coverage` is materialized in the
# binary's venv site-packages instead of being added to `sys.path`.
py_reconfig_binary(
name = "instrumented_pypi_venv",
srcs = ["instrumented.py"],
collect_code_coverage = True,
config_settings = {
"//python/config_settings:coverage": "@dev_pip//coverage",
},
main = "instrumented.py",
python_version = _PYPI_PYTHON_VERSION,
tags = ["manual"],
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
venvs_site_packages = "yes",
deps = [":lib"],
)
pytest_test(
name = "coverage_from_pypi_venvs_site_packages_test",
srcs = ["coverage_collection_test.py"],
data = [":instrumented_pypi_venv"],
env = {
"INSTRUMENTED": "$(rlocationpath :instrumented_pypi_venv)",
},
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = ["//python/runfiles"],
)
# `coverage` targets with `.coveragerc` files, used via the `coverage` flag.
py_library(
name = "coverage_base_coveragerc",
data = ["base/.coveragerc"],
tags = ["manual"],
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = ["@dev_pip//coverage"],
)
py_library(
name = "coverage_with_coveragerc",
data = [".coveragerc"],
tags = ["manual"],
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = [":coverage_base_coveragerc"],
)
py_reconfig_binary(
name = "instrumented_pypi_coveragerc",
srcs = ["instrumented.py"],
collect_code_coverage = True,
config_settings = {
"//python/config_settings:coverage": ":coverage_with_coveragerc",
},
main = "instrumented.py",
python_version = _PYPI_PYTHON_VERSION,
tags = ["manual"],
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = [":lib"],
)
pytest_test(
name = "coverage_from_pypi_coveragerc_test",
srcs = ["coverage_collection_test.py"],
data = [":instrumented_pypi_coveragerc"],
env = {
"EXPECT_COVERAGERC": "1",
"INSTRUMENTED": "$(rlocationpath :instrumented_pypi_coveragerc)",
},
target_compatible_with = NOT_WINDOWS + SUPPORTS_BZLMOD,
deps = ["//python/runfiles"],
)