浏览代码

Implement custom context manager for changing CWD

Signed-off-by: Lumír Balhar <[email protected]>
Lumir Balhar 6 年之前
父节点
当前提交
60458c8ae7
共有 1 个文件被更改,包括 15 次插入0 次删除
  1. 15 0
      tests/helpers.py

+ 15 - 0
tests/helpers.py

@@ -1,6 +1,7 @@
 from __future__ import absolute_import
 from __future__ import absolute_import
 from __future__ import unicode_literals
 from __future__ import unicode_literals
 
 
+import contextlib
 import os
 import os
 
 
 from compose.config.config import ConfigDetails
 from compose.config.config import ConfigDetails
@@ -55,3 +56,17 @@ def create_host_file(client, filename):
         content = fh.read()
         content = fh.read()
 
 
     return create_custom_host_file(client, filename, content)
     return create_custom_host_file(client, filename, content)
+
+
[email protected]
+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)