1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from collections import namedtuple
import pytest
from awscli.autoprompt import core
from awscli.clidriver import create_clidriver
from awscli.customizations.exceptions import ParamValidationError
from awscli.testutils import mock, unittest
class TestCLIAutoPrompt(unittest.TestCase):
def setUp(self):
self.driver = mock.Mock()
self.prompter = mock.Mock(spec=core.AutoPrompter)
self.prompt_driver = core.AutoPromptDriver(
self.driver, prompter=self.prompter
)
def test_throw_error_if_both_args_specified(self):
args = ['--cli-auto-prompt', '--no-cli-auto-prompt']
self.assertRaises(
ParamValidationError,
self.prompt_driver.validate_auto_prompt_args_are_mutually_exclusive,
args,
)
def _generate_auto_prompt_resolve_cases():
# Each case is a 5-namedtuple with the following meaning:
# "args" is a list of arguments that command got as input from
# command line
# "config_variable" is the result from get_config_variable
# This takes a value of either 'on' , 'off' or 'on-partial'
# "expected_result" is a boolean indicating whether auto-prompt
# should be used or not.
#
# Note: This set of tests assumes that only one of --no-cli-auto-prompt
# or --cli-auto-prompt overrides can be specified.
# TestCLIAutoPrompt.test_throw_error_if_both_args_specified tests
# that these command line overrides are mutually exclusive.
Case = namedtuple(
'Case',
[
'args',
'config_variable',
'expected_result',
],
)
return [
Case([], 'off', 'off'),
Case([], 'on', 'on'),
Case(['--cli-auto-prompt'], 'off', 'on'),
Case(['--cli-auto-prompt'], 'on', 'on'),
Case(['--no-cli-auto-prompt'], 'off', 'off'),
Case(['--no-cli-auto-prompt'], 'on', 'off'),
Case([], 'on', 'on'),
Case([], 'on-partial', 'on-partial'),
Case(['--cli-auto-prompt'], 'on-partial', 'on'),
Case(['--no-cli-auto-prompt'], 'on-partial', 'off'),
Case(['--version'], 'on', 'off'),
Case(['help'], 'on', 'off'),
]
@pytest.mark.parametrize('case', _generate_auto_prompt_resolve_cases())
def test_auto_prompt_resolve_mode(case):
driver = create_clidriver()
driver.session.set_config_variable('cli_auto_prompt', case.config_variable)
prompter = mock.Mock(spec=core.AutoPrompter)
prompt_driver = core.AutoPromptDriver(driver, prompter=prompter)
result = prompt_driver.resolve_mode(args=case.args)
assert result == case.expected_result
def test_auto_prompt_resolve_mode_on_non_existing_profile():
driver = create_clidriver()
driver.session.set_config_variable('profile', 'not_exist')
prompter = mock.Mock(spec=core.AutoPrompter)
prompt_driver = core.AutoPromptDriver(driver, prompter=prompter)
result = prompt_driver.resolve_mode(args=[])
assert result == 'off'
class TestAutoPrompter(unittest.TestCase):
def setUp(self):
completion_source = mock.Mock()
driver = mock.Mock()
prompter = mock.Mock()
prompter.prompt_for_args = lambda x: x
driver.arg_table = []
self.auto_prompter = core.AutoPrompter(
completion_source, driver, prompter
)
def test_auto_prompter_returns_args(self):
original_args = ['aws', 'ec2', 'help']
prompted_values = self.auto_prompter.prompt_for_values(original_args)
self.assertEqual(prompted_values, original_args)
|