Fixing read before prompt bug

Due to Python buffering sys.stdin and sys.stdout, the read can be shown
before text prompt.

Change-Id: I86d9ec62f7791da276585ce74a48162315effa7b
Signed-off-by: Joe Borg <joe@josephb.org>
This commit is contained in:
Joe Borg
2017-07-25 15:30:02 +01:00
parent 6462e5183f
commit b45c792f95
3 changed files with 9 additions and 7 deletions

View File

@@ -684,9 +684,9 @@ class DeleteStack(command.Command):
try:
if not parsed_args.yes and sys.stdin.isatty():
sys.stdout.write(
_("Are you sure you want to delete this stack(s) [y/N]? "))
prompt_response = sys.stdin.readline().lower()
prompt_response = six.moves.input(
_("Are you sure you want to delete this stack(s) [y/N]? ")
).lower()
if not prompt_response.startswith('y'):
self.log.info('User did not confirm stack delete so '
'taking no action.')

View File

@@ -2067,6 +2067,7 @@ class ShellTestUserPass(ShellBase):
mock_stdin.isatty.return_value = True
mock_stdin.readline = mock.Mock()
mock_stdin.readline.return_value = 'n'
mock_stdin.fileno.return_value = 0
sys.stdin = mock_stdin
self.mock_request_delete('/stacks/teststack2/2', None)
@@ -2078,7 +2079,7 @@ class ShellTestUserPass(ShellBase):
self.assertEqual(resp_text, resp)
self.m.ReplayAll()
mock_stdin.readline.return_value = 'Y'
mock_stdin.readline.return_value = 'y'
resp = self.shell('stack-delete teststack2/2')
msg = 'Request to delete stack teststack2/2 has been accepted.'
self.assertRegex(resp, msg)
@@ -2093,6 +2094,7 @@ class ShellTestUserPass(ShellBase):
mock_stdin.isatty.return_value = True
mock_stdin.readline = mock.Mock()
mock_stdin.readline.return_value = ''
mock_stdin.fileno.return_value = 0
sys.stdin = mock_stdin
self.mock_request_delete('/stacks/teststack2/2')

View File

@@ -302,9 +302,9 @@ def do_stack_delete(hc, args):
try:
if not args.yes and sys.stdin.isatty():
sys.stdout.write(
_("Are you sure you want to delete this stack(s) [y/N]? "))
prompt_response = sys.stdin.readline().lower()
prompt_response = six.moves.input(
_("Are you sure you want to delete this stack(s) [y/N]? ")
).lower()
if not prompt_response.startswith('y'):
logger.info(
'User did not confirm stack delete so taking no action.')