Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create temp_dir if not exists #2781

Merged
merged 25 commits into from Dec 28, 2022
Merged
1 change: 1 addition & 0 deletions docs/changelog/2770.bugfix.rst
@@ -0,0 +1 @@
Create temp_dir if not exists - by :user:`q0w`.
2 changes: 1 addition & 1 deletion docs/config.rst
Expand Up @@ -172,7 +172,7 @@ Core

.. conf::
:keys: temp_dir
:default: {tox_root}/.temp
:default: {tox_root}/.tmp

Directory where to put tox temporary files. For example: we create a hard link (if possible, otherwise new copy) in
this directory for the project package. This ensures tox works correctly when having parallel runs (as each session
Expand Down
8 changes: 8 additions & 0 deletions src/tox/tox_env/api.py
Expand Up @@ -288,6 +288,7 @@ def _setup_env(self) -> None:
if eq is False and old is not None: # pragma: no branch # recreate if already created and not equals
raise Recreate(f"env type changed from {old} to {conf}")
self._handle_env_tmp_dir()
self._handle_core_tmp_dir()

def _setup_with_env(self) -> None: # noqa: B027 # empty abstract base class
pass
Expand All @@ -303,6 +304,13 @@ def _handle_env_tmp_dir(self) -> None:
ensure_empty_dir(env_tmp_dir)
env_tmp_dir.mkdir(parents=True, exist_ok=True)

def _handle_core_tmp_dir(self) -> None:
core_tmp_dir = self.core["temp_dir"]
if core_tmp_dir.exists() and next(core_tmp_dir.iterdir(), None) is not None:
LOGGER.debug("clear tox temp folder %s", core_tmp_dir)
ensure_empty_dir(core_tmp_dir)
q0w marked this conversation as resolved.
Show resolved Hide resolved
core_tmp_dir.mkdir(parents=True, exist_ok=True)

def _clean(self, transitive: bool = False) -> None: # noqa: U100
if self._run_state["clean"]: # pragma: no branch
return # pragma: no cover
Expand Down
8 changes: 8 additions & 0 deletions tests/config/test_sets.py
Expand Up @@ -179,3 +179,11 @@ def test_config_work_dir(tox_project: ToxProjectCreator, work_dir: str) -> None:
result = project.run("c", *(["--workdir", str(project.path / work_dir)] if work_dir else []))
expected = project.path / work_dir if work_dir else Path("b")
assert expected == result.state.conf.core["work_dir"]


def test_config_temp_dir(tox_project: ToxProjectCreator) -> None:
ini = '[testenv]\ncommands=python -c \'import os; os.mkdir(os.path.join("{temp_dir}", "foo"))\''
project = tox_project({"tox.ini": ini})
result = project.run()
result.assert_success()
assert (result.state.conf.core["temp_dir"] / "foo").exists()