I have a function that searches for .json files in directory recursively. It uses pathlib
.
def search(where: Path) -> List[Path]: ...
I want to unit test it, so I need to a way create fake Path
object with children, so that fake_path.iterdir()
and fake_path.resolve()
would work.
Ideally, I want this:
topdir = FakePath()
subdir1 = FakePath()
subdir1.add_children(Path('file1'), Path('file2'))
topdir.add_children(subdir1)
for dir in topdir.iterdir():
for file in dir.iterdir():
print(file.name)
>> file1
>> file2
and have it behave like normal Path object after that.
Is there a library that does that? Or can pathlib
do what I want, and I'm just blind?
(I know I can just create temp files, but I'd prefer to not have any IO in unit tests.)