Skip to content

Commit

Permalink
Create temp_dir if not exists (#2781)
Browse files Browse the repository at this point in the history
Closes #2770
  • Loading branch information
q0w committed Dec 28, 2022
1 parent c850edf commit 39dd2b6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
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
4 changes: 4 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,9 @@ 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:
self.core["temp_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
18 changes: 18 additions & 0 deletions tests/tox_env/test_api.py
@@ -0,0 +1,18 @@
from pathlib import Path

from tox.pytest import ToxProjectCreator


def test_ensure_temp_dir_exists(tox_project: ToxProjectCreator) -> None:
ini = "[testenv]\ncommands=python -c 'import os; os.path.exists(r\"{temp_dir}\")'"
project = tox_project({"tox.ini": ini})
result = project.run()
result.assert_success()


def test_dont_cleanup_temp_dir(tox_project: ToxProjectCreator, tmp_path: Path) -> None:
(tmp_path / "foo" / "bar").mkdir(parents=True)
project = tox_project({"tox.ini": "[tox]\ntemp_dir=foo"})
result = project.run()
result.assert_success()
assert (tmp_path / "foo" / "bar").exists()

0 comments on commit 39dd2b6

Please sign in to comment.