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
15 from cliff import commandmanager
16 from cliff.tests import utils
19 def test_lookup_and_find():
21 cmd, name, remaining = mgr.find_command(argv)
23 assert name == ' '.join(argv)
25 mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
26 for expected in [['one'],
28 ['three', 'word', 'command'],
30 yield check, mgr, expected
34 def test_lookup_with_remainder():
36 cmd, name, remaining = mgr.find_command(argv)
38 assert remaining == ['--opt']
39 mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
40 for expected in [['one', '--opt'],
41 ['two', 'words', '--opt'],
42 ['three', 'word', 'command', '--opt'],
44 yield check, mgr, expected
48 def test_find_invalid_command():
49 mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
53 mgr.find_command(argv)
54 except ValueError as err:
55 # make sure err include 'a' when ['a', '-b']
56 assert argv[0] in ('%s' % err)
57 assert '-b' in ('%s' % err)
59 assert False, 'expected a failure'
60 for argv in [['a', '-b'],
66 def test_find_unknown_command():
67 mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
69 mgr.find_command(['a', 'b'])
70 except ValueError as err:
71 assert "['a', 'b']" in ('%s' % err)
73 assert False, 'expected a failure'
76 def test_add_command():
77 mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
78 mock_cmd = mock.Mock()
79 mgr.add_command('mock', mock_cmd)
80 found_cmd, name, args = mgr.find_command(['mock'])
81 assert found_cmd is mock_cmd
84 def test_intersected_commands():
91 mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
92 mgr.add_command('foo', foo)
93 mgr.add_command('foo bar', foo_bar)
95 assert mgr.find_command(['foo', 'bar'])[0] is foo_bar
96 assert mgr.find_command(['foo', 'arg0'])[0] is foo
99 def test_load_commands():
100 testcmd = mock.Mock(name='testcmd')
101 testcmd.name.replace.return_value = 'test'
102 mock_pkg_resources = mock.Mock(return_value=[testcmd])
103 with mock.patch('pkg_resources.iter_entry_points',
104 mock_pkg_resources) as iter_entry_points:
105 mgr = commandmanager.CommandManager('test')
106 iter_entry_points.assert_called_once_with('test')
107 names = [n for n, v in mgr]
108 assert names == ['test']
111 def test_load_commands_keep_underscores():
112 testcmd = mock.Mock()
113 testcmd.name = 'test_cmd'
114 mock_pkg_resources = mock.Mock(return_value=[testcmd])
115 with mock.patch('pkg_resources.iter_entry_points',
116 mock_pkg_resources) as iter_entry_points:
117 mgr = commandmanager.CommandManager('test', convert_underscores=False)
118 iter_entry_points.assert_called_once_with('test')
119 names = [n for n, v in mgr]
120 assert names == ['test_cmd']
123 def test_load_commands_replace_underscores():
124 testcmd = mock.Mock()
125 testcmd.name = 'test_cmd'
126 mock_pkg_resources = mock.Mock(return_value=[testcmd])
127 with mock.patch('pkg_resources.iter_entry_points',
128 mock_pkg_resources) as iter_entry_points:
129 mgr = commandmanager.CommandManager('test', convert_underscores=True)
130 iter_entry_points.assert_called_once_with('test')
131 names = [n for n, v in mgr]
132 assert names == ['test cmd']