-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: factory fixture for creating temporary copies of directory trees
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Pytest fixture factory for making temporary copies of directory trees.""" | ||
|
||
from __future__ import annotations | ||
|
||
import re | ||
from contextlib import contextmanager | ||
from shutil import copytree | ||
from typing import TYPE_CHECKING, Callable, ContextManager | ||
|
||
import pytest | ||
|
||
if TYPE_CHECKING: | ||
from pathlib import Path | ||
from typing import Generator | ||
|
||
|
||
@pytest.fixture | ||
def make_temp_copy( | ||
request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory | ||
) -> Callable[[Path], ContextManager[Path]]: | ||
"""Pytest fixture to create a temporary clone of a directory structure.""" | ||
|
||
@contextmanager | ||
def temp_copy_factory(path: Path) -> Generator[Path]: | ||
max_len = 30 | ||
name = re.sub(r"\W", "_", f"clone_{request.node.name}")[:max_len] | ||
clone = tmp_path_factory.mktemp(name, numbered=True) / path.name | ||
copytree(path, clone) | ||
yield clone | ||
|
||
return temp_copy_factory |