test.py 16.2 KB
Newer Older
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
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

#from mock import patch, call
from unittest import TestCase
from itertools import product

from kamaki.cli import command_tree


class Command(TestCase):

    def test___init__(self):
        for args in product(
                (None, '', 'cmd'),
                (None, '', 'Some help'),
                (None, '', {}, dict(cmd0a=None, cmd0b=None)),
                (None, command_tree.Command('cmd_cmd0'))):
            path, help, subcommands, cmd_class = args
            try:
                cmd = command_tree.Command(*args)
            except Exception as e:
                if path:
                    raise
                self.assertTrue(isinstance(e, AssertionError))
                continue
            self.assertEqual(cmd.help, help or '')
            self.assertEqual(cmd.subcommands, subcommands or {})
            self.assertEqual(cmd.cmd_class, cmd_class or None)

    def test_name(self):
        for path in ('cmd', 'cmd_cmd0', 'cmd_cmd0_cmd1', '', None):
            if path:
                cmd = command_tree.Command(path)
                self.assertEqual(cmd.name, path.split('_')[-1])
            else:
                try:
                    command_tree.Command(path)
                except Exception as e:
                    self.assertTrue(isinstance(e, AssertionError))

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    def test_add_subcmd(self):
        cmd = command_tree.Command('cmd')
        for subname in (None, 'cmd0', 'cmd_cmd0'):
            if subname:
                subcmd = command_tree.Command(subname)
                if subname.startswith(cmd.name + '_'):
                    self.assertTrue(cmd.add_subcmd(subcmd))
                    self.assertTrue(subcmd.name in cmd.subcommands)
                else:
                    self.assertFalse(cmd.add_subcmd(subcmd))
                    self.assertTrue(len(cmd.subcommands) == 0)
            else:
                self.assertRaises(cmd.add_subcmd, subname, AttributeError)

    def test_get_subcmd(self):
        cmd = command_tree.Command('cmd')
        cmd.subcommands = dict(
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
            cmd0b=command_tree.Command('cmd_cmd0b'))
        for subname in ('', None, 'cmd0a', 'cmd1', 'cmd0b'):
            try:
                expected = cmd.subcommands[subname] if subname else None
            except KeyError:
                expected = None
            self.assertEqual(cmd.get_subcmd(subname), expected)

    def test_contains(self):
        cmd = command_tree.Command('cmd')
        for subname in ('', 'cmd0'):
            self.assertFalse(cmd.contains(subname))
        cmd.subcommands = dict(
            cmd0a=command_tree.Command('cmd_cmd0a'),
            cmd0b=command_tree.Command('cmd_cmd0b'))
        for subname in ('cmd0a', 'cmd0b'):
            self.assertTrue(cmd.contains(subname))
        for subname in ('', 'cmd0c'):
            self.assertFalse(cmd.contains(subname))
        cmd.subcommands['cmd0a'].subcommands = dict(
            cmd1=command_tree.Command('cmd_cmd0a_cmd1'))
        for subname in ('cmd0a', 'cmd0b'):
            self.assertTrue(cmd.contains(subname))
        for subname in ('', 'cmd0c', 'cmd1', 'cmd0a_cmd1'):
            self.assertFalse(cmd.contains(subname))

    def test_is_command(self):
        cmd = command_tree.Command('cmd')
        cmd.subcommands = dict(
            itis=command_tree.Command('cmd_itis', cmd_class=Command),
            itsnot=command_tree.Command('cmd_itsnot'))
        self.assertFalse(cmd.is_command)
        self.assertTrue(cmd.subcommands['itis'].is_command)
        self.assertFalse(cmd.subcommands['itsnot'].is_command)

    def test_parent_path(self):
        cmd = command_tree.Command('cmd')
        cmd.subcommands = dict(
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
            cmd0b=command_tree.Command('cmd_cmd0b'))
        self.assertEqual(cmd.parent_path, '')
        self.assertEqual(cmd.subcommands['cmd0a'].parent_path, cmd.path)
        self.assertEqual(cmd.subcommands['cmd0b'].parent_path, cmd.path)
        cmd0a = cmd.subcommands['cmd0a']
        self.assertEqual(cmd0a.subcommands['cmd1'].parent_path, cmd0a.path)

    def test_parse_out(self):
        cmd = command_tree.Command('cmd')
        cmd.subcommands = dict(
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
                cmd1=command_tree.Command('cmd_cmd0a_cmd1'))),
            cmd0b=command_tree.Command('cmd_cmd0b'))
        for invalids in (None, 42, 0.88):
            self.assertRaises(TypeError, cmd.parse_out, invalids)
        for c, l, expc, expl in (
                (cmd, ['cmd'], cmd, ['cmd']),
                (cmd, ['XXX'], cmd, ['XXX']),
                (cmd, ['cmd0a'], cmd.subcommands['cmd0a'], []),
                (cmd, ['XXX', 'cmd0a'], cmd, ['XXX', 'cmd0a']),
                (cmd, ['cmd0a', 'XXX'], cmd.subcommands['cmd0a'], ['XXX']),
                (cmd, ['cmd0a', 'cmd0b'], cmd.subcommands['cmd0a'], ['cmd0b']),
                (cmd, ['cmd0b', 'XXX'], cmd.subcommands['cmd0b'], ['XXX']),
                (
                    cmd, ['cmd0a', 'cmd1'],
                    cmd.subcommands['cmd0a'].subcommands['cmd1'], []),
                (
                    cmd, ['cmd0a', 'cmd1', 'XXX'],
                    cmd.subcommands['cmd0a'].subcommands['cmd1'], ['XXX']),
                (
                    cmd, ['cmd0a', 'XXX', 'cmd1'],
                    cmd.subcommands['cmd0a'], ['XXX', 'cmd1'])):
            self.assertEqual((expc, expl), c.parse_out(l))

165

166
167
class CommandTree(TestCase):

Stavros Sachtouris's avatar
Stavros Sachtouris committed
168
169
170
171
172
173
174
175
176
177
    def _add_commands(self, ctree):
        for cmd in self.commands:
            ctree.add_command(cmd.path, cmd.help, cmd.cmd_class)

    def _commands_are_equal(self, c1, c2):
        self.assertEqual(c1.path, c2.path)
        self.assertEqual(c1.name, c2.name)
        self.assertEqual(c1.cmd_class, c2.cmd_class)
        self.assertEqual(c1.help, c2.help)

178
179
180
181
182
    def setUp(self):
        cmd = command_tree.Command('cmd', subcommands=dict(
            cmd0a=command_tree.Command('cmd_cmd0a', subcommands=dict(
                cmd1a=command_tree.Command(
                    'cmd_cmd0a_cmd1a', subcommands=dict(
Stavros Sachtouris's avatar
Stavros Sachtouris committed
183
184
185
                        cmd2=command_tree.Command(
                            'cmd_cmd0a_cmd1a_cmd2', cmd_class=Command)
                        ),
186
187
188
                ),
                cmd1b=command_tree.Command(
                    'cmd_cmd0a_cmd1b', subcommands=dict(
Stavros Sachtouris's avatar
Stavros Sachtouris committed
189
190
191
                        cmd2=command_tree.Command(
                            'cmd_cmd0a_cmd1b_cmd2', cmd_class=Command)
                        ),
192
193
194
195
196
197
198
                )
            )),
            cmd0b=command_tree.Command('cmd_cmd0b'),
            cmd0c=command_tree.Command('cmd_cmd0c', subcommands=dict(
                cmd1a=command_tree.Command('cmd_cmd0c_cmd1a'),
                cmd1b=command_tree.Command(
                    'cmd_cmd0c_cmd1b', subcommands=dict(
Stavros Sachtouris's avatar
Stavros Sachtouris committed
199
200
201
                        cmd2=command_tree.Command(
                            'cmd_cmd0c_cmd1b_cmd2', cmd_class=Command)
                        ),
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                )
            ))
        ))
        self.commands = [
            cmd,
            cmd.subcommands['cmd0a'],
            cmd.subcommands['cmd0a'].subcommands['cmd1a'],
            cmd.subcommands['cmd0a'].subcommands['cmd1a'].subcommands['cmd2'],
            cmd.subcommands['cmd0a'].subcommands['cmd1b'],
            cmd.subcommands['cmd0a'].subcommands['cmd1b'].subcommands['cmd2'],
            cmd.subcommands['cmd0b'],
            cmd.subcommands['cmd0c'],
            cmd.subcommands['cmd0c'].subcommands['cmd1a'],
            cmd.subcommands['cmd0c'].subcommands['cmd1b'],
            cmd.subcommands['cmd0c'].subcommands['cmd1b'].subcommands['cmd2'],
Stavros Sachtouris's avatar
Stavros Sachtouris committed
217
            command_tree.Command('othercmd')
218
219
220
221
222
223
224
225
        ]

    def tearDown(self):
        for cmd in self.commands:
            del cmd
        del self.commands

    def test___init__(self):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        name, description = 'sampleTree', 'a sample Tree'
        ctree = command_tree.CommandTree(name)
        for attr, exp in (
                ('groups', {}), ('_all_commands', {}),
                ('name', name), ('description', '')):
            self.assertEqual(getattr(ctree, attr), exp)
        ctree = command_tree.CommandTree(name, description)
        for attr, exp in (
                ('groups', {}), ('_all_commands', {}),
                ('name', name), ('description', description)):
            self.assertEqual(getattr(ctree, attr), exp)

    def test_exclude(self):
        ctree = command_tree.CommandTree('excludeTree', 'test exclude group')
        exp = dict()
        for cmd in self.commands[0:6]:
            ctree.groups[cmd.name] = cmd
            exp[cmd.name] = cmd
        self.assertEqual(exp, ctree.groups)
        ctree.exclude(exp.keys()[1::2])
        for key in exp.keys()[1::2]:
            exp.pop(key)
        self.assertEqual(exp, ctree.groups)

    def test_add_command(self):
        ctree = command_tree.CommandTree('addCommand', 'test add_command')
        self._add_commands(ctree)
        for cmd in self.commands:
            self.assertTrue(cmd, ctree._all_commands)
            if cmd.path.count('_'):
                self.assertFalse(cmd.name in ctree.groups)
            else:
                self.assertTrue(cmd.name in ctree.groups)
                self._commands_are_equal(cmd, ctree.groups[cmd.name])

    def test_find_best_match(self):
        ctree = command_tree.CommandTree('bestMatch', 'test find_best_match')
        for cmd in self.commands:
            terms = cmd.path.split('_')
            best_match, rest = ctree.find_best_match(terms)
            if len(terms) > 1:
                self.assertEqual(best_match.path, '_'.join(terms[:-1]))
            else:
                self.assertEqual(best_match, None)
            self.assertEqual(rest, terms[-1:])
            ctree.add_command(cmd.path, cmd.help, cmd.cmd_class)
            best_match, rest = ctree.find_best_match(terms)
            self._commands_are_equal(best_match, cmd)
            self.assertEqual(rest, [])

    def test_add_tree(self):
        ctree = command_tree.CommandTree('tree', 'the main tree')
        ctree1 = command_tree.CommandTree('tree1', 'the first tree')
        ctree2 = command_tree.CommandTree('tree2', 'the second tree')

        cmds = list(self.commands)
        del self.commands
        cmds1, cmds2 = cmds[:6], cmds[6:]
        self.commands = cmds1
        self._add_commands(ctree1)
        self.commands = cmds2
        self._add_commands(ctree2)
        self.commands = cmds

        def check_all(
                p1=False, p2=False, p3=False, p4=False, p5=False, p6=False):
            for cmd in cmds[:6]:
                self.assertEquals(cmd.path in ctree._all_commands, p1)
                self.assertEquals(cmd.path in ctree1._all_commands, p2)
                if cmd.path != 'cmd':
                    self.assertEquals(cmd.path in ctree2._all_commands, p3)
            for cmd in cmds[6:]:
                self.assertEquals(cmd.path in ctree._all_commands, p4)
                if cmd.path != 'cmd':
                    self.assertEquals(cmd.path in ctree1._all_commands, p5)
                self.assertEquals(cmd.path in ctree2._all_commands, p6)

        check_all(False, True, False, False, False, True)
        ctree.add_tree(ctree1)
        check_all(True, True, False, False, False, True)
        ctree.add_tree(ctree2)
        check_all(True, True, False, True, False, True)
        ctree2.add_tree(ctree1)
        check_all(True, True, True, True, False, True)

    def test_has_command(self):
        ctree = command_tree.CommandTree('treeHasCommand', 'test has_command')
        for cmd in self.commands:
            self.assertFalse(ctree.has_command(cmd.path))
        self._add_commands(ctree)
        for cmd in self.commands:
            self.assertTrue(ctree.has_command(cmd.path))
        self.assertFalse(ctree.has_command('NON_EXISTING_COMMAND'))

    def test_get_command(self):
        ctree = command_tree.CommandTree('treeGetCommand', 'test get_command')
        for cmd in self.commands:
            self.assertRaises(KeyError, ctree.get_command, cmd.path)
        self._add_commands(ctree)
        for cmd in self.commands:
            self._commands_are_equal(ctree.get_command(cmd.path), cmd)
        self.assertRaises(KeyError, ctree.get_command, 'NON_EXISTNG_COMMAND')

    def test_subnames(self):
        ctree = command_tree.CommandTree('treeSubnames', 'test subnames')
        self.assertEqual(ctree.subnames(), [])
        self.assertRaises(KeyError, ctree.subnames, 'cmd')
        self._add_commands(ctree)
        for l1, l2 in (
                (ctree.subnames(), ['cmd', 'othercmd']),
                (ctree.subnames('cmd'), ['cmd0a', 'cmd0b', 'cmd0c']),
                (ctree.subnames('cmd_cmd0a'), ['cmd1a', 'cmd1b']),
                (ctree.subnames('cmd_cmd0a_cmd1a'), ['cmd2', ]),
                (ctree.subnames('cmd_cmd0a_cmd1b'), ['cmd2', ]),
                (ctree.subnames('cmd_cmd0a_cmd1a_cmd2'), []),
                (ctree.subnames('cmd_cmd0a_cmd1b_cmd2'), []),
                (ctree.subnames('cmd_cmd0b'), []),
                (ctree.subnames('cmd_cmd0c'), ['cmd1a', 'cmd1b']),
                (ctree.subnames('cmd_cmd0c_cmd1a'), []),
                (ctree.subnames('cmd_cmd0c_cmd1b'), ['cmd2', ]),
                (ctree.subnames('cmd_cmd0c_cmd1b_cmd2'), []),
                (ctree.subnames('othercmd'), [])):
            l1.sort(), l2.sort(), self.assertEqual(l1, l2)
        self.assertRaises(KeyError, ctree.subnames, 'NON_EXISTNG_CMD')

    def test_get_subcommands(self):
        ctree = command_tree.CommandTree('treeSub', 'test get_subcommands')
        self.assertEqual(ctree.get_subcommands(), [])
        self.assertRaises(KeyError, ctree.get_subcommands, 'cmd')
        self._add_commands(ctree)
        for s1, l2 in (
            ('', ['cmd', 'othercmd']),
            ('cmd', ['cmd0a', 'cmd0b', 'cmd0c']),
            ('cmd_cmd0a', ['cmd1a', 'cmd1b']),
            ('cmd_cmd0a_cmd1a', ['cmd2', ]),
            ('cmd_cmd0a_cmd1b', ['cmd2', ]),
            ('cmd_cmd0a_cmd1a_cmd2', []),
            ('cmd_cmd0a_cmd1b_cmd2', []),
            ('cmd_cmd0b', []),
            ('cmd_cmd0c', ['cmd1a', 'cmd1b']),
            ('cmd_cmd0c_cmd1a', []),
            ('cmd_cmd0c_cmd1b', ['cmd2', ]),
            ('cmd_cmd0c_cmd1b_cmd2', []),
            ('othercmd', [])
        ):
            l1 = [cmd.path for cmd in ctree.get_subcommands(s1)]
            l2 = ['_'.join([s1, i]) for i in l2] if s1 else l2
            l1.sort(), l2.sort(), self.assertEqual(l1, l2)
        self.assertRaises(KeyError, ctree.get_subcommands, 'NON_EXISTNG_CMD')
375
376


377
378
379
380
if __name__ == '__main__':
    from sys import argv
    from kamaki.cli.test import runTestCase
    runTestCase(Command, 'Command', argv[1:])
381
    runTestCase(CommandTree, 'CommandTree', argv[1:])