def test_prompt_int_question_no_help_string(self, _): my_response = '?' with mock.patch('logging.Logger.warning') as mock_log_warn: with self.assertRaises(StopIteration): with mock.patch('knack.prompting._input', side_effect=['?']): prompt_int('Please enter a number: ') mock_log_warn.assert_called_once_with('%s is not a valid number', my_response)
def test_prompt_int_nan(self, _): my_response = 'This is clearly not a number.' with mock.patch('logging.Logger.warning') as mock_log_warn: with self.assertRaises(StopIteration): with mock.patch('knack.prompting._input', side_effect=[my_response]): prompt_int('Please enter some text: ') mock_log_warn.assert_called_once_with('%s is not a valid number', my_response)
def test_prompt_int_question_with_help_string(self, _): my_response = '42' with mock.patch('knack.prompting._input', side_effect=['?', my_response]): with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout: actual_result = prompt_int('Please enter a number: ', help_string='Anything you want!') self.assertEqual(int(my_response), actual_result) self.assertIn('Anything you want!', mock_stdout.getvalue())
def _prompt_for_parameters(missing_parameters, fail_on_no_tty=True): # pylint: disable=too-many-statements prompt_list = missing_parameters.keys() if isinstance(missing_parameters, OrderedDict) \ else sorted(missing_parameters) result = OrderedDict() no_tty = False for param_name in prompt_list: param = missing_parameters[param_name] param_type = param.get('type', 'string') description = 'Missing description' metadata = param.get('metadata', None) if metadata is not None: description = metadata.get('description', description) allowed_values = param.get('allowedValues', None) prompt_str = "Please provide {} value for '{}' (? for help): ".format( param_type, param_name) while True: if allowed_values is not None: try: ix = prompt_choice_list(prompt_str, allowed_values, help_string=description) result[param_name] = allowed_values[ix] except NoTTYException: result[param_name] = None no_tty = True break elif param_type == 'securestring': try: value = prompt_pass(prompt_str, help_string=description) except NoTTYException: value = None no_tty = True result[param_name] = value break elif param_type == 'int': try: int_value = prompt_int(prompt_str, help_string=description) result[param_name] = int_value except NoTTYException: result[param_name] = 0 no_tty = True break elif param_type == 'bool': try: value = prompt_t_f(prompt_str, help_string=description) result[param_name] = value except NoTTYException: result[param_name] = False no_tty = True break elif param_type in ['object', 'array']: try: value = prompt(prompt_str, help_string=description) except NoTTYException: value = '' no_tty = True if value == '': value = {} if param_type == 'object' else [] else: try: value = shell_safe_json_parse(value) except Exception as ex: # pylint: disable=broad-except logger.error(ex) continue result[param_name] = value break else: try: result[param_name] = prompt(prompt_str, help_string=description) except NoTTYException: result[param_name] = None no_tty = True break if no_tty and fail_on_no_tty: raise NoTTYException return result
def _prompt_for_parameters(missing_parameters, fail_on_no_tty=True): # pylint: disable=too-many-statements prompt_list = missing_parameters.keys() if isinstance(missing_parameters, OrderedDict) \ else sorted(missing_parameters) result = OrderedDict() no_tty = False for param_name in prompt_list: param = missing_parameters[param_name] param_type = param.get('type', 'string') description = 'Missing description' metadata = param.get('metadata', None) if metadata is not None: description = metadata.get('description', description) allowed_values = param.get('allowedValues', None) prompt_str = "Please provide {} value for '{}' (? for help): ".format(param_type, param_name) while True: if allowed_values is not None: try: ix = prompt_choice_list(prompt_str, allowed_values, help_string=description) result[param_name] = allowed_values[ix] except NoTTYException: result[param_name] = None no_tty = True break elif param_type == 'securestring': try: value = prompt_pass(prompt_str, help_string=description) except NoTTYException: value = None no_tty = True result[param_name] = value break elif param_type == 'int': try: int_value = prompt_int(prompt_str, help_string=description) result[param_name] = int_value except NoTTYException: result[param_name] = 0 no_tty = True break elif param_type == 'bool': try: value = prompt_t_f(prompt_str, help_string=description) result[param_name] = value except NoTTYException: result[param_name] = False no_tty = True break elif param_type in ['object', 'array']: try: value = prompt(prompt_str, help_string=description) except NoTTYException: value = '' no_tty = True if value == '': value = {} if param_type == 'object' else [] else: try: value = shell_safe_json_parse(value) except Exception as ex: # pylint: disable=broad-except logger.error(ex) continue result[param_name] = value break else: try: result[param_name] = prompt(prompt_str, help_string=description) except NoTTYException: result[param_name] = None no_tty = True break if no_tty and fail_on_no_tty: raise NoTTYException return result
def test_prompt_int(self, _): my_response = '42' with mock.patch('knack.prompting._input', return_value=my_response): actual_result = prompt_int('Please enter a number: ') self.assertEqual(int(my_response), actual_result)