Skip to content

Commit

Permalink
Ignore missing interpreters when classifying env type
Browse files Browse the repository at this point in the history
We now separate environments into either run or packaging environments
[1]. As noted in 'tox.session.env_select.EnvSelector._defined_envs' [2],
the name of the environment is not enough to determine what type of
environment it is and we must actually build the environment and inspect
it. This allows us to prevent users *running* these packaging
environments (e.g. 'tox -e .pkg'). Part of this process of building an
environment is validating the base python. If this validation fails
(i.e. the Python version does not exist), we will raise
'tox.tox_env.python.api.NoInterpreter'. We were not handling this
exception, and thus the process of determining the types of each
environment would cause a failure if any environment requested a Python
version we did not support, even if we weren't actually trying to run
this environment.

The fix for this is simple: handle the exception and simply ignore these
unsupported environments.

While we're here, fix some issues with an existing test that were
noticed while adding new tests.

[1] https://tox.wiki/en/latest/upgrading.html#packaging-configuration-and-inheritance
[2] https://github.com/tox-dev/tox/blob/af35384bb2ee/src/tox/session/env_select.py#L173

Signed-off-by: Stephen Finucane <stephen@that.guru>
Closes: tox-dev#2811
  • Loading branch information
stephenfin committed Jan 6, 2023
1 parent 4944b77 commit 43c9dd0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/changelog/2811.bugfix.rst
@@ -0,0 +1,2 @@
The combination of ``usedevelop = true`` and ``--skip-missing-interpreters=false`` will no longer fail for environments
that were *not* invoked.
8 changes: 6 additions & 2 deletions src/tox/tox_env/python/package.py
Expand Up @@ -14,7 +14,7 @@
from ..errors import Skip
from ..package import Package, PackageToxEnv, PathPackage
from ..runner import RunToxEnv
from .api import Python
from .api import NoInterpreter, Python
from .pip.req_file import PythonDeps

if TYPE_CHECKING:
Expand Down Expand Up @@ -87,7 +87,11 @@ def default_wheel_tag(conf: Config, env_name: str | None) -> str: # noqa: U100
# python only code are often compatible at major level (unless universal wheel in which case both 2/3)
# c-extension codes are trickier, but as of today both poetry/setuptools uses pypa/wheels logic
# https://github.com/pypa/wheel/blob/master/src/wheel/bdist_wheel.py#L234-L280
run_py = cast(Python, run_env).base_python
try:
run_py = cast(Python, run_env).base_python
except NoInterpreter:
run_py = None

if run_py is None:
base = ",".join(run_env.conf["base_python"])
raise Skip(f"could not resolve base python with {base}")
Expand Down
37 changes: 34 additions & 3 deletions tests/tox_env/python/test_python_runner.py
Expand Up @@ -128,8 +128,39 @@ def test_extras_are_normalized(
("config", "cli", "expected"),
[("false", "true", True), ("true", "false", False), ("false", "config", False), ("true", "config", True)],
)
def test_config_skip_missing_interpreters(tox_project: ToxProjectCreator, config: str, cli: str, expected: str) -> None:
def test_config_skip_missing_interpreters(
tox_project: ToxProjectCreator,
config: str,
cli: str,
expected: bool,
) -> None:
py_ver = ".".join(str(i) for i in sys.version_info[0:2])
project = tox_project({"tox.ini": f"[tox]\nenvlist=py4,py{py_ver}\nskip_missing_interpreters={config}"})
result = project.run("--skip-missing-interpreters", cli)
assert result.code == 0 if expected else 1
result = project.run(f"--skip-missing-interpreters={cli}")
assert result.code == (0 if expected else -1)


@pytest.mark.parametrize(
("skip", "env", "retcode"),
[
("true", f"py{''.join(str(i) for i in sys.version_info[0:2])}", 0),
("false", f"py{''.join(str(i) for i in sys.version_info[0:2])}", 0),
("true", "py31", 0),
("false", "py31", 1),
("true", None, 0),
("false", None, -1),
],
)
def test_skip_missing_interpreters_specified_env(
tox_project: ToxProjectCreator,
skip: str,
env: str | None,
retcode: int,
) -> None:
py_ver = "".join(str(i) for i in sys.version_info[0:2])
project = tox_project({"tox.ini": f"[tox]\nenvlist=py31,py{py_ver}\n[testenv]\nusedevelop=true"})
args = [f"--skip-missing-interpreters={skip}"]
if env:
args += ["-e", env]
result = project.run(*args)
assert result.code == retcode
1 change: 1 addition & 0 deletions whitelist.txt
Expand Up @@ -142,6 +142,7 @@ replacer
repo
reqs
retann
retcode
rfind
rpartition
rreq
Expand Down

0 comments on commit 43c9dd0

Please sign in to comment.