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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
14 from StringIO import StringIO
16 from io import StringIO
22 from cliff import app as application
23 from cliff import commandmanager
24 from cliff import help
25 from cliff.tests import utils
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
33 app = application.App('testing', '1',
34 utils.TestCommandManager(utils.TEST_NAMESPACE),
37 help_cmd = help.HelpCommand(app, mock.Mock())
38 parser = help_cmd.get_parser('test')
39 parsed_args = parser.parse_args(['one'])
41 help_cmd.run(parsed_args)
44 assert stdout.getvalue() == 'TestParser'
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
52 app = application.App('testing', '1',
53 utils.TestCommandManager(utils.TEST_NAMESPACE),
56 help_cmd = help.HelpCommand(app, mock.Mock())
57 parser = help_cmd.get_parser('test')
58 parsed_args = parser.parse_args(['t'])
60 help_cmd.run(parsed_args)
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
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
73 app = application.App('testing', '1',
74 utils.TestCommandManager(utils.TEST_NAMESPACE),
77 help_cmd = help.HelpCommand(app, mock.Mock())
78 parser = help_cmd.get_parser('test')
79 parsed_args = parser.parse_args(['z'])
81 help_cmd.run(parsed_args)
87 assert False, 'Should have seen a ValueError'
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
95 app = application.App('testing', '1',
96 utils.TestCommandManager(utils.TEST_NAMESPACE),
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([])
104 help_cmd.run(parsed_args)
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
112 ' one Test command.\n'
113 ' three word command Test command.\n'
115 assert expected in help_text
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
123 app = application.App('testing', '1',
124 utils.TestCommandManager(utils.TEST_NAMESPACE),
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
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):
141 app = application.App('testing', '1',
142 utils.TestCommandManager(utils.TEST_NAMESPACE),
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([])
151 help_cmd.run(parsed_args)
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
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):
164 app = application.App('testing', '1',
165 utils.TestCommandManager(utils.TEST_NAMESPACE),
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([])
174 help_cmd.run(parsed_args)
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