From 3766ad9830b5e2bffa370b4bff6bbd4d20b7cc36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 20 Dec 2022 10:50:53 -0800 Subject: [PATCH] Fix change_dir is not always relative to tox_root MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bernát Gábor --- .pre-commit-config.yaml | 2 +- docs/changelog/2619.bugfix.rst | 2 ++ pyproject.toml | 6 ++--- .../python/virtual_env/package/cmd_builder.py | 8 ++---- src/tox/tox_env/runner.py | 11 +++----- src/tox/tox_env/util.py | 26 +++++++++++++++++++ tests/tox_env/test_tox_env_api.py | 8 ++++++ 7 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 docs/changelog/2619.bugfix.rst create mode 100644 src/tox/tox_env/util.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 277015e94..0d694fdd8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -58,7 +58,7 @@ repos: - flake8-spellcheck==0.28 - flake8-unused-arguments==0.0.12 - flake8-noqa==1.3 - - pep8-naming==0.13.2 + - pep8-naming==0.13.3 - flake8-pyproject==1.2.2 - repo: https://github.com/pre-commit/mirrors-prettier rev: "v2.7.1" diff --git a/docs/changelog/2619.bugfix.rst b/docs/changelog/2619.bugfix.rst new file mode 100644 index 000000000..b1719fd4b --- /dev/null +++ b/docs/changelog/2619.bugfix.rst @@ -0,0 +1,2 @@ +Fix :ref:`change_dir` is relative to current working directory rather than to the :ref:`tox_root` when using the ``-c`` +argument to locate the ``tox.ini`` file - by :user:`gaborbernat`. diff --git a/pyproject.toml b/pyproject.toml index 7ffb8baef..1d966aa16 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] build-backend = "hatchling.build" -requires = ["hatchling>=1.11.1", "hatch-vcs>=0.2.1"] +requires = ["hatchling>=1.11.1", "hatch-vcs>=0.3"] [project] name = "tox" @@ -31,7 +31,7 @@ dependencies = [ 'tomli>=2.0.1; python_version < "3.11"', "virtualenv>=20.17.1", "filelock>=3.8.2", - 'importlib-metadata>=5.1; python_version < "3.8"', + 'importlib-metadata>=5.2; python_version < "3.8"', 'typing-extensions>=4.4; python_version < "3.8"', ] optional-dependencies.docs = [ @@ -51,7 +51,7 @@ optional-dependencies.testing = [ "diff-cover>=7.3", "distlib>=0.3.6", "flaky>=3.7", - "hatch-vcs>=0.2.1", + "hatch-vcs>=0.3", "hatchling>=1.11.1", "psutil>=5.9.4", "pytest>=7.2", diff --git a/src/tox/tox_env/python/virtual_env/package/cmd_builder.py b/src/tox/tox_env/python/virtual_env/package/cmd_builder.py index 9140a9361..2d6097a44 100644 --- a/src/tox/tox_env/python/virtual_env/package/cmd_builder.py +++ b/src/tox/tox_env/python/virtual_env/package/cmd_builder.py @@ -26,6 +26,7 @@ from tox.tox_env.python.virtual_env.api import VirtualEnv from tox.tox_env.register import ToxEnvRegister from tox.tox_env.runner import RunToxEnv +from tox.tox_env.util import add_change_dir_conf from .pyproject import Pep517VirtualEnvPackager from .util import dependencies_with_extras @@ -61,12 +62,7 @@ def register_config(self) -> None: default=[], desc="the commands to be called for testing", ) - self.conf.add_config( - keys=["change_dir", "changedir"], - of_type=Path, - default=lambda conf, name: cast(Path, conf.core["tox_root"]), # noqa: U100 - desc="change to this working directory when executing the test command", - ) + add_change_dir_conf(self.conf, self.core) self.conf.add_config( keys=["ignore_errors"], of_type=bool, diff --git a/src/tox/tox_env/runner.py b/src/tox/tox_env/runner.py index c9c3c6ad2..fb9a95e0d 100644 --- a/src/tox/tox_env/runner.py +++ b/src/tox/tox_env/runner.py @@ -5,14 +5,14 @@ import re from abc import ABC, abstractmethod from hashlib import sha256 -from pathlib import Path -from typing import Any, Iterable, List, cast +from typing import Any, Iterable, List from tox.config.types import Command, EnvList from tox.journal import EnvJournal from .api import ToxEnv, ToxEnvCreateArgs from .package import Package, PackageToxEnv, PathPackage +from .util import add_change_dir_conf class RunToxEnv(ToxEnv, ABC): @@ -58,12 +58,7 @@ def ensure_one_line(value: str) -> str: default=[], desc="the commands to be called after testing", ) - self.conf.add_config( - keys=["change_dir", "changedir"], - of_type=Path, - default=lambda conf, name: cast(Path, conf.core["tox_root"]), # noqa: U100 - desc="change to this working directory when executing the test command", - ) + add_change_dir_conf(self.conf, self.core) self.conf.add_config( keys=["args_are_paths"], of_type=bool, diff --git a/src/tox/tox_env/util.py b/src/tox/tox_env/util.py new file mode 100644 index 000000000..aa13f12b9 --- /dev/null +++ b/src/tox/tox_env/util.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +from pathlib import Path +from typing import cast + +from tox.config.sets import CoreConfigSet, EnvConfigSet + + +def add_change_dir_conf(config: EnvConfigSet, core: CoreConfigSet) -> None: + def _post_process_change_dir(value: Path) -> Path: + if not value.is_absolute(): + value = (core["tox_root"] / value).resolve() + return value + + config.add_config( + keys=["change_dir", "changedir"], + of_type=Path, + default=lambda conf, name: cast(Path, conf.core["tox_root"]), # noqa: U100 + desc="change to this working directory when executing the test command", + post_process=_post_process_change_dir, + ) + + +__all__ = [ + "add_change_dir_conf", +] diff --git a/tests/tox_env/test_tox_env_api.py b/tests/tox_env/test_tox_env_api.py index 65623b739..f4f286078 100644 --- a/tests/tox_env/test_tox_env_api.py +++ b/tests/tox_env/test_tox_env_api.py @@ -113,3 +113,11 @@ def test_change_dir_is_created_if_not_exist(tox_project: ToxProjectCreator) -> N result_first = prj.run("r") result_first.assert_success() assert (prj.path / "a" / "b").exists() + + +def test_change_dir_is_relative_to_conf(tox_project: ToxProjectCreator) -> None: + prj = tox_project({"tox.ini": "[testenv]\npackage=skip\nchange_dir=a"}) + result = prj.run("c", "-e", "py", "-k", "change_dir", "-c", prj.path.name, from_cwd=prj.path.parent) + result.assert_success() + lines = result.out.splitlines() + assert lines[1] == f"change_dir = {prj.path / 'a'}"