summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2014-08-22 14:44:00 +1200
committerRobert Collins <robertc@robertcollins.net>2014-08-22 14:44:00 +1200
commit1777cf19bf816bb9d3cc054ad502954ae3a0a65c (patch)
tree06d2104c98fc2429ed4ab713014b5dc4adb9c0a6
parent9ec1b3af612bf93882dda4ce07a6ef4329e1a6f8 (diff)
parent1d17cdade0aa8be68b39e79af50f52d273a9fbf2 (diff)
downloadfixtures-1777cf19bf816bb9d3cc054ad502954ae3a0a65c.tar.gz
* ``FakePopen`` now supports being called under a context manager (IE: with).
(Steve Kowalik)
-rw-r--r--NEWS3
-rw-r--r--lib/fixtures/_fixtures/popen.py6
-rw-r--r--lib/fixtures/tests/_fixtures/test_popen.py5
3 files changed, 14 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index d89bdd4..31af1aa 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,9 @@ NEXT
CHANGES
-------
+* ``FakePopen`` now supports being called under a context manager (IE: with).
+ (Steve Kowalik)
+
* ``MonkeyPatch`` now preserves ``staticmethod`` functions.
(Dan Kenigsberg)
diff --git a/lib/fixtures/_fixtures/popen.py b/lib/fixtures/_fixtures/popen.py
index 708c5fd..80629fd 100644
--- a/lib/fixtures/_fixtures/popen.py
+++ b/lib/fixtures/_fixtures/popen.py
@@ -48,6 +48,12 @@ class FakeProcess(object):
err = ''
return out, err
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self.wait()
+
def wait(self):
if self.returncode is None:
self.communicate()
diff --git a/lib/fixtures/tests/_fixtures/test_popen.py b/lib/fixtures/tests/_fixtures/test_popen.py
index dc9374c..8eb0174 100644
--- a/lib/fixtures/tests/_fixtures/test_popen.py
+++ b/lib/fixtures/tests/_fixtures/test_popen.py
@@ -72,6 +72,11 @@ class TestFakePopen(testtools.TestCase, TestWithFixtures):
self.assertEqual(1, proc.wait())
self.assertEqual(1, proc.returncode)
+ def test_with_popen_custom(self):
+ fixture = self.useFixture(FakePopen())
+ with subprocess.Popen(['ls -lh']) as proc:
+ self.assertEqual(None, proc.returncode)
+
class TestFakeProcess(testtools.TestCase):