summaryrefslogtreecommitdiff
path: root/tests/test_options.py
blob: 44a1097127908d2c011bf7f424ff1b5d080319d0 (plain)
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
# -*- coding: utf-8 -*-
import re
import os
import click
import pytest

from click._compat import text_type


def test_prefixes(runner):
    @click.command()
    @click.option('++foo', is_flag=True, help='das foo')
    @click.option('--bar', is_flag=True, help='das bar')
    def cli(foo, bar):
        click.echo('foo=%s bar=%s' % (foo, bar))

    result = runner.invoke(cli, ['++foo', '--bar'])
    assert not result.exception
    assert result.output == 'foo=True bar=True\n'

    result = runner.invoke(cli, ['--help'])
    assert re.search(r'\+\+foo\s+das foo', result.output) is not None
    assert re.search(r'--bar\s+das bar', result.output) is not None


def test_invalid_option(runner):
    try:
        @click.command()
        @click.option('foo')
        def cli(foo):
            pass
    except TypeError as e:
        assert 'No options defined but a name was passed (foo).' \
            in str(e)
    else:
        assert False, 'Expected a type error because of an invalid option.'


def test_invalid_nargs(runner):
    try:
        @click.command()
        @click.option('--foo', nargs=-1)
        def cli(foo):
            pass
    except TypeError as e:
        assert 'Options cannot have nargs < 0' in str(e)
    else:
        assert False, 'Expected a type error because of an invalid option.'


def test_nargs_tup_composite_mult(runner):
    @click.command()
    @click.option('--item', type=(str, int), multiple=True)
    def copy(item):
        for item in item:
            click.echo('name=%s id=%d' % item)

    result = runner.invoke(copy, ['--item', 'peter', '1', '--item', 'max', '2'])
    assert not result.exception
    assert result.output.splitlines() == [
        'name=peter id=1',
        'name=max id=2',
    ]


def test_counting(runner):
    @click.command()
    @click.option('-v', count=True, help='Verbosity',
                  type=click.IntRange(0, 3))
    def cli(v):
        click.echo('verbosity=%d' % v)

    result = runner.invoke(cli, ['-vvv'])
    assert not result.exception
    assert result.output == 'verbosity=3\n'

    result = runner.invoke(cli, ['-vvvv'])
    assert result.exception
    assert 'Invalid value for "-v": 4 is not in the valid range of 0 to 3.' \
        in result.output

    result = runner.invoke(cli, [])
    assert not result.exception
    assert result.output == 'verbosity=0\n'

    result = runner.invoke(cli, ['--help'])
    assert re.search('-v\s+Verbosity', result.output) is not None


@pytest.mark.parametrize('unknown_flag', ['--foo', '-f'])
def test_unknown_options(runner, unknown_flag):
    @click.command()
    def cli():
        pass

    result = runner.invoke(cli, [unknown_flag])
    assert result.exception
    assert 'no such option: {0}'.format(unknown_flag) in result.output


def test_multiple_required(runner):
    @click.command()
    @click.option('-m', '--message', multiple=True, required=True)
    def cli(message):
        click.echo('\n'.join(message))

    result = runner.invoke(cli, ['-m', 'foo', '-mbar'])
    assert not result.exception
    assert result.output == 'foo\nbar\n'

    result = runner.invoke(cli, [])
    assert result.exception
    assert 'Error: Missing option "-m" / "--message".' in result.output


def test_multiple_envvar(runner):
    @click.command()
    @click.option('--arg', multiple=True)
    def cmd(arg):
        click.echo('|'.join(arg))

    result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',
                           env={'TEST_ARG': 'foo bar baz'})
    assert not result.exception
    assert result.output == 'foo|bar|baz\n'

    @click.command()
    @click.option('--arg', multiple=True, envvar='X')
    def cmd(arg):
        click.echo('|'.join(arg))

    result = runner.invoke(cmd, [], env={'X': 'foo bar baz'})
    assert not result.exception
    assert result.output == 'foo|bar|baz\n'

    @click.command()
    @click.option('--arg', multiple=True, type=click.Path())
    def cmd(arg):
        click.echo('|'.join(arg))

    result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',
                           env={'TEST_ARG': 'foo%sbar' % os.path.pathsep})
    assert not result.exception
    assert result.output == 'foo|bar\n'


def test_multiple_default_help(runner):
    @click.command()
    @click.option('--arg1', multiple=True, default=('foo', 'bar'),
                  show_default=True)
    @click.option('--arg2', multiple=True, default=(1, 2), type=int,
                  show_default=True)
    def cmd(arg, arg2):
        pass

    result = runner.invoke(cmd, ['--help'])
    assert not result.exception
    assert 'foo, bar' in result.output
    assert '1, 2' in result.output


def test_multiple_default_type(runner):
    @click.command()
    @click.option('--arg1', multiple=True, default=('foo', 'bar'))
    @click.option('--arg2', multiple=True, default=(1, 'a'))
    def cmd(arg1, arg2):
        assert all(isinstance(e[0], text_type) for e in arg1)
        assert all(isinstance(e[1], text_type) for e in arg1)

        assert all(isinstance(e[0], int) for e in arg2)
        assert all(isinstance(e[1], text_type) for e in arg2)

    result = runner.invoke(cmd, '--arg1 a b --arg1 test 1 --arg2 2 '
                           'two --arg2 4 four'.split())
    assert not result.exception


def test_nargs_envvar(runner):
    @click.command()
    @click.option('--arg', nargs=2)
    def cmd(arg):
        click.echo('|'.join(arg))

    result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',
                           env={'TEST_ARG': 'foo bar'})
    assert not result.exception
    assert result.output == 'foo|bar\n'

    @click.command()
    @click.option('--arg', nargs=2, multiple=True)
    def cmd(arg):
        for item in arg:
            click.echo('|'.join(item))

    result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',
                           env={'TEST_ARG': 'x 1 y 2'})
    assert not result.exception
    assert result.output == 'x|1\ny|2\n'


def test_custom_validation(runner):
    def validate_pos_int(ctx, value):
        if value < 0:
            raise click.BadParameter('Value needs to be positive')
        return value

    @click.command()
    @click.option('--foo', callback=validate_pos_int, default=1)
    def cmd(foo):
        click.echo(foo)

    result = runner.invoke(cmd, ['--foo', '-1'])
    assert 'Invalid value for "--foo": Value needs to be positive' \
        in result.output

    result = runner.invoke(cmd, ['--foo', '42'])
    assert result.output == '42\n'


def test_winstyle_options(runner):
    @click.command()
    @click.option('/debug;/no-debug', help='Enables or disables debug mode.')
    def cmd(debug):
        click.echo(debug)

    result = runner.invoke(cmd, ['/debug'], help_option_names=['/?'])
    assert result.output == 'True\n'
    result = runner.invoke(cmd, ['/no-debug'], help_option_names=['/?'])
    assert result.output == 'False\n'
    result = runner.invoke(cmd, [], help_option_names=['/?'])
    assert result.output == 'False\n'
    result = runner.invoke(cmd, ['/?'], help_option_names=['/?'])
    assert '/debug; /no-debug  Enables or disables debug mode.' in result.output
    assert '/?                 Show this message and exit.' in result.output


def test_legacy_options(runner):
    @click.command()
    @click.option('-whatever')
    def cmd(whatever):
        click.echo(whatever)

    result = runner.invoke(cmd, ['-whatever', '42'])
    assert result.output == '42\n'
    result = runner.invoke(cmd, ['-whatever=23'])
    assert result.output == '23\n'


def test_missing_choice(runner):
    @click.command()
    @click.option('--foo', type=click.Choice(['foo', 'bar']),
                  required=True)
    def cmd(foo):
        click.echo(foo)

    result = runner.invoke(cmd)
    assert result.exit_code == 2
    assert 'Error: Missing option "--foo".  Choose from foo, bar.' \
        in result.output


def test_multiline_help(runner):
    @click.command()
    @click.option('--foo', help="""
        hello

        i am

        multiline
    """)
    def cmd(foo):
        click.echo(foo)

    result = runner.invoke(cmd, ['--help'])
    assert result.exit_code == 0
    out = result.output.splitlines()
    assert '  --foo TEXT  hello' in out
    assert '              i am' in out
    assert '              multiline' in out


def test_argument_custom_class(runner):
    class CustomArgument(click.Argument):
        def get_default(self, ctx):
            '''a dumb override of a default value for testing'''
            return 'I am a default'

    @click.command()
    @click.argument('testarg', cls=CustomArgument, default='you wont see me')
    def cmd(testarg):
        click.echo(testarg)

    result = runner.invoke(cmd)
    assert 'I am a default' in result.output
    assert 'you wont see me' not in result.output


def test_option_custom_class(runner):
    class CustomOption(click.Option):
        def get_help_record(self, ctx):
            '''a dumb override of a help text for testing'''
            return ('--help', 'I am a help text')

    @click.command()
    @click.option('--testoption', cls=CustomOption, help='you wont see me')
    def cmd(testoption):
        click.echo(testoption)

    result = runner.invoke(cmd, ['--help'])
    assert 'I am a help text' in result.output
    assert 'you wont see me' not in result.output


def test_aliases_for_flags(runner):
    @click.command()
    @click.option('--warnings/--no-warnings', ' /-W', default=True)
    def cli(warnings):
        click.echo(warnings)

    result = runner.invoke(cli, ['--warnings'])
    assert result.output == 'True\n'
    result = runner.invoke(cli, ['--no-warnings'])
    assert result.output == 'False\n'
    result = runner.invoke(cli, ['-W'])
    assert result.output == 'False\n'

    @click.command()
    @click.option('--warnings/--no-warnings', '-w', default=True)
    def cli_alt(warnings):
        click.echo(warnings)

    result = runner.invoke(cli_alt, ['--warnings'])
    assert result.output == 'True\n'
    result = runner.invoke(cli_alt, ['--no-warnings'])
    assert result.output == 'False\n'
    result = runner.invoke(cli_alt, ['-w'])
    assert result.output == 'True\n'

@pytest.mark.parametrize('option_args,expected', [
    (['--aggressive', '--all', '-a'], 'aggressive'),
    (['--first', '--second', '--third', '-a', '-b', '-c'], 'first'),
    (['--apple', '--banana', '--cantaloupe', '-a', '-b', '-c'], 'apple'),
    (['--cantaloupe', '--banana', '--apple', '-c', '-b', '-a'], 'cantaloupe'),
    (['-a', '-b', '-c'], 'a'),
    (['-c', '-b', '-a'], 'c'),
    (['-a', '--apple', '-b', '--banana', '-c', '--cantaloupe'], 'apple'),
    (['-c', '-a', '--cantaloupe', '-b', '--banana', '--apple'], 'cantaloupe'),
    (['--from', '-f', '_from'], '_from'),
    (['--return', '-r', '_ret'], '_ret'),
])
def test_option_names(runner, option_args, expected):

    @click.command()
    @click.option(*option_args, is_flag=True)
    def cmd(**kwargs):
        click.echo(str(kwargs[expected]))

    assert cmd.params[0].name == expected

    for form in option_args:
        if form.startswith('-'):
            result = runner.invoke(cmd, [form])
            assert result.output == 'True\n'