Browse Source

simplify changedir with @contextmanager

Thomas Waldmann 1 year ago
parent
commit
a2e7854911
1 changed files with 8 additions and 12 deletions
  1. 8 12
      src/borg/testsuite/__init__.py

+ 8 - 12
src/borg/testsuite/__init__.py

@@ -76,6 +76,14 @@ def unopened_tempfile():
         yield os.path.join(tempdir, "file")
 
 
+@contextmanager
+def changedir(dir):
+    cwd = os.getcwd()
+    os.chdir(dir)
+    yield
+    os.chdir(cwd)
+
+
 def is_root():
     """return True if running with high privileges, like as root"""
     if is_win32:
@@ -195,18 +203,6 @@ class BaseTestCase(unittest.TestCase):
         assert_raises = unittest.TestCase.assertRaises  # type: ignore
 
 
-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 FakeInputs:
     """Simulate multiple user inputs, can be used as input() replacement"""