eclass/python-utils-r1: Sync with Gentoo

It's from Gentoo commit cc63aaba87ed789e8155d0eb1f3f9f2b5a77b67f.

Signed-off-by: Mathieu Tortuyaux <mtortuyaux@microsoft.com>
This commit is contained in:
Flatcar Buildbot 2025-07-21 07:14:56 +00:00 committed by Mathieu Tortuyaux
parent 0d3400b200
commit 9c671a3447
No known key found for this signature in database
GPG Key ID: AC5CCFB52545D9B8

View File

@ -1312,6 +1312,15 @@ _python_check_occluded_packages() {
# The recommended way to disable it in EAPI 8 or earlier is to set
# EPYTEST_PLUGINS (possibly to an empty array).
# @ECLASS_VARIABLE: EPYTEST_PLUGIN_LOAD_VIA_ENV
# @DEFAULT_UNSET
# @DESCRIPTION:
# If set to a non-empty value, plugins will be loaded via PYTEST_PLUGINS
# environment variable rather than explicit "-p" options. This ensures
# that plugins are passed down to subprocess, which may be necessary
# when testing pytest plugins. However, this is also more likely
# to cause duplicate plugin errors.
# @FUNCTION: _set_epytest_plugins
# @INTERNAL
# @DESCRIPTION:
@ -1442,21 +1451,42 @@ epytest() {
if [[ ${PYTEST_DISABLE_PLUGIN_AUTOLOAD} ]]; then
if [[ ${EPYTEST_PLUGINS[@]} ]]; then
local plugin_args=()
readarray -t -d '' plugin_args < <(
if [[ ${EPYTEST_PLUGIN_LOAD_VIA_ENV} ]]; then
local -x PYTEST_PLUGINS=$(
"${EPYTHON}" - "${EPYTEST_PLUGINS[@]}" <<-EOF || die
import sys
from importlib.metadata import distribution, entry_points
packages = {distribution(x).name for x in sys.argv[1:]}
# In packages defining multiple entry points, we must
# list them in the same order!
plugins = (
x.value for x in entry_points(group="pytest11")
if x.dist.name in packages
)
sys.stdout.write(",".join(plugins))
EOF
)
else
local plugin_args=()
readarray -t -d '' plugin_args < <(
"${EPYTHON}" - "${EPYTEST_PLUGINS[@]}" <<-EOF || die
import os
import sys
from importlib.metadata import distribution, entry_points
env_plugins = os.environ.get("PYTEST_PLUGINS", "").split(",")
packages = {distribution(x).name for x in sys.argv[1:]}
eps = {
f"-p{x.name}" for x in entry_points(group="pytest11")
if x.dist.name in packages
if x.dist.name in packages and x.value not in env_plugins
}
sys.stdout.write("\\0".join(sorted(eps)))
EOF
)
args+=( "${plugin_args[@]}" )
fi
fi
else
args+=(
# disable the undesirable-dependency plugins by default to
@ -1518,6 +1548,17 @@ epytest() {
fi
fi
# If we are using hypothesis (require use via EPYTEST_PLUGINS, since
# ebuilds may disable autoloading manually) *and* hypothesis-gentoo
# is available, use it to disable all health checks, to prevent the tests
# from failing randomly under load.
if has hypothesis "${EPYTEST_PLUGINS[@]}" &&
"${EPYTHON}" -c 'import hypothesis_gentoo' 2>/dev/null &&
[[ ! ${HYPOTHESIS_NO_PLUGINS} ]]
then
args+=( --hypothesis-profile=gentoo )
fi
local x
for x in "${EPYTEST_DESELECT[@]}"; do
args+=( --deselect "${x}" )
@ -1527,6 +1568,9 @@ epytest() {
done
set -- "${EPYTHON}" -m pytest "${args[@]}" "${@}" ${EPYTEST_FLAGS}
if [[ ${PYTEST_PLUGINS} ]]; then
einfo "PYTEST_PLUGINS=${PYTEST_PLUGINS}"
fi
echo "${@}" >&2
"${@}"
local ret=${?}