diff --git a/docs/changelog/2562.bugfix.rst b/docs/changelog/2562.bugfix.rst new file mode 100644 index 000000000..65afd0ce7 --- /dev/null +++ b/docs/changelog/2562.bugfix.rst @@ -0,0 +1,2 @@ +Ensure paths constructed by tox are stable by resolving relative paths to fully qualified one, this insures that running +tox from a different folder than project root still generates meaningful paths - by :user:`gaborbernat`. diff --git a/src/tox/config/main.py b/src/tox/config/main.py index d122d598f..38efb600b 100644 --- a/src/tox/config/main.py +++ b/src/tox/config/main.py @@ -29,7 +29,6 @@ def __init__( pos_args: Sequence[str] | None, work_dir: Path, ) -> None: - self._pos_args = None if pos_args is None else tuple(pos_args) self._work_dir = work_dir self._root = root @@ -93,6 +92,9 @@ def make(cls, parsed: Parsed, pos_args: Sequence[str] | None, source: Source) -> # work dir is where we put our own files root: Path = source.path.parent if parsed.root_dir is None else parsed.root_dir work_dir: Path = source.path.parent if parsed.work_dir is None else parsed.work_dir + # if these are relative we need to expand them them to ensure paths built on this can resolve independent on cwd + root = root.resolve() + work_dir = work_dir.resolve() return cls( config_source=source, options=parsed, diff --git a/tests/config/test_main.py b/tests/config/test_main.py index f77f92cad..cff7d2f4a 100644 --- a/tests/config/test_main.py +++ b/tests/config/test_main.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +from pathlib import Path from tests.conftest import ToxIniCreator from tox.config.loader.api import Override @@ -83,3 +84,19 @@ def test_args_are_paths_when_with_change_dir(tox_project: ToxProjectCreator) -> result = project.run("c", "-e", "py", "-k", "commands", "--", *args) result.assert_success() assert result.out == f"[testenv:py]\ncommands = magic.py {project.path} ..{os.sep}tox.ini a.txt . ..\n" + + +def test_relative_config_paths_resolve(tox_project: ToxProjectCreator) -> None: + project = tox_project({"tox.ini": "[tox]"}) + result = project.run( + "c", + "-c", + str(Path(project.path.name) / "tox.ini"), + "-k", + "change_dir", + "env_dir", + from_cwd=project.path.parent, + ) + result.assert_success() + expected = f"[testenv:py]\nchange_dir = {project.path}\nenv_dir = {project.path / '.tox' / 'py'}\n\n[tox]\n" + assert result.out == expected