diff options
Diffstat (limited to 'nova/test.py')
-rw-r--r-- | nova/test.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/nova/test.py b/nova/test.py index 2adac89532..a7ed312c12 100644 --- a/nova/test.py +++ b/nova/test.py @@ -355,7 +355,7 @@ class TestCase(base.BaseTestCase): self.useFixture(fixtures.MonkeyPatch(old, new)) @staticmethod - def patch_exists(patched_path, result): + def patch_exists(patched_path, result, other=None): """Provide a static method version of patch_exists(), which if you haven't already imported nova.test can be slightly easier to use as a context manager within a test method via: @@ -364,7 +364,7 @@ class TestCase(base.BaseTestCase): with self.patch_exists(path, True): ... """ - return patch_exists(patched_path, result) + return patch_exists(patched_path, result, other) @staticmethod def patch_open(patched_path, read_data): @@ -845,10 +845,12 @@ class ContainKeyValue(object): @contextlib.contextmanager -def patch_exists(patched_path, result): +def patch_exists(patched_path, result, other=None): """Selectively patch os.path.exists() so that if it's called with patched_path, return result. Calls with any other path are passed - through to the real os.path.exists() function. + through to the real os.path.exists() function if other is not provided. + If other is provided then that will be the result of the call on paths + other than patched_path. Either import and use as a decorator / context manager, or use the nova.TestCase.patch_exists() static method as a context manager. @@ -882,7 +884,10 @@ def patch_exists(patched_path, result): def fake_exists(path): if path == patched_path: return result - return real_exists(path) + elif other is not None: + return other + else: + return real_exists(path) with mock.patch.object(os.path, "exists") as mock_exists: mock_exists.side_effect = fake_exists |