From 60458c8ae7c3d99a4dd408bc7fbced3b4a8cd7de Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Tue, 27 Aug 2019 12:25:20 +0200 Subject: [PATCH] Implement custom context manager for changing CWD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Lumír Balhar --- tests/helpers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/helpers.py b/tests/helpers.py index 327715ee2..1365c5bcf 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from __future__ import unicode_literals +import contextlib import os from compose.config.config import ConfigDetails @@ -55,3 +56,17 @@ def create_host_file(client, filename): content = fh.read() return create_custom_host_file(client, filename, content) + + +@contextlib.contextmanager +def cd(path): + """ + A context manager which changes the working directory to the given + path, and then changes it back to its previous value on exit. + """ + prev_cwd = os.getcwd() + os.chdir(path) + try: + yield + finally: + os.chdir(prev_cwd)