File: test_doc.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (97 lines) | stat: -rw-r--r-- 4,120 bytes parent folder | download
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
# Copyright 2020 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 awscli.autocomplete import parser
from awscli.autoprompt.doc import DocsGetter
from awscli.clidriver import create_clidriver
from awscli.testutils import mock, unittest
from tests.unit.autocomplete import InMemoryIndex


class TestDocsGetter(unittest.TestCase):
    def setUp(self):
        self.driver = create_clidriver()
        self.docs_getter = DocsGetter(self.driver)
        self.index = InMemoryIndex(
            {
                'command_names': {
                    '': [('aws', None)],
                    'aws': [('ec2', None)],
                    'aws.ec2': [('describe-instances', None)],
                },
                'arg_names': {
                    '': {'aws': ['region']},
                    'aws.ec2': {'describe-instances': []},
                },
                'arg_data': {
                    '': {
                        'aws': {
                            'region': (
                                'region',
                                'string',
                                'aws',
                                '',
                                None,
                                False,
                                False,
                            ),
                        }
                    },
                    'aws.ec2': {'describe-instances': {}},
                },
            }
        )
        self.parser = parser.CLIParser(self.index)

    def test_get_service_command_docs(self):
        parsed_args = self.parser.parse('aws ec2')
        actual_docs = self.docs_getter.get_docs(parsed_args)
        expected_docs = 'Elastic Compute Cloud'
        self.assertIn(expected_docs, actual_docs)

    def test_get_service_operation_docs(self):
        parsed_args = self.parser.parse('aws ec2 describe-instances')
        actual_docs = self.docs_getter.get_docs(parsed_args)
        expected_docs = 'Describes the specified instances'
        self.assertIn(expected_docs, actual_docs)

    def test_get_service_command_docs_with_invalid_service_operation(self):
        parsed_args = self.parser.parse('aws ec2 fake')
        actual_docs = self.docs_getter.get_docs(parsed_args)
        expected_docs = 'Elastic Compute Cloud'
        self.assertIn(expected_docs, actual_docs)

    def test_get_top_level_aws_docs_if_no_command_specified(self):
        parsed_args = self.parser.parse('aws')
        actual_docs = self.docs_getter.get_docs(parsed_args)
        expected_docs = 'The AWS Command Line Interface'
        self.assertIn(expected_docs, actual_docs)

    def test_get_top_level_aws_docs_if_service_command_is_invalid(self):
        parsed_args = self.parser.parse('aws fake')
        actual_docs = self.docs_getter.get_docs(parsed_args)
        expected_docs = 'The AWS Command Line Interface'
        self.assertIn(expected_docs, actual_docs)

    def test_get_service_operation_docs_if_input_is_vanild(self):
        parsed_args = self.parser.parse('aws ec2 describe-instances fake')
        actual_docs = self.docs_getter.get_docs(parsed_args)
        expected_docs = 'Describes the specified instances'
        self.assertIn(expected_docs, actual_docs)

    def test_cache_is_used(self):
        parsed_args = self.parser.parse('aws ec2 describe-instances fake')
        self.docs_getter.get_doc_content = mock.Mock()
        self.docs_getter.get_docs(parsed_args)
        self.assertEqual(self.docs_getter.get_doc_content.call_count, 1)
        self.docs_getter.get_docs(parsed_args)
        self.assertEqual(self.docs_getter.get_doc_content.call_count, 1)