summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2013-08-16 12:36:42 +1200
committerRobert Collins <robertc@robertcollins.net>2013-08-16 12:36:42 +1200
commit5211a9db5107b7f0c5540e8525647a4bab46cdf3 (patch)
treef5af1924808af5266579140b8889d415e937a526
parent039ff7b76ca753b9241724f63e3acc67ff623afb (diff)
downloadfixtures-5211a9db5107b7f0c5540e8525647a4bab46cdf3.tar.gz
* ``FakePopen`` now accepts all the parameters available in Python 2.7.
(Robert Collins) * ``FakePopen`` now only passes parameters to the get_info routine if the caller supplied them. (Robert Collins)
-rw-r--r--NEWS9
-rw-r--r--lib/fixtures/_fixtures/popen.py33
-rw-r--r--lib/fixtures/tests/_fixtures/test_popen.py13
3 files changed, 50 insertions, 5 deletions
diff --git a/NEWS b/NEWS
index a79557d..470506f 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,15 @@ fixtures release notes
NEXT
~~~~
+CHANGES
+-------
+
+* ``FakePopen`` now accepts all the parameters available in Python 2.7.
+ (Robert Collins)
+
+* ``FakePopen`` now only passes parameters to the get_info routine if the
+ caller supplied them. (Robert Collins)
+
0.3.12
~~~~~~
diff --git a/lib/fixtures/_fixtures/popen.py b/lib/fixtures/_fixtures/popen.py
index 6f13a4c..15c48f2 100644
--- a/lib/fixtures/_fixtures/popen.py
+++ b/lib/fixtures/_fixtures/popen.py
@@ -60,13 +60,25 @@ class FakePopen(Fixture):
:ivar procs: A list of the processes created by the fixture.
"""
+ _unpassed = object()
+
def __init__(self, get_info=lambda _:{}):
"""Create a PopenFixture
:param get_info: Optional callback to control the behaviour of the
created process. This callback takes a kwargs dict for the Popen
call, and should return a dict with any desired attributes.
- e.g. return {'stdin': StringIO('foobar')}
+ Only parameters that are supplied to the Popen call are in the
+ dict, making it possible to detect the difference between 'passed
+ with a default value' and 'not passed at all'.
+
+ e.g.
+ def get_info(proc_args):
+ self.assertEqual(subprocess.PIPE, proc_args['stdin'])
+ return {'stdin': StringIO('foobar')}
+
+ The default behaviour if no get_info is supplied is for the return
+ process to have returncode of None, empty streams and a random pid.
"""
super(FakePopen, self).__init__()
self.get_info = get_info
@@ -77,10 +89,21 @@ class FakePopen(Fixture):
subprocess.Popen = self
self.procs = []
- def __call__(self, args, bufsize=0, executable=None, stdin=None,
- stdout=None, stderr=None):
- proc_args = dict(args=args, bufsize=bufsize, executable=executable,
- stdin=stdin, stdout=stdout, stderr=stderr)
+ # The method has the correct signature so we error appropriately if called
+ # wrongly.
+ def __call__(self, args, bufsize=_unpassed, executable=_unpassed,
+ stdin=_unpassed, stdout=_unpassed, stderr=_unpassed,
+ preexec_fn=_unpassed, close_fds=_unpassed, shell=_unpassed,
+ cwd=_unpassed, env=_unpassed, universal_newlines=_unpassed,
+ startupinfo=_unpassed, creationflags=_unpassed):
+ proc_args = dict(args=args)
+ local = locals()
+ for param in [
+ "bufsize", "executable", "stdin", "stdout", "stderr",
+ "preexec_fn", "close_fds", "shell", "cwd", "env",
+ "universal_newlines", "startupinfo", "creationflags"]:
+ if local[param] is not FakePopen._unpassed:
+ proc_args[param] = local[param]
proc_info = self.get_info(proc_args)
result = FakeProcess(proc_args, proc_info)
self.procs.append(result)
diff --git a/lib/fixtures/tests/_fixtures/test_popen.py b/lib/fixtures/tests/_fixtures/test_popen.py
index 59bef71..4c11619 100644
--- a/lib/fixtures/tests/_fixtures/test_popen.py
+++ b/lib/fixtures/tests/_fixtures/test_popen.py
@@ -51,6 +51,19 @@ class TestFakePopen(testtools.TestCase, TestWithFixtures):
proc = fixture(['foo'])
self.assertEqual('stdout', proc.stdout)
+ def test_handles_all_2_7_args(self):
+ all_args = dict(
+ args="args", bufsize="bufsize", executable="executable",
+ stdin="stdin", stdout="stdout", stderr="stderr",
+ preexec_fn="preexec_fn", close_fds="close_fds", shell="shell",
+ cwd="cwd", env="env", universal_newlines="universal_newlines",
+ startupinfo="startupinfo", creationflags="creationflags")
+ def get_info(proc_args):
+ self.assertEqual(all_args, proc_args)
+ return {}
+ fixture = self.useFixture(FakePopen(get_info))
+ proc = fixture(**all_args)
+
class TestFakeProcess(testtools.TestCase):