From 3808f7fe58f9e5724fbf9ab7e213cde4f21e26f2 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Mon, 21 Nov 2011 19:30:40 -0600 Subject: Added tests for shell ordering in pshell. --- pyramid/scripts/pshell.py | 34 ++++++++++++--------- pyramid/tests/test_scripts/test_pshell.py | 49 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/pyramid/scripts/pshell.py b/pyramid/scripts/pshell.py index 8cf1a4b39..72e2c0a89 100644 --- a/pyramid/scripts/pshell.py +++ b/pyramid/scripts/pshell.py @@ -38,7 +38,7 @@ class PShellCommand(object): parser = optparse.OptionParser() parser.add_option('-p', '--python-shell', action='store', type='string', dest='python_shell', - default = '', help='ipython | bpython | python') + default='', help='ipython | bpython | python') parser.add_option('--setup', dest='setup', help=("A callable that will be passed the environment " @@ -141,30 +141,36 @@ class PShellCommand(object): for var in sorted(self.object_help.keys()): help += '\n %-12s %s' % (var, self.object_help[var]) + if shell is None: + shell = self.make_shell() + + try: + shell(env, help) + finally: + closer() + + def make_shell(self): + shell = None user_shell = self.options.python_shell.lower() if not user_shell: + shell = self.make_ipython_v0_11_shell() if shell is None: - shell = self.make_ipython_v0_11_shell() - if shell is None: - shell = self.make_ipython_v0_10_shell() - if shell is None: - shell = self.make_bpython_shell() + shell = self.make_ipython_v0_10_shell() + if shell is None: + shell = self.make_bpython_shell() - if shell is None and user_shell == 'ipython': + elif user_shell == 'ipython': shell = self.make_ipython_v0_11_shell() if shell is None: shell = self.make_ipython_v0_10_shell() - if shell is None and user_shell == 'bpython': + elif user_shell == 'bpython': shell = self.make_bpython_shell() if shell is None: shell = self.make_default_shell() - try: - shell(env, help) - finally: - closer() + return shell def make_default_shell(self, interact=interact): def shell(env, help): @@ -174,8 +180,8 @@ class PShellCommand(object): interact(banner, local=env) return shell - def make_bpython_shell(self, BPShellFactory=None): - if BPShellFactory is None: # pragma: no cover + def make_bpython_shell(self, BPShell=None): + if BPShell is None: # pragma: no cover try: from bpython import embed BPShell = embed diff --git a/pyramid/tests/test_scripts/test_pshell.py b/pyramid/tests/test_scripts/test_pshell.py index 4e5deb31d..c1f648b6f 100644 --- a/pyramid/tests/test_scripts/test_pshell.py +++ b/pyramid/tests/test_scripts/test_pshell.py @@ -171,6 +171,55 @@ class TestPShellCommand(unittest.TestCase): self.assertTrue(self.bootstrap.closer.called) self.assertTrue(shell.banner) + def test_shell_ipython_ordering(self): + command = self._makeOne() + shell0_11 = dummy.DummyShell() + shell0_10 = dummy.DummyShell() + command.make_ipython_v0_11_shell = lambda: shell0_11 + command.make_ipython_v0_10_shell = lambda: shell0_10 + command.make_bpython_shell = lambda: None + shell = command.make_shell() + self.assertEqual(shell, shell0_11) + + command.options.python_shell = 'ipython' + shell = command.make_shell() + self.assertEqual(shell, shell0_11) + + def test_shell_ordering(self): + command = self._makeOne() + ipshell = dummy.DummyShell() + bpshell = dummy.DummyShell() + dshell = dummy.DummyShell() + command.make_ipython_v0_11_shell = lambda: None + command.make_ipython_v0_10_shell = lambda: None + command.make_bpython_shell = lambda: None + command.make_default_shell = lambda: dshell + + shell = command.make_shell() + self.assertEqual(shell, dshell) + + command.options.python_shell = 'ipython' + shell = command.make_shell() + self.assertEqual(shell, dshell) + + command.options.python_shell = 'bpython' + shell = command.make_shell() + self.assertEqual(shell, dshell) + + command.make_ipython_v0_11_shell = lambda: ipshell + command.make_bpython_shell = lambda: bpshell + command.options.python_shell = 'ipython' + shell = command.make_shell() + self.assertEqual(shell, ipshell) + + command.options.python_shell = 'bpython' + shell = command.make_shell() + self.assertEqual(shell, bpshell) + + command.options.python_shell = 'python' + shell = command.make_shell() + self.assertEqual(shell, dshell) + def test_command_loads_custom_items(self): command = self._makeOne() model = dummy.Dummy() -- cgit v1.2.3