Skip to content

Commit 32c7d7f

Browse files
committed
tmp_dir: add cat
1 parent 345dee9 commit 32c7d7f

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

pytest_test_utils/tmp_dir.py

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def chdir(self) -> Iterator[None]:
3333
yield
3434
finally:
3535
os.chdir(old)
36+
37+
def cat(self):
38+
if self.is_dir():
39+
return {p.name: p.cat() for p in self.iterdir()}
40+
return self.read_text(encoding="utf-8")

pytest_test_utils/tmp_dir.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ AnyStruct = Dict[AnyPath[T], Union[Text, Dict[AnyPath[T], Any]]]
99
StrStruct = AnyStruct[str]
1010
BytesStruct = AnyStruct[bytes]
1111

12+
CatStruct = Union[str, Dict[str, Union[str, Dict[str, Any]]]]
13+
1214
class TmpDir(Path):
1315
@overload
1416
def gen(self, struct: AnyPath[T], text: Text = "") -> List[T]: ...
@@ -17,3 +19,4 @@ class TmpDir(Path):
1719
@overload
1820
def gen(self, struct: StrStruct, text: Text = "") -> List[str]: ...
1921
def chdir(self) -> ContextManager[None]: ...
22+
def cat(self) -> CatStruct: ...

tests.py

+8
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ def test_chdir(tmp_path: Path, tmp_dir: TmpDir) -> None:
5151
assert Path.cwd() == subdir
5252
assert Path.cwd() == tmp_path / "dir"
5353
assert Path.cwd() == tmp_dir / "dir"
54+
55+
56+
def test_cat(tmp_dir: TmpDir) -> None:
57+
tmp_dir.gen({"dir": {"file": "lorem ipsum"}})
58+
59+
assert tmp_dir.cat() == {"dir": {"file": "lorem ipsum"}}
60+
assert (tmp_dir / "dir").cat() == {"file": "lorem ipsum"}
61+
assert (tmp_dir / "dir" / "file").cat() == "lorem ipsum"

0 commit comments

Comments
 (0)