Skip to content

Commit

Permalink
Fixed how the six.moves.input function is imported and used.
Browse files Browse the repository at this point in the history
Also added a unit test for the cmd2.Cmd.select() method.
  • Loading branch information
tleonhardt committed Feb 5, 2017
1 parent 59bd32f commit ce71e89
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
10 changes: 5 additions & 5 deletions cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
# Possible types for text data. This is basestring() in Python 2 and str in Python 3.
from six import string_types

# raw_input() for Python 2 or input() for Python 3
from six.moves import input
# Used for sm.input: raw_input() for Python 2 or input() for Python 3
import six.moves as sm

# itertools.zip() for Python 2 or zip() for Python 3 - produces an iterator in both cases
from six.moves import zip
Expand Down Expand Up @@ -1010,7 +1010,7 @@ def pseudo_raw_input(self, prompt):

if self.use_rawinput:
try:
line = input(prompt)
line = sm.input(prompt)
except EOFError:
line = 'EOF'
else:
Expand Down Expand Up @@ -1103,7 +1103,7 @@ def select(self, options, prompt='Your choice? '):
for (idx, (value, text)) in enumerate(fulloptions):
self.poutput(' %2d. %s\n' % (idx + 1, text))
while True:
response = input(prompt)
response = sm.input(prompt)
try:
response = int(response)
result = fulloptions[response - 1][0]
Expand Down Expand Up @@ -1164,7 +1164,7 @@ def do_set(self, arg):

def do_pause(self, arg):
'Displays the specified text then waits for the user to press RETURN.'
input(arg + '\n')
sm.input(arg + '\n')

def do_shell(self, arg):
'execute a command as if at the OS prompt.'
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
""".splitlines()))

INSTALL_REQUIRES = ['pyparsing >= 2.0.1', 'six']
# unitest.mock was added in Python 3.3. mock is a backport of unittest.mock to all versions of Python
TESTS_REQUIRE = ['mock', 'pytest']

setup(
Expand Down
33 changes: 31 additions & 2 deletions tests/test_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
"""
import sys

import mock
import pytest
from mock import patch

# Used for sm.input: raw_input() for Python 2 or input() for Python 3
import six.moves as sm

from cmd2 import Cmd, make_option, options, Cmd2TestCase
from conftest import run_cmd, StdOut, normalize
Expand Down Expand Up @@ -55,6 +58,13 @@ def do_hello(self, arg, opts):
else:
self.stdout.write('Hello Nobody\n')

def do_eat(self, arg):
"""Eat something, with a selection of sauces to choose from."""
sauce = self.select('sweet salty', 'Sauce? ')
result = '{food} with {sauce} sauce, yum!'
result = result.format(food=arg, sauce=sauce)
self.stdout.write(result + '\n')


@pytest.fixture
def _cmdline_app():
Expand Down Expand Up @@ -217,11 +227,30 @@ def test_optarser_options_with_spaces_in_quotes(_demo_app):
def test_commands_at_invocation(_cmdline_app):
testargs = ["prog", "say hello", "say Gracie", "quit"]
expected = "hello\nGracie\n"
with patch.object(sys, 'argv', testargs):
with mock.patch.object(sys, 'argv', testargs):
app = CmdLineApp()
app.stdout = StdOut()
app.cmdloop()
out = app.stdout.buffer
assert out == expected


def test_select_options(_demo_app):
# Mock out the input call so we don't actually wait for a user's `response on stdin
m = mock.MagicMock(name='input', return_value='2')
sm.input = m

food = 'bacon'
out = run_cmd(_demo_app, "set debug true")
out = run_cmd(_demo_app, "eat {}".format(food))
expected = normalize("""
1. sweet
2. salty
{} with salty sauce, yum!
""".format(food))

# Make sure our mock was called with the expected arguments
m.assert_called_once_with('Sauce? ')

# And verify the expected output to stdout
assert out == expected

0 comments on commit ce71e89

Please sign in to comment.