Skip to content

Commit 47052d4

Browse files
authored
Fix change_dir is not always relative to tox_root (#2761)
Resolves #2619
1 parent a26b975 commit 47052d4

File tree

7 files changed

+45
-18
lines changed

7 files changed

+45
-18
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ repos:
5858
- flake8-spellcheck==0.28
5959
- flake8-unused-arguments==0.0.12
6060
- flake8-noqa==1.3
61-
- pep8-naming==0.13.2
61+
- pep8-naming==0.13.3
6262
- flake8-pyproject==1.2.2
6363
- repo: https://github.com/pre-commit/mirrors-prettier
6464
rev: "v2.7.1"

docs/changelog/2619.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :ref:`change_dir` is relative to current working directory rather than to the :ref:`tox_root` when using the ``-c``
2+
argument to locate the ``tox.ini`` file - by :user:`gaborbernat`.

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build-system]
22
build-backend = "hatchling.build"
3-
requires = ["hatchling>=1.11.1", "hatch-vcs>=0.2.1"]
3+
requires = ["hatchling>=1.11.1", "hatch-vcs>=0.3"]
44

55
[project]
66
name = "tox"
@@ -31,7 +31,7 @@ dependencies = [
3131
'tomli>=2.0.1; python_version < "3.11"',
3232
"virtualenv>=20.17.1",
3333
"filelock>=3.8.2",
34-
'importlib-metadata>=5.1; python_version < "3.8"',
34+
'importlib-metadata>=5.2; python_version < "3.8"',
3535
'typing-extensions>=4.4; python_version < "3.8"',
3636
]
3737
optional-dependencies.docs = [
@@ -51,7 +51,7 @@ optional-dependencies.testing = [
5151
"diff-cover>=7.3",
5252
"distlib>=0.3.6",
5353
"flaky>=3.7",
54-
"hatch-vcs>=0.2.1",
54+
"hatch-vcs>=0.3",
5555
"hatchling>=1.11.1",
5656
"psutil>=5.9.4",
5757
"pytest>=7.2",

src/tox/tox_env/python/virtual_env/package/cmd_builder.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from tox.tox_env.python.virtual_env.api import VirtualEnv
2727
from tox.tox_env.register import ToxEnvRegister
2828
from tox.tox_env.runner import RunToxEnv
29+
from tox.tox_env.util import add_change_dir_conf
2930

3031
from .pyproject import Pep517VirtualEnvPackager
3132
from .util import dependencies_with_extras
@@ -61,12 +62,7 @@ def register_config(self) -> None:
6162
default=[],
6263
desc="the commands to be called for testing",
6364
)
64-
self.conf.add_config(
65-
keys=["change_dir", "changedir"],
66-
of_type=Path,
67-
default=lambda conf, name: cast(Path, conf.core["tox_root"]), # noqa: U100
68-
desc="change to this working directory when executing the test command",
69-
)
65+
add_change_dir_conf(self.conf, self.core)
7066
self.conf.add_config(
7167
keys=["ignore_errors"],
7268
of_type=bool,

src/tox/tox_env/runner.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import re
66
from abc import ABC, abstractmethod
77
from hashlib import sha256
8-
from pathlib import Path
9-
from typing import Any, Iterable, List, cast
8+
from typing import Any, Iterable, List
109

1110
from tox.config.types import Command, EnvList
1211
from tox.journal import EnvJournal
1312

1413
from .api import ToxEnv, ToxEnvCreateArgs
1514
from .package import Package, PackageToxEnv, PathPackage
15+
from .util import add_change_dir_conf
1616

1717

1818
class RunToxEnv(ToxEnv, ABC):
@@ -58,12 +58,7 @@ def ensure_one_line(value: str) -> str:
5858
default=[],
5959
desc="the commands to be called after testing",
6060
)
61-
self.conf.add_config(
62-
keys=["change_dir", "changedir"],
63-
of_type=Path,
64-
default=lambda conf, name: cast(Path, conf.core["tox_root"]), # noqa: U100
65-
desc="change to this working directory when executing the test command",
66-
)
61+
add_change_dir_conf(self.conf, self.core)
6762
self.conf.add_config(
6863
keys=["args_are_paths"],
6964
of_type=bool,

src/tox/tox_env/util.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
from typing import cast
5+
6+
from tox.config.sets import CoreConfigSet, EnvConfigSet
7+
8+
9+
def add_change_dir_conf(config: EnvConfigSet, core: CoreConfigSet) -> None:
10+
def _post_process_change_dir(value: Path) -> Path:
11+
if not value.is_absolute():
12+
value = (core["tox_root"] / value).resolve()
13+
return value
14+
15+
config.add_config(
16+
keys=["change_dir", "changedir"],
17+
of_type=Path,
18+
default=lambda conf, name: cast(Path, conf.core["tox_root"]), # noqa: U100
19+
desc="change to this working directory when executing the test command",
20+
post_process=_post_process_change_dir,
21+
)
22+
23+
24+
__all__ = [
25+
"add_change_dir_conf",
26+
]

tests/tox_env/test_tox_env_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@ def test_change_dir_is_created_if_not_exist(tox_project: ToxProjectCreator) -> N
113113
result_first = prj.run("r")
114114
result_first.assert_success()
115115
assert (prj.path / "a" / "b").exists()
116+
117+
118+
def test_change_dir_is_relative_to_conf(tox_project: ToxProjectCreator) -> None:
119+
prj = tox_project({"tox.ini": "[testenv]\npackage=skip\nchange_dir=a"})
120+
result = prj.run("c", "-e", "py", "-k", "change_dir", "-c", prj.path.name, from_cwd=prj.path.parent)
121+
result.assert_success()
122+
lines = result.out.splitlines()
123+
assert lines[1] == f"change_dir = {prj.path / 'a'}"

0 commit comments

Comments
 (0)