From 9c06672ad4e4bbc831785355dfb488a36f95dfad Mon Sep 17 00:00:00 2001 From: Jonas Pfannschmidt Date: Wed, 9 Mar 2016 17:59:01 +0000 Subject: [PATCH] Use six.moves.input rather than six.moves.builtin.input six.moves.builtin.input is the builtin python input. This makes the code fail in Python 2.7. six.moves.input links to raw_input which is the correct behaviour in this case. Fixes bug: 1555268 Change-Id: I87062c99dc17faedbd0705565cb7a46029b9fdf0 --- freezer_api/cmd/db_init.py | 4 ++-- freezer_api/tests/unit/test_db_init.py | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/freezer_api/cmd/db_init.py b/freezer_api/cmd/db_init.py index b55adbcb..6973f57f 100755 --- a/freezer_api/cmd/db_init.py +++ b/freezer_api/cmd/db_init.py @@ -21,8 +21,8 @@ import argparse import json import os import re -from six.moves import builtins from six.moves import configparser +from six.moves import input import sys import requests @@ -200,7 +200,7 @@ class ElastichsearchEngine(object): if assume_yes: return True while True: - selection = builtins.input(message) + selection = input(message) if selection.upper() == 'Y': return True elif selection.upper() == 'N': diff --git a/freezer_api/tests/unit/test_db_init.py b/freezer_api/tests/unit/test_db_init.py index 9701f253..a5d9aafc 100644 --- a/freezer_api/tests/unit/test_db_init.py +++ b/freezer_api/tests/unit/test_db_init.py @@ -16,7 +16,6 @@ limitations under the License. """ import os -from six.moves import builtins import unittest import json from mock import Mock, patch @@ -182,13 +181,17 @@ class TestElasticsearchEngine(unittest.TestCase): self.assertRaises(Exception, self.es_manager.put_mapping, 'jobs', self.test_mappings['jobs']) def test_proceed_returns_true_on_user_y(self): - with patch('six.moves.builtins.input', return_value='y') as _raw_input: + # This really mocks 'six.moves.input'. Because of the way mock and six + # replace function, we need to mock it at the source. + with patch('freezer_api.cmd.db_init.input', return_value='y') as _raw_input: res = self.es_manager.proceed('fancy a drink ?') self.assertTrue(res) _raw_input.assert_called_once_with('fancy a drink ?') def test_proceed_returns_false_on_user_n(self): - with patch('six.moves.builtins.input', return_value='n') as _raw_input: + # This really mocks 'six.moves.input'. Because of the way mock and six + # replace function, we need to mock it at the source. + with patch('freezer_api.cmd.db_init.input', return_value='n') as _raw_input: res = self.es_manager.proceed('are you drunk ?') self.assertFalse(res) _raw_input.assert_called_once_with('are you drunk ?')