File tree Expand file tree Collapse file tree 4 files changed +42
-2
lines changed Expand file tree Collapse file tree 4 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 11from ._any import ANY
22from .tmp_dir import TmpDir
3+ from .tmp_dir_factory import TempDirFactory
34
4- __all__ = ["ANY" , "TmpDir" ]
5+ __all__ = ["ANY" , "TmpDir" , "TempDirFactory" ]
Original file line number Diff line number Diff line change 22from typing import Iterator
33
44import 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
Original file line number Diff line number Diff line change 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 ())
Original file line number Diff line number Diff line change 44import pytest
55
66from pytest_test_utils import ANY , TmpDir
7+ from pytest_test_utils .tmp_dir_factory import TempDirFactory
78
89
910def 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
You can’t perform that action at this time.
0 commit comments