summaryrefslogtreecommitdiff
path: root/test/utils/path.py
blob: 40717474fdfda1c138b2469a81bab9fb95e333be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
from contextlib import contextmanager
from pathlib import PurePath
from typing import Iterator, TypeVar, Union

PathLike = Union[PurePath, str]
PathLikeT = TypeVar("PathLikeT", bound=PathLike)


@contextmanager
def ctx_chdir(newdir: PathLikeT) -> Iterator[PathLikeT]:
    cwd = os.getcwd()
    try:
        os.chdir(f"{newdir}")
        yield newdir
    finally:
        os.chdir(cwd)