23659a27a901073eab33df972a3252c78d3881f5
[sdc/sdc-distribution-client.git] /
1 #  Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #  not use this file except in compliance with the License. You may obtain
3 #  a copy of the License at
4 #
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #  Unless required by applicable law or agreed to in writing, software
8 #  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #  License for the specific language governing permissions and limitations
11 #  under the License.
12
13 try:
14     from StringIO import StringIO
15 except:
16     from io import StringIO
17 import os
18 import sys
19
20 import mock
21
22 from cliff import app as application
23 from cliff import commandmanager
24 from cliff import help
25 from cliff.tests import utils
26
27
28 def test_show_help_for_command():
29     # FIXME(dhellmann): Are commands tied too closely to the app? Or
30     # do commands know too much about apps by using them to get to the
31     # command manager?
32     stdout = StringIO()
33     app = application.App('testing', '1',
34                           utils.TestCommandManager(utils.TEST_NAMESPACE),
35                           stdout=stdout)
36     app.NAME = 'test'
37     help_cmd = help.HelpCommand(app, mock.Mock())
38     parser = help_cmd.get_parser('test')
39     parsed_args = parser.parse_args(['one'])
40     try:
41         help_cmd.run(parsed_args)
42     except SystemExit:
43         pass
44     assert stdout.getvalue() == 'TestParser'
45
46
47 def test_list_matching_commands():
48     # FIXME(dhellmann): Are commands tied too closely to the app? Or
49     # do commands know too much about apps by using them to get to the
50     # command manager?
51     stdout = StringIO()
52     app = application.App('testing', '1',
53                           utils.TestCommandManager(utils.TEST_NAMESPACE),
54                           stdout=stdout)
55     app.NAME = 'test'
56     help_cmd = help.HelpCommand(app, mock.Mock())
57     parser = help_cmd.get_parser('test')
58     parsed_args = parser.parse_args(['t'])
59     try:
60         help_cmd.run(parsed_args)
61     except SystemExit:
62         pass
63     help_output = stdout.getvalue()
64     assert 'Command "t" matches:' in help_output
65     assert 'three word command\n  two words\n' in help_output
66
67
68 def test_list_matching_commands_no_match():
69     # FIXME(dhellmann): Are commands tied too closely to the app? Or
70     # do commands know too much about apps by using them to get to the
71     # command manager?
72     stdout = StringIO()
73     app = application.App('testing', '1',
74                           utils.TestCommandManager(utils.TEST_NAMESPACE),
75                           stdout=stdout)
76     app.NAME = 'test'
77     help_cmd = help.HelpCommand(app, mock.Mock())
78     parser = help_cmd.get_parser('test')
79     parsed_args = parser.parse_args(['z'])
80     try:
81         help_cmd.run(parsed_args)
82     except SystemExit:
83         pass
84     except ValueError:
85         pass
86     else:
87         assert False, 'Should have seen a ValueError'
88
89
90 def test_show_help_for_help():
91     # FIXME(dhellmann): Are commands tied too closely to the app? Or
92     # do commands know too much about apps by using them to get to the
93     # command manager?
94     stdout = StringIO()
95     app = application.App('testing', '1',
96                           utils.TestCommandManager(utils.TEST_NAMESPACE),
97                           stdout=stdout)
98     app.NAME = 'test'
99     app.options = mock.Mock()
100     help_cmd = help.HelpCommand(app, mock.Mock())
101     parser = help_cmd.get_parser('test')
102     parsed_args = parser.parse_args([])
103     try:
104         help_cmd.run(parsed_args)
105     except SystemExit:
106         pass
107     help_text = stdout.getvalue()
108     basecommand = os.path.split(sys.argv[0])[1]
109     assert 'usage: %s [--version]' % basecommand in help_text
110     assert 'optional arguments:\n  --version' in help_text
111     expected = (
112         '  one            Test command.\n'
113         '  three word command  Test command.\n'
114     )
115     assert expected in help_text
116
117
118 def test_list_deprecated_commands():
119     # FIXME(dhellmann): Are commands tied too closely to the app? Or
120     # do commands know too much about apps by using them to get to the
121     # command manager?
122     stdout = StringIO()
123     app = application.App('testing', '1',
124                           utils.TestCommandManager(utils.TEST_NAMESPACE),
125                           stdout=stdout)
126     app.NAME = 'test'
127     try:
128         app.run(['--help'])
129     except SystemExit:
130         pass
131     help_output = stdout.getvalue()
132     assert 'two words' in help_output
133     assert 'three word command' in help_output
134     assert 'old cmd' not in help_output
135
136
137 @mock.patch.object(commandmanager.EntryPointWrapper, 'load',
138                    side_effect=Exception('Could not load EntryPoint'))
139 def test_show_help_with_ep_load_fail(mock_load):
140     stdout = StringIO()
141     app = application.App('testing', '1',
142                           utils.TestCommandManager(utils.TEST_NAMESPACE),
143                           stdout=stdout)
144     app.NAME = 'test'
145     app.options = mock.Mock()
146     app.options.debug = False
147     help_cmd = help.HelpCommand(app, mock.Mock())
148     parser = help_cmd.get_parser('test')
149     parsed_args = parser.parse_args([])
150     try:
151         help_cmd.run(parsed_args)
152     except SystemExit:
153         pass
154     help_output = stdout.getvalue()
155     assert 'Commands:' in help_output
156     assert 'Could not load' in help_output
157     assert 'Exception: Could not load EntryPoint' not in help_output
158
159
160 @mock.patch.object(commandmanager.EntryPointWrapper, 'load',
161                    side_effect=Exception('Could not load EntryPoint'))
162 def test_show_help_print_exc_with_ep_load_fail(mock_load):
163     stdout = StringIO()
164     app = application.App('testing', '1',
165                           utils.TestCommandManager(utils.TEST_NAMESPACE),
166                           stdout=stdout)
167     app.NAME = 'test'
168     app.options = mock.Mock()
169     app.options.debug = True
170     help_cmd = help.HelpCommand(app, mock.Mock())
171     parser = help_cmd.get_parser('test')
172     parsed_args = parser.parse_args([])
173     try:
174         help_cmd.run(parsed_args)
175     except SystemExit:
176         pass
177     help_output = stdout.getvalue()
178     assert 'Commands:' in help_output
179     assert 'Could not load' in help_output
180     assert 'Exception: Could not load EntryPoint' in help_output