File: test_commands.py

package info (click to toggle)
mopidy 3.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,608 kB
  • sloc: python: 16,656; sh: 159; makefile: 127
file content (525 lines) | stat: -rw-r--r-- 16,828 bytes parent folder | download | duplicates (3)
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import argparse
import unittest
from unittest import mock

from mopidy import commands


class ConfigOverrideTypeTest(unittest.TestCase):
    def test_valid_override(self):
        expected = ("section", "key", "value")
        assert expected == commands.config_override_type("section/key=value")
        assert expected == commands.config_override_type("section/key=value ")
        assert expected == commands.config_override_type("section/key =value")
        assert expected == commands.config_override_type("section /key=value")

    def test_empty_override(self):
        expected = ("section", "key", "")
        assert expected == commands.config_override_type("section/key=")
        assert expected == commands.config_override_type("section/key=  ")

    def test_invalid_override(self):
        with self.assertRaises(argparse.ArgumentTypeError):
            commands.config_override_type("section/key")
        with self.assertRaises(argparse.ArgumentTypeError):
            commands.config_override_type("section=")
        with self.assertRaises(argparse.ArgumentTypeError):
            commands.config_override_type("section")


class CommandParsingTest(unittest.TestCase):
    def setUp(self):  # noqa: N802
        self.exit_patcher = mock.patch.object(commands.Command, "exit")
        self.exit_mock = self.exit_patcher.start()
        self.exit_mock.side_effect = SystemExit

    def tearDown(self):  # noqa: N802
        self.exit_patcher.stop()

    def test_command_parsing_returns_namespace(self):
        cmd = commands.Command()
        assert isinstance(cmd.parse([]), argparse.Namespace)

    def test_command_parsing_does_not_contain_args(self):
        cmd = commands.Command()
        result = cmd.parse([])
        assert not hasattr(result, "_args")

    def test_unknown_options_bails(self):
        cmd = commands.Command()
        with self.assertRaises(SystemExit):
            cmd.parse(["--foobar"])

    def test_invalid_sub_command_bails(self):
        cmd = commands.Command()
        with self.assertRaises(SystemExit):
            cmd.parse(["foo"])

    def test_command_arguments(self):
        cmd = commands.Command()
        cmd.add_argument("--bar")

        result = cmd.parse(["--bar", "baz"])
        assert result.bar == "baz"

    def test_command_arguments_and_sub_command(self):
        child = commands.Command()
        child.add_argument("--baz")

        cmd = commands.Command()
        cmd.add_argument("--bar")
        cmd.add_child("foo", child)

        result = cmd.parse(["--bar", "baz", "foo"])
        assert result.bar == "baz"
        assert result.baz is None

    def test_subcommand_may_have_positional(self):
        child = commands.Command()
        child.add_argument("bar")

        cmd = commands.Command()
        cmd.add_child("foo", child)

        result = cmd.parse(["foo", "baz"])
        assert result.bar == "baz"

    def test_subcommand_may_have_remainder(self):
        child = commands.Command()
        child.add_argument("bar", nargs=argparse.REMAINDER)

        cmd = commands.Command()
        cmd.add_child("foo", child)

        result = cmd.parse(["foo", "baz", "bep", "bop"])
        assert result.bar == ["baz", "bep", "bop"]

    def test_result_stores_choosen_command(self):
        child = commands.Command()

        cmd = commands.Command()
        cmd.add_child("foo", child)

        result = cmd.parse(["foo"])
        assert result.command == child

        result = cmd.parse([])
        assert result.command == cmd

        child2 = commands.Command()
        cmd.add_child("bar", child2)

        subchild = commands.Command()
        child.add_child("baz", subchild)

        result = cmd.parse(["bar"])
        assert result.command == child2

        result = cmd.parse(["foo", "baz"])
        assert result.command == subchild

    def test_invalid_type(self):
        cmd = commands.Command()
        cmd.add_argument("--bar", type=int)

        with self.assertRaises(SystemExit):
            cmd.parse(["--bar", "zero"], prog="foo")

        self.exit_mock.assert_called_once_with(
            1,
            "argument --bar: invalid int value: 'zero'",
            "usage: foo [--bar BAR]",
        )

    @mock.patch("sys.argv")
    def test_command_error_usage_prog(self, argv_mock):
        argv_mock.__getitem__.return_value = "/usr/bin/foo"

        cmd = commands.Command()
        cmd.add_argument("--bar", required=True)

        with self.assertRaises(SystemExit):
            cmd.parse([])
        self.exit_mock.assert_called_once_with(
            mock.ANY, mock.ANY, "usage: foo --bar BAR"
        )

        self.exit_mock.reset_mock()
        with self.assertRaises(SystemExit):
            cmd.parse([], prog="baz")

        self.exit_mock.assert_called_once_with(
            mock.ANY, mock.ANY, "usage: baz --bar BAR"
        )

    def test_missing_required(self):
        cmd = commands.Command()
        cmd.add_argument("--bar", required=True)

        with self.assertRaises(SystemExit):
            cmd.parse([], prog="foo")

        self.exit_mock.assert_called_once_with(
            1,
            "the following arguments are required: --bar",
            "usage: foo --bar BAR",
        )

    def test_missing_positionals(self):
        cmd = commands.Command()
        cmd.add_argument("bar")

        with self.assertRaises(SystemExit):
            cmd.parse([], prog="foo")

        self.exit_mock.assert_called_once_with(
            1,
            "the following arguments are required: bar, _args",
            "usage: foo bar",
        )

    def test_missing_positionals_subcommand(self):
        child = commands.Command()
        child.add_argument("baz")

        cmd = commands.Command()
        cmd.add_child("bar", child)

        with self.assertRaises(SystemExit):
            cmd.parse(["bar"], prog="foo")

        self.exit_mock.assert_called_once_with(
            1,
            "the following arguments are required: baz, _args",
            "usage: foo bar baz",
        )

    def test_unknown_command(self):
        cmd = commands.Command()

        with self.assertRaises(SystemExit):
            cmd.parse(["--help"], prog="foo")

        self.exit_mock.assert_called_once_with(
            1, "unrecognized arguments: --help", "usage: foo"
        )

    def test_invalid_subcommand(self):
        cmd = commands.Command()
        cmd.add_child("baz", commands.Command())

        with self.assertRaises(SystemExit):
            cmd.parse(["bar"], prog="foo")

        self.exit_mock.assert_called_once_with(
            1, "unrecognized command: bar", "usage: foo"
        )

    def test_set(self):
        cmd = commands.Command()
        cmd.set(foo="bar")

        result = cmd.parse([])
        assert result.foo == "bar"

    def test_set_propegate(self):
        child = commands.Command()

        cmd = commands.Command()
        cmd.set(foo="bar")
        cmd.add_child("command", child)

        result = cmd.parse(["command"])
        assert result.foo == "bar"

    def test_innermost_set_wins(self):
        child = commands.Command()
        child.set(foo="bar", baz=1)

        cmd = commands.Command()
        cmd.set(foo="baz", baz=None)
        cmd.add_child("command", child)

        result = cmd.parse(["command"])
        assert result.foo == "bar"
        assert result.baz == 1

    def test_help_action_works(self):
        cmd = commands.Command()
        cmd.add_argument("-h", action="help")
        cmd.format_help = mock.Mock()

        with self.assertRaises(SystemExit):
            cmd.parse(["-h"])

        cmd.format_help.assert_called_once_with(mock.ANY)
        self.exit_mock.assert_called_once_with(0, cmd.format_help.return_value)


class UsageTest(unittest.TestCase):
    @mock.patch("sys.argv")
    def test_prog_name_default_and_override(self, argv_mock):
        argv_mock.__getitem__.return_value = "/usr/bin/foo"
        cmd = commands.Command()
        assert "usage: foo" == cmd.format_usage().strip()
        assert "usage: baz" == cmd.format_usage("baz").strip()

    def test_basic_usage(self):
        cmd = commands.Command()
        assert "usage: foo" == cmd.format_usage("foo").strip()

        cmd.add_argument("-h", "--help", action="store_true")
        assert "usage: foo [-h]" == cmd.format_usage("foo").strip()

        cmd.add_argument("bar")
        assert "usage: foo [-h] bar" == cmd.format_usage("foo").strip()

    def test_nested_usage(self):
        child = commands.Command()
        cmd = commands.Command()
        cmd.add_child("bar", child)

        assert "usage: foo" == cmd.format_usage("foo").strip()
        assert "usage: foo bar" == cmd.format_usage("foo bar").strip()

        cmd.add_argument("-h", "--help", action="store_true")
        assert "usage: foo bar" == child.format_usage("foo bar").strip()

        child.add_argument("-h", "--help", action="store_true")
        assert "usage: foo bar [-h]" == child.format_usage("foo bar").strip()


class HelpTest(unittest.TestCase):
    @mock.patch("sys.argv")
    def test_prog_name_default_and_override(self, argv_mock):
        argv_mock.__getitem__.return_value = "/usr/bin/foo"
        cmd = commands.Command()
        assert "usage: foo" == cmd.format_help().strip()
        assert "usage: bar" == cmd.format_help("bar").strip()

    def test_command_without_documenation_or_options(self):
        cmd = commands.Command()
        assert "usage: bar" == cmd.format_help("bar").strip()

    def test_command_with_option(self):
        cmd = commands.Command()
        cmd.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )

        expected = (
            "usage: foo [-h]\n\n"
            "OPTIONS:\n\n"
            "  -h, --help  show this message"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_command_with_option_and_positional(self):
        cmd = commands.Command()
        cmd.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )
        cmd.add_argument("bar", help="some help text")

        expected = (
            "usage: foo [-h] bar\n\n"
            "OPTIONS:\n\n"
            "  -h, --help  show this message\n"
            "  bar         some help text"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_command_with_documentation(self):
        cmd = commands.Command()
        cmd.help = "some text about everything this command does."

        expected = (
            "usage: foo\n\n" "some text about everything this command does."
        )
        assert expected == cmd.format_help("foo").strip()

    def test_command_with_documentation_and_option(self):
        cmd = commands.Command()
        cmd.help = "some text about everything this command does."
        cmd.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )

        expected = (
            "usage: foo [-h]\n\n"
            "some text about everything this command does.\n\n"
            "OPTIONS:\n\n"
            "  -h, --help  show this message"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_subcommand_without_documentation_or_options(self):
        child = commands.Command()
        cmd = commands.Command()
        cmd.add_child("bar", child)

        assert "usage: foo" == cmd.format_help("foo").strip()

    def test_subcommand_with_documentation_shown(self):
        child = commands.Command()
        child.help = "some text about everything this command does."

        cmd = commands.Command()
        cmd.add_child("bar", child)
        expected = (
            "usage: foo\n\n"
            "COMMANDS:\n\n"
            "bar\n\n"
            "  some text about everything this command does."
        )
        assert expected == cmd.format_help("foo").strip()

    def test_subcommand_with_options_shown(self):
        child = commands.Command()
        child.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = (
            "usage: foo\n\n"
            "COMMANDS:\n\n"
            "bar [-h]\n\n"
            "    -h, --help  show this message"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_subcommand_with_positional_shown(self):
        child = commands.Command()
        child.add_argument("baz", help="the great and wonderful")

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = (
            "usage: foo\n\n"
            "COMMANDS:\n\n"
            "bar baz\n\n"
            "    baz  the great and wonderful"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_subcommand_with_options_and_documentation(self):
        child = commands.Command()
        child.help = "  some text about everything this command does."
        child.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = (
            "usage: foo\n\n"
            "COMMANDS:\n\n"
            "bar [-h]\n\n"
            "  some text about everything this command does.\n\n"
            "    -h, --help  show this message"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_nested_subcommands_with_options(self):
        subchild = commands.Command()
        subchild.add_argument("--test", help="the great and wonderful")

        child = commands.Command()
        child.add_child("baz", subchild)
        child.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = (
            "usage: foo\n\n"
            "COMMANDS:\n\n"
            "bar [-h]\n\n"
            "    -h, --help  show this message\n\n"
            "bar baz [--test TEST]\n\n"
            "    --test TEST  the great and wonderful"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_nested_subcommands_skipped_intermediate(self):
        subchild = commands.Command()
        subchild.add_argument("--test", help="the great and wonderful")

        child = commands.Command()
        child.add_child("baz", subchild)

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = (
            "usage: foo\n\n"
            "COMMANDS:\n\n"
            "bar baz [--test TEST]\n\n"
            "    --test TEST  the great and wonderful"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_command_with_option_and_subcommand_with_option(self):
        child = commands.Command()
        child.add_argument("--test", help="the great and wonderful")

        cmd = commands.Command()
        cmd.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )
        cmd.add_child("bar", child)

        expected = (
            "usage: foo [-h]\n\n"
            "OPTIONS:\n\n"
            "  -h, --help  show this message\n\n"
            "COMMANDS:\n\n"
            "bar [--test TEST]\n\n"
            "    --test TEST  the great and wonderful"
        )
        assert expected == cmd.format_help("foo").strip()

    def test_command_with_options_doc_and_subcommand_with_option_and_doc(self):
        child = commands.Command()
        child.help = "some text about this sub-command."
        child.add_argument("--test", help="the great and wonderful")

        cmd = commands.Command()
        cmd.help = "some text about everything this command does."
        cmd.add_argument(
            "-h", "--help", action="store_true", help="show this message"
        )
        cmd.add_child("bar", child)

        expected = (
            "usage: foo [-h]\n\n"
            "some text about everything this command does.\n\n"
            "OPTIONS:\n\n"
            "  -h, --help  show this message\n\n"
            "COMMANDS:\n\n"
            "bar [--test TEST]\n\n"
            "  some text about this sub-command.\n\n"
            "    --test TEST  the great and wonderful"
        )
        assert expected == cmd.format_help("foo").strip()


class RunTest(unittest.TestCase):
    def test_default_implmentation_raises_error(self):
        with self.assertRaises(NotImplementedError):
            commands.Command().run()


class RootCommandTest(unittest.TestCase):
    def test_config_overrides(self):
        cmd = commands.RootCommand()
        result = cmd.parse(["--option", "foo/bar=baz"])

        assert result.config_overrides[0] == ("foo", "bar", "baz")