Source

argh / tests.py

The branch '18-version-attribute' does not exist.
Full commit
  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
# -*- coding: utf-8 -*-

import sys
from argh.six import (
    BytesIO, u, b, string_types, text_type, binary_type, iteritems
)
import unittest2 as unittest
import argparse
import argh.helpers
from argh import (
    alias, ArghParser, arg, command, CommandError, dispatch_command,
    dispatch_commands, plain_signature, wrap_errors
)
from argh import completion


class DebugArghParser(ArghParser):
    "(does not print stuff to stderr on exit)"

    def exit(self, status=0, message=None):
        raise SystemExit(message)

    def error(self, message):
        self.exit(2, message)


@arg('text')
@arg('--twice', default=False, help='repeat twice')
def echo(args):
    repeat = 2 if args.twice else 1
    return (u('you said {0}').format(args.text)) * repeat

@arg('text')
@plain_signature
def plain_echo(text):
    return u('you said {0}').format(text)

@arg('--name', default='world')
def hello(args):
    return u('Hello {0}!').format(args.name or 'world')

@arg('buddy')
def howdy(args):
    return u('Howdy {0}?').format(args.buddy)

@alias('aliased')
def do_aliased(args):
    return 'ok'

@arg('foo')
@arg('bar')
def foo_bar(args):
    return args.foo, args.bar

def custom_namespace(args):
    return args.custom_value

def whiner_plain(args):
    raise CommandError('I feel depressed.')

def whiner_iterable(args):
    yield 'Hello...'
    raise CommandError('I feel depressed.')

@arg('text')
def strict_hello(args):
    assert args.text == 'world', 'Do it yourself'  # bad manners :-(
    yield 'Hello {0}'.format(args.text)

@arg('text')
@wrap_errors(AssertionError)
def strict_hello_smart(args):
    assert args.text == 'world', 'Do it yourself'  # bad manners :-(
    yield 'Hello {0}'.format(args.text)

@command
def command_deco(text='Hello'):
    yield text

@command
def command_deco_issue12(foo=1, fox=2):
    yield u('foo {0}, fox {1}').format(foo, fox)


class BaseArghTestCase(unittest.TestCase):
    commands = {}

    def setUp(self):
        self.parser = DebugArghParser('PROG')
        for namespace, commands in iteritems(self.commands):
            self.parser.add_commands(commands, namespace=namespace)

    def _call_cmd(self, command_string, **kwargs):
        if isinstance(command_string, string_types):
            args = command_string.split()
        else:
            args = command_string

        io = BytesIO()
        if 'output_file' not in kwargs:
            kwargs['output_file'] = io

        result = self.parser.dispatch(args, **kwargs)

        if kwargs.get('output_file') is None:
            return result
        else:
            io.seek(0)
            return io.read()

    def assert_cmd_returns(self, command_string, expected_result, **kwargs):
        """Executes given command using given parser and asserts that it prints
        given value.
        """
        try:
            result = self._call_cmd(command_string, **kwargs)
        except SystemExit as error:
            self.fail('Argument parsing failed for {0!r}: {1!r}'.format(
                command_string, error))
        self.assertEqual(result, expected_result)

    def assert_cmd_exits(self, command_string, message_regex=None):
        "When a command forces exit, it *may* fail, but may just print help."
        message_regex = text_type(message_regex)  # make sure None -> "None"
        f = lambda: self.parser.dispatch(command_string.split())
        self.assertRaisesRegexp(SystemExit, message_regex, f)

    def assert_cmd_fails(self, command_string, message_regex):
        "exists with a message = fails"
        self.assert_cmd_exits(command_string, message_regex)

    def assert_cmd_doesnt_fail(self, command_string):
        """(for cases when a commands doesn't fail but also (maybe) doesn't
        return results and just prints them.)
        """
        self.assert_cmd_exits(command_string)


class ArghTestCase(BaseArghTestCase):
    commands = {
        None: [echo, plain_echo, foo_bar, do_aliased,
               whiner_plain, whiner_iterable, custom_namespace],
        'greet': [hello, howdy]
    }

    def test_argv(self):
        _argv = sys.argv
        sys.argv = sys.argv[:1] + ['echo', 'hi there']
        self.assert_cmd_returns(None, b('you said hi there\n'))
        sys.argv = _argv

    def test_no_command(self):
        self.assert_cmd_fails('', 'too few arguments')

    def test_invalid_choice(self):
        self.assert_cmd_fails('whatchamacallit', '^invalid choice')

    def test_echo(self):
        "A simple command is resolved to a function."
        self.assert_cmd_returns('echo foo', b('you said foo\n'))

    def test_bool_action(self):
        "Action `store_true`/`store_false` is inferred from default value."
        self.assert_cmd_returns('echo --twice foo', b('you said fooyou said foo\n'))

    def test_plain_signature(self):
        "Arguments can be passed to the function without a Namespace instance."
        self.assert_cmd_returns('plain-echo bar', b('you said bar\n'))

    def test_bare_namespace(self):
        "A command can be resolved to a function, not a namespace."
        self.assert_cmd_fails('greet', 'too few arguments')
        self.assert_cmd_fails('greet --name=world', 'too few arguments')

    def test_namespaced_function(self):
        "A subcommand is resolved to a function."
        self.assert_cmd_returns('greet hello', b('Hello world!\n'))
        self.assert_cmd_returns('greet hello --name=John', b('Hello John!\n'))
        self.assert_cmd_fails('greet hello John', 'unrecognized arguments')
        self.assert_cmd_fails('greet howdy --name=John', 'too few arguments')
        self.assert_cmd_returns('greet howdy John', b('Howdy John?\n'))

    def test_alias(self):
        self.assert_cmd_returns('aliased', b('ok\n'))

    def test_help_alias(self):
        self.assert_cmd_doesnt_fail('--help')
        self.assert_cmd_doesnt_fail('greet --help')
        self.assert_cmd_doesnt_fail('greet hello --help')

        self.assert_cmd_doesnt_fail('help')
        self.assert_cmd_doesnt_fail('help greet')
        self.assert_cmd_doesnt_fail('help greet hello')

    def test_arg_order(self):
        """Positional arguments are resolved in the order in which the @arg
        decorators are defined.
        """
        self.assert_cmd_returns('foo-bar foo bar', b('foo\nbar\n'))

    def test_raw_output(self):
        "If the raw_output flag is set, no extra whitespace is added"
        self.assert_cmd_returns('foo-bar foo bar', b('foo\nbar\n'))
        self.assert_cmd_returns('foo-bar foo bar', b('foobar'), raw_output=True)

    def test_output_file(self):
        self.assert_cmd_returns('greet hello', b('Hello world!\n'))
        self.assert_cmd_returns('greet hello', b('Hello world!\n'), output_file=None)

    def test_command_error(self):
        self.assert_cmd_returns('whiner-plain', b('I feel depressed.\n'))
        self.assert_cmd_returns('whiner-iterable', b('Hello...\nI feel depressed.\n'))

    def test_custom_namespace(self):
        namespace = argparse.Namespace()
        namespace.custom_value = 'foo'
        self.assert_cmd_returns('custom-namespace', b('foo\n'),
                                namespace=namespace)


class CommandDecoratorTests(BaseArghTestCase):
    commands = {None: [command_deco, command_deco_issue12]}

    def test_command_decorator(self):
        """The @command decorator creates arguments from function signature.
        """
        self.assert_cmd_returns('command-deco', b('Hello\n'))
        self.assert_cmd_returns('command-deco --text=hi', b('hi\n'))

    def test_regression_issue12(self):
        """Issue #12: @command was broken if there were more than one argument
        to begin with same character (i.e. short option names were inferred
        incorrectly).
        """
        self.assert_cmd_returns('command-deco-issue12', b('foo 1, fox 2\n'))
        self.assert_cmd_returns('command-deco-issue12 --foo 3', b('foo 3, fox 2\n'))
        self.assert_cmd_returns('command-deco-issue12 --fox 3', b('foo 1, fox 3\n'))
        self.assert_cmd_fails('command-deco-issue12 -f 3', 'unrecognized')


class ErrorWrappingTestCase(BaseArghTestCase):
    commands = {None: [strict_hello, strict_hello_smart]}
    def test_error_raised(self):
        f = lambda: self.parser.dispatch(['strict-hello', 'John'])
        self.assertRaisesRegexp(AssertionError, 'Do it yourself', f)

    def test_error_wrapped(self):
        self.assert_cmd_returns('strict-hello-smart John', b('Do it yourself\n'))
        self.assert_cmd_returns('strict-hello-smart world', b('Hello world\n'))


class NoCommandsTestCase(BaseArghTestCase):
    "Edge case: no commands defined"
    commands = {}
    def test_no_command(self):
        self.assert_cmd_returns('', b(self.parser.format_usage()), raw_output=True)
        self.assert_cmd_returns('', b(self.parser.format_usage()+'\n'))


class DefaultCommandTestCase(BaseArghTestCase):
    def setUp(self):
        self.parser = DebugArghParser('PROG')

        @arg('--foo', default=1)
        def main(args):
            return args.foo

        self.parser.set_default_command(main)

    def test_default_command(self):
        self.assert_cmd_returns('', b('1\n'))
        self.assert_cmd_returns('--foo 2', b('2\n'))
        self.assert_cmd_exits('--help')

    def test_prevent_conflict_with_single_command(self):
        def one(args): return 1
        def two(args): return 2

        p = DebugArghParser('PROG')
        p.set_default_command(one)
        with self.assertRaisesRegexp(RuntimeError,
                               'Cannot add commands to a single-command parser'):
            p.add_commands([two])

    def test_prevent_conflict_with_subparsers(self):
        def one(args): return 1
        def two(args): return 2

        p = DebugArghParser('PROG')
        p.add_commands([one])
        with self.assertRaisesRegexp(RuntimeError,
                               'Cannot set default command to a parser with '
                               'existing subparsers'):
            p.set_default_command(two)


class DispatchCommandTestCase(BaseArghTestCase):
    "Tests for :func:`argh.helpers.dispatch_command`"

    def _dispatch_and_capture(self, func, command_string, **kwargs):
        if isinstance(command_string, string_types):
            args = command_string.split()
        else:
            args = command_string

        io = BytesIO()
        if 'output_file' not in kwargs:
            kwargs['output_file'] = io

        result = dispatch_command(func, args, **kwargs)

        if kwargs.get('output_file') is None:
            return result
        else:
            io.seek(0)
            return io.read()

    def assert_cmd_returns(self, func, command_string, expected_result, **kwargs):
        """Executes given command using given parser and asserts that it prints
        given value.
        """
        try:
            result = self._dispatch_and_capture(func, command_string, **kwargs)
        except SystemExit as error:
            self.fail('Argument parsing failed for {0!r}: {1!r}'.format(
                command_string, error))
        self.assertEqual(result, expected_result)

    def test_dispatch_command_shortcut(self):

        @arg('--foo', default=1)
        def main(args):
            return args.foo

        self.assert_cmd_returns(main, '', b('1\n'))
        self.assert_cmd_returns(main, '--foo 2', b('2\n'))


class DispatchCommandsTestCase(BaseArghTestCase):
    "Tests for :func:`argh.helpers.dispatch_commands`"

    def _dispatch_and_capture(self, funcs, command_string, **kwargs):
        if isinstance(command_string, string_types):
            args = command_string.split()
        else:
            args = command_string

        io = BytesIO()
        if 'output_file' not in kwargs:
            kwargs['output_file'] = io

        result = dispatch_commands(funcs, args, **kwargs)

        if kwargs.get('output_file') is None:
            return result
        else:
            io.seek(0)
            return io.read()

    def assert_cmd_returns(self, funcs, command_string, expected_result, **kwargs):
        """Executes given command using given parser and asserts that it prints
        given value.
        """
        try:
            result = self._dispatch_and_capture(funcs, command_string, **kwargs)
        except SystemExit as error:
            self.fail('Argument parsing failed for {0!r}: {1!r}'.format(
                command_string, error))
        self.assertEqual(result, expected_result)

    def test_dispatch_commands_shortcut(self):

        @arg('-x', default=1)
        def foo(args):
            return args.x

        @arg('-y', default=2)
        def bar(args):
            return args.y

        self.assert_cmd_returns([foo, bar], 'foo', b('1\n'))
        self.assert_cmd_returns([foo, bar], 'foo -x 5', b('5\n'))
        self.assert_cmd_returns([foo, bar], 'bar', b('2\n'))


class ConfirmTestCase(unittest.TestCase):
    def assert_choice(self, choice, expected, **kwargs):
        argh.helpers.raw_input = lambda prompt: choice
        self.assertEqual(argh.confirm('test', **kwargs), expected)

    def test_simple(self):
        self.assert_choice('', None)
        self.assert_choice('', None, default=None)
        self.assert_choice('', True, default=True)
        self.assert_choice('', False, default=False)

        self.assert_choice('y', True)
        self.assert_choice('y', True, default=True)
        self.assert_choice('y', True, default=False)
        self.assert_choice('y', True, default=None)

        self.assert_choice('n', False)
        self.assert_choice('n', False, default=True)
        self.assert_choice('n', False, default=False)
        self.assert_choice('n', False, default=None)

        self.assert_choice('x', None)

    def test_prompt(self):
        "Prompt is properly formatted"
        prompts = []

        def raw_input_mock(prompt):
            prompts.append(prompt)
        argh.helpers.raw_input = raw_input_mock

        argh.confirm('do smth')
        self.assertEqual(prompts[-1], b('do smth? (y/n)'))

        argh.confirm('do smth', default=None)
        self.assertEqual(prompts[-1], b('do smth? (y/n)'))

        argh.confirm('do smth', default=True)
        self.assertEqual(prompts[-1], b('do smth? (Y/n)'))

        argh.confirm('do smth', default=False)
        self.assertEqual(prompts[-1], b('do smth? (y/N)'))

    def test_encoding(self):
        "Unicode and bytes are accepted as prompt message"
        def raw_input_mock(prompt):
            assert isinstance(prompt, binary_type)
        argh.helpers.raw_input = raw_input_mock
        argh.confirm(u('привет'))


class CompletionTestCase(unittest.TestCase):
    def setUp(self):
        "Declare some commands and allocate two namespaces for them"
        def echo(args):
            return args

        def load(args):
            return 'fake load'

        @arg('--format')
        def dump(args):
            return 'fake dump'

        self.parser = DebugArghParser()
        self.parser.add_commands([echo], namespace='silly')
        self.parser.add_commands([load, dump], namespace='fixtures')

    def assert_choices(self, arg_string, expected):
        args = arg_string.split()
        cwords = args
        cword = len(args) + 1
        choices = completion._autocomplete(self.parser, cwords, cword)
        self.assertEqual(' '.join(sorted(choices)), expected)

    def test_root(self):
        self.assert_choices('', 'fixtures silly')

    def test_root_missing(self):
        self.assert_choices('xyz', '')

    def test_root_partial(self):
        self.assert_choices('f', 'fixtures')
        self.assert_choices('fi', 'fixtures')
        self.assert_choices('s', 'silly')

    def test_inner(self):
        self.assert_choices('fixtures', 'dump load')
        self.assert_choices('silly', 'echo')

    def test_inner_partial(self):
        self.assert_choices('fixtures d', 'dump')
        self.assert_choices('fixtures dum', 'dump')
        self.assert_choices('silly e', 'echo')

    def test_inner_extra(self):
        self.assert_choices('silly echo foo', '')

    @unittest.expectedFailure
    def test_inner_options(self):
        self.assert_choices('fixtures dump', '--format')
        self.assert_choices('silly echo', 'text')