From abccec1150d6fada29eae9819968e3d4419440ab Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Wed, 11 Feb 2015 11:54:48 +0000 Subject: Add helpers for patching open and stdout/stderr. --- tests/unittests/helpers.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'tests/unittests') diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py index 6b9394b3..ce77af93 100644 --- a/tests/unittests/helpers.py +++ b/tests/unittests/helpers.py @@ -254,6 +254,19 @@ class FilesystemMockingTestCase(ResourceUsingTestCase): self.patched_funcs.enter_context( mock.patch.object(mod, f, trap_func)) + def patchOpen(self, new_root): + trap_func = retarget_many_wrapper(new_root, 1, open) + name = 'builtins.open' if PY3 else '__builtin__.open' + self.patched_funcs.enter_context(mock.patch(name, trap_func)) + + def patchStdoutAndStderr(self, stdout=None, stderr=None): + if stdout is not None: + self.patched_funcs.enter_context( + mock.patch.object(sys, 'stdout', stdout)) + if stderr is not None: + self.patched_funcs.enter_context( + mock.patch.object(sys, 'stderr', stderr)) + class HttprettyTestCase(TestCase): # necessary as http_proxy gets in the way of httpretty -- cgit v1.2.3