Fixing read before prompt bug
Due to Python buffering sys.stdin and sys.stdout, the read can be shown
before text prompt.
MODIFY
heatclient/osc/v1/stack.py
heatclient/osc/v1/stack.py
@@ -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.')

MODIFY
heatclient/tests/unit/test_shell.py
heatclient/tests/unit/test_shell.py
@@ -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')

MODIFY
heatclient/v1/shell.py
heatclient/v1/shell.py
@@ -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.')

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>