Skip to content

Commit

Permalink
Fix change_dir is not always relative to tox_root
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Dec 20, 2022
1 parent a26b975 commit 3766ad9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions 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`.
6 changes: 3 additions & 3 deletions 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"
Expand Down Expand Up @@ -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 = [
Expand All @@ -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",
Expand Down
8 changes: 2 additions & 6 deletions src/tox/tox_env/python/virtual_env/package/cmd_builder.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 3 additions & 8 deletions src/tox/tox_env/runner.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions 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",
]
8 changes: 8 additions & 0 deletions tests/tox_env/test_tox_env_api.py
Expand Up @@ -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'}"

0 comments on commit 3766ad9

Please sign in to comment.