7f5ce0c9bf9e9a1349968b3241c363205286ea1a
[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 import mock
14
15 from cliff import commandmanager
16 from cliff.tests import utils
17
18
19 def test_lookup_and_find():
20     def check(mgr, argv):
21         cmd, name, remaining = mgr.find_command(argv)
22         assert cmd
23         assert name == ' '.join(argv)
24         assert not remaining
25     mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
26     for expected in [['one'],
27                      ['two', 'words'],
28                      ['three', 'word', 'command'],
29                      ]:
30         yield check, mgr, expected
31     return
32
33
34 def test_lookup_with_remainder():
35     def check(mgr, argv):
36         cmd, name, remaining = mgr.find_command(argv)
37         assert cmd
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'],
43                      ]:
44         yield check, mgr, expected
45     return
46
47
48 def test_find_invalid_command():
49     mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
50
51     def check_one(argv):
52         try:
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)
58         else:
59             assert False, 'expected a failure'
60     for argv in [['a', '-b'],
61                  ['-b'],
62                  ]:
63         yield check_one, argv
64
65
66 def test_find_unknown_command():
67     mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
68     try:
69         mgr.find_command(['a', 'b'])
70     except ValueError as err:
71         assert "['a', 'b']" in ('%s' % err)
72     else:
73         assert False, 'expected a failure'
74
75
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
82
83
84 def test_intersected_commands():
85     def foo(arg):
86         pass
87
88     def foo_bar():
89         pass
90
91     mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
92     mgr.add_command('foo', foo)
93     mgr.add_command('foo bar', foo_bar)
94
95     assert mgr.find_command(['foo', 'bar'])[0] is foo_bar
96     assert mgr.find_command(['foo', 'arg0'])[0] is foo
97
98
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']
109
110
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']
121
122
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']