X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/f2ea461e9e9fa5c47bb61fd72d512c748928badc..41e670064063e3e221b1c3c40db5aaae784b5231:/tests/util.py?ds=sidebyside diff --git a/tests/util.py b/tests/util.py index 1e86a3f..e83017f 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,7 +1,8 @@ import os import unittest from pathlib import Path -from typing import List, Tuple, Any +from typing import Iterator, List, Tuple, Any +from contextlib import contextmanager from functools import partial import black @@ -72,3 +73,14 @@ def read_data_from_file(file_name: Path) -> Tuple[str, str]: # If there's no output marker, treat the entire file as already pre-formatted. _output = _input[:] return "".join(_input).strip() + "\n", "".join(_output).strip() + "\n" + + +@contextmanager +def change_directory(path: Path) -> Iterator[None]: + """Context manager to temporarily chdir to a different directory.""" + previous_dir = os.getcwd() + try: + os.chdir(path) + yield + finally: + os.chdir(previous_dir)