Merge lp:~stevenk/python-fixtures/fakeopen-with into lp:~python-fixtures/python-fixtures/trunk

Proposed by Steve Kowalik
Status: Merged
Merged at revision: 77
Proposed branch: lp:~stevenk/python-fixtures/fakeopen-with
Merge into: lp:~python-fixtures/python-fixtures/trunk
Diff against target: 48 lines (+17/-0)
3 files modified
NEWS (+6/-0)
lib/fixtures/_fixtures/popen.py (+6/-0)
lib/fixtures/tests/_fixtures/test_popen.py (+5/-0)
To merge this branch: bzr merge lp:~stevenk/python-fixtures/fakeopen-with
Reviewer Review Type Date Requested Status
python-fixtures committers Pending
Review via email: mp+231149@code.launchpad.net

Description of the change

Add __enter__() and __exit__() methods to FakeProcess (which is returned by FakePopen) to allow FakePopen to be used within a context manager itself.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS'
2--- NEWS 2013-08-16 03:04:37 +0000
3+++ NEWS 2014-08-18 06:16:30 +0000
4@@ -6,6 +6,12 @@
5 NEXT
6 ~~~~
7
8+CHANGES
9+-------
10+
11+* ``FakePopen`` now supports being called under a context manager (IE: with).
12+ (Steve Kowalik)
13+
14 0.3.14
15 ~~~~~~
16
17
18=== modified file 'lib/fixtures/_fixtures/popen.py'
19--- lib/fixtures/_fixtures/popen.py 2013-08-16 03:03:08 +0000
20+++ lib/fixtures/_fixtures/popen.py 2014-08-18 06:16:30 +0000
21@@ -48,6 +48,12 @@
22 err = ''
23 return out, err
24
25+ def __enter__(self):
26+ return self
27+
28+ def __exit__(self, exc_type, exc_value, traceback):
29+ self.wait()
30+
31 def wait(self):
32 if self.returncode is None:
33 self.communicate()
34
35=== modified file 'lib/fixtures/tests/_fixtures/test_popen.py'
36--- lib/fixtures/tests/_fixtures/test_popen.py 2013-08-16 03:03:08 +0000
37+++ lib/fixtures/tests/_fixtures/test_popen.py 2014-08-18 06:16:30 +0000
38@@ -72,6 +72,11 @@
39 self.assertEqual(1, proc.wait())
40 self.assertEqual(1, proc.returncode)
41
42+ def test_with_popen_custom(self):
43+ fixture = self.useFixture(FakePopen())
44+ with subprocess.Popen(['ls -lh']) as proc:
45+ self.assertEqual(None, proc.returncode)
46+
47
48 class TestFakeProcess(testtools.TestCase):
49

Subscribers

People subscribed via source and target branches