Skip to content

Commit d8f4c89

Browse files
committed
add tmp_dir_factory fixture
1 parent fa804ab commit d8f4c89

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

pytest_test_utils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ._any import ANY
22
from .tmp_dir import TmpDir
3+
from .tmp_dir_factory import TempDirFactory
34

4-
__all__ = ["ANY", "TmpDir"]
5+
__all__ = ["ANY", "TmpDir", "TempDirFactory"]

pytest_test_utils/pytest_plugin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
from typing import Iterator
33

44
import pytest
5+
from pytest import TempPathFactory
56

6-
from .tmp_dir import TmpDir
7+
from . import TempDirFactory, TmpDir
8+
9+
10+
@pytest.fixture(scope="session")
11+
def tmp_dir_factory(tmp_path_factory: TempPathFactory) -> TempDirFactory:
12+
return TempDirFactory(tmp_path_factory)
713

814

915
@pytest.fixture
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import TYPE_CHECKING
2+
3+
from .tmp_dir import TmpDir
4+
5+
if TYPE_CHECKING:
6+
from pytest import TempPathFactory
7+
8+
9+
class TempDirFactory:
10+
def __init__(self, tmp_path_factory: "TempPathFactory") -> None:
11+
self.tmp_path_factory: "TempPathFactory" = tmp_path_factory
12+
13+
def mktemp(self, basename: str, numbered: bool = True) -> TmpDir:
14+
return TmpDir(
15+
self.tmp_path_factory.mktemp(basename, numbered=numbered)
16+
)
17+
18+
def getbasetemp(self) -> TmpDir:
19+
return TmpDir(self.tmp_path_factory.getbasetemp())

tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from pytest_test_utils import ANY, TmpDir
7+
from pytest_test_utils.tmp_dir_factory import TempDirFactory
78

89

910
def test_any() -> None:
@@ -71,3 +72,16 @@ def test_cat(tmp_dir: TmpDir) -> None:
7172
assert tmp_dir.cat() == {"dir": {"file": "lorem ipsum"}}
7273
assert (tmp_dir / "dir").cat() == {"file": "lorem ipsum"}
7374
assert (tmp_dir / "dir" / "file").cat() == "lorem ipsum"
75+
76+
77+
def test_tmp_dir_factory(tmp_dir_factory: TempDirFactory) -> None:
78+
tmp_dir = tmp_dir_factory.mktemp("test-dir")
79+
assert isinstance(tmp_dir, TmpDir)
80+
assert isinstance(tmp_dir, os.PathLike)
81+
assert isinstance(tmp_dir, Path)
82+
assert "test-dir0" in tmp_dir.name
83+
84+
tmp_dir.gen({"foo": "foo"})
85+
assert tmp_dir.cat() == {"foo": "foo"}
86+
87+
assert "test-dir1" in tmp_dir_factory.mktemp("test-dir").name

0 commit comments

Comments
 (0)