diff options
Diffstat (limited to 'infra/test_helpers.py')
-rw-r--r-- | infra/test_helpers.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/infra/test_helpers.py b/infra/test_helpers.py index 5a9a4d582..be0b1b811 100644 --- a/infra/test_helpers.py +++ b/infra/test_helpers.py @@ -13,7 +13,10 @@ # limitations under the License. """Contains convenient helpers for writing tests.""" +import contextlib import os +import shutil +import tempfile from unittest import mock @@ -25,3 +28,12 @@ def patch_environ(testcase_obj, env=None): patcher = mock.patch.dict(os.environ, env) testcase_obj.addCleanup(patcher.stop) patcher.start() + + +@contextlib.contextmanager +def temp_dir_copy(directory): + """Context manager that yields a temporary copy of |directory|.""" + with tempfile.TemporaryDirectory() as temp_dir: + temp_copy_path = os.path.join(temp_dir, os.path.basename(directory)) + shutil.copytree(directory, temp_copy_path) + yield temp_copy_path |