ソースを参照

move test utilities to testsuite package, add FakeInputs utility

Thomas Waldmann 9 年 前
コミット
36900051c5
2 ファイル変更48 行追加30 行削除
  1. 47 0
      borg/testsuite/__init__.py
  2. 1 30
      borg/testsuite/archiver.py

+ 47 - 0
borg/testsuite/__init__.py

@@ -103,3 +103,50 @@ class BaseTestCase(unittest.TestCase):
                 return
             time.sleep(.1)
         raise Exception('wait_for_mount(%s) timeout' % path)
+
+
+class changedir:
+    def __init__(self, dir):
+        self.dir = dir
+
+    def __enter__(self):
+        self.old = os.getcwd()
+        os.chdir(self.dir)
+
+    def __exit__(self, *args, **kw):
+        os.chdir(self.old)
+
+
+class environment_variable:
+    def __init__(self, **values):
+        self.values = values
+        self.old_values = {}
+
+    def __enter__(self):
+        for k, v in self.values.items():
+            self.old_values[k] = os.environ.get(k)
+            if v is None:
+                os.environ.pop(k, None)
+            else:
+                os.environ[k] = v
+
+    def __exit__(self, *args, **kw):
+        for k, v in self.old_values.items():
+            if v is None:
+                os.environ.pop(k, None)
+            else:
+                os.environ[k] = v
+
+
+class FakeInputs:
+    """Simulate multiple user inputs, can be used as input() replacement"""
+    def __init__(self, inputs):
+        self.inputs = inputs
+
+    def __call__(self, prompt=None):
+        if prompt is not None:
+            print(prompt, end='')
+        try:
+            return self.inputs.pop(0)
+        except IndexError:
+            raise EOFError

+ 1 - 30
borg/testsuite/archiver.py

@@ -23,7 +23,7 @@ from ..crypto import bytes_to_long, num_aes_blocks
 from ..helpers import Manifest, EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR, st_atime_ns, st_mtime_ns
 from ..remote import RemoteRepository, PathNotAllowed
 from ..repository import Repository
-from . import BaseTestCase
+from . import BaseTestCase, changedir, environment_variable
 
 try:
     import llfuse
@@ -42,35 +42,6 @@ except NameError:
     PermissionError = OSError
 
 
-class changedir:
-    def __init__(self, dir):
-        self.dir = dir
-
-    def __enter__(self):
-        self.old = os.getcwd()
-        os.chdir(self.dir)
-
-    def __exit__(self, *args, **kw):
-        os.chdir(self.old)
-
-
-class environment_variable:
-    def __init__(self, **values):
-        self.values = values
-        self.old_values = {}
-
-    def __enter__(self):
-        for k, v in self.values.items():
-            self.old_values[k] = os.environ.get(k)
-            os.environ[k] = v
-
-    def __exit__(self, *args, **kw):
-        for k, v in self.old_values.items():
-            if v is None:
-                del os.environ[k]
-            else:
-                os.environ[k] = v
-
 def exec_cmd(*args, archiver=None, fork=False, exe=None, **kw):
     if fork:
         try: