aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/click/py2/tests/test_basic.py
blob: f07b6d1f50065dc89c0ee2a50e414b4da996ca79 (plain) (blame)
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# -*- coding: utf-8 -*-
import os
import uuid

import click


def test_basic_functionality(runner):
    @click.command()
    def cli():
        """Hello World!"""
        click.echo("I EXECUTED")

    result = runner.invoke(cli, ["--help"])
    assert not result.exception
    assert "Hello World!" in result.output
    assert "Show this message and exit." in result.output
    assert result.exit_code == 0
    assert "I EXECUTED" not in result.output

    result = runner.invoke(cli, [])
    assert not result.exception
    assert "I EXECUTED" in result.output
    assert result.exit_code == 0


def test_repr():
    @click.command()
    def command():
        pass

    @click.group()
    def group():
        pass

    @group.command()
    def subcommand():
        pass

    assert repr(command) == "<Command command>"
    assert repr(group) == "<Group group>"
    assert repr(subcommand) == "<Command subcommand>"


def test_return_values():
    @click.command()
    def cli():
        return 42

    with cli.make_context("foo", []) as ctx:
        rv = cli.invoke(ctx)
        assert rv == 42


def test_basic_group(runner):
    @click.group()
    def cli():
        """This is the root."""
        click.echo("ROOT EXECUTED")

    @cli.command()
    def subcommand():
        """This is a subcommand."""
        click.echo("SUBCOMMAND EXECUTED")

    result = runner.invoke(cli, ["--help"])
    assert not result.exception
    assert "COMMAND [ARGS]..." in result.output
    assert "This is the root" in result.output
    assert "This is a subcommand." in result.output
    assert result.exit_code == 0
    assert "ROOT EXECUTED" not in result.output

    result = runner.invoke(cli, ["subcommand"])
    assert not result.exception
    assert result.exit_code == 0
    assert "ROOT EXECUTED" in result.output
    assert "SUBCOMMAND EXECUTED" in result.output


def test_basic_option(runner):
    @click.command()
    @click.option("--foo", default="no value")
    def cli(foo):
        click.echo(u"FOO:[{}]".format(foo))

    result = runner.invoke(cli, [])
    assert not result.exception
    assert "FOO:[no value]" in result.output

    result = runner.invoke(cli, ["--foo=42"])
    assert not result.exception
    assert "FOO:[42]" in result.output

    result = runner.invoke(cli, ["--foo"])
    assert result.exception
    assert "--foo option requires an argument" in result.output

    result = runner.invoke(cli, ["--foo="])
    assert not result.exception
    assert "FOO:[]" in result.output

    result = runner.invoke(cli, [u"--foo=\N{SNOWMAN}"])
    assert not result.exception
    assert u"FOO:[\N{SNOWMAN}]" in result.output


def test_int_option(runner):
    @click.command()
    @click.option("--foo", default=42)
    def cli(foo):
        click.echo("FOO:[{}]".format(foo * 2))

    result = runner.invoke(cli, [])
    assert not result.exception
    assert "FOO:[84]" in result.output

    result = runner.invoke(cli, ["--foo=23"])
    assert not result.exception
    assert "FOO:[46]" in result.output

    result = runner.invoke(cli, ["--foo=bar"])
    assert result.exception
    assert "Invalid value for '--foo': bar is not a valid integer" in result.output


def test_uuid_option(runner):
    @click.command()
    @click.option(
        "--u", default="ba122011-349f-423b-873b-9d6a79c688ab", type=click.UUID
    )
    def cli(u):
        assert type(u) is uuid.UUID
        click.echo("U:[{}]".format(u))

    result = runner.invoke(cli, [])
    assert not result.exception
    assert "U:[ba122011-349f-423b-873b-9d6a79c688ab]" in result.output

    result = runner.invoke(cli, ["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"])
    assert not result.exception
    assert "U:[821592c1-c50e-4971-9cd6-e89dc6832f86]" in result.output

    result = runner.invoke(cli, ["--u=bar"])
    assert result.exception
    assert "Invalid value for '--u': bar is not a valid UUID value" in result.output


def test_float_option(runner):
    @click.command()
    @click.option("--foo", default=42, type=click.FLOAT)
    def cli(foo):
        assert type(foo) is float
        click.echo("FOO:[{}]".format(foo))

    result = runner.invoke(cli, [])
    assert not result.exception
    assert "FOO:[42.0]" in result.output

    result = runner.invoke(cli, ["--foo=23.5"])
    assert not result.exception
    assert "FOO:[23.5]" in result.output

    result = runner.invoke(cli, ["--foo=bar"])
    assert result.exception
    assert "Invalid value for '--foo': bar is not a valid float" in result.output


def test_boolean_option(runner):
    for default in True, False:

        @click.command()
        @click.option("--with-foo/--without-foo", default=default)
        def cli(with_foo):
            click.echo(with_foo)

        result = runner.invoke(cli, ["--with-foo"])
        assert not result.exception
        assert result.output == "True\n"
        result = runner.invoke(cli, ["--without-foo"])
        assert not result.exception
        assert result.output == "False\n"
        result = runner.invoke(cli, [])
        assert not result.exception
        assert result.output == "{}\n".format(default)

    for default in True, False:

        @click.command()
        @click.option("--flag", is_flag=True, default=default)
        def cli(flag):
            click.echo(flag)

        result = runner.invoke(cli, ["--flag"])
        assert not result.exception
        assert result.output == "{}\n".format(not default)
        result = runner.invoke(cli, [])
        assert not result.exception
        assert result.output == "{}\n".format(default)


def test_boolean_conversion(runner):
    for default in True, False:

        @click.command()
        @click.option("--flag", default=default, type=bool)
        def cli(flag):
            click.echo(flag)

        for value in "true", "t", "1", "yes", "y":
            result = runner.invoke(cli, ["--flag", value])
            assert not result.exception
            assert result.output == "True\n"

        for value in "false", "f", "0", "no", "n":
            result = runner.invoke(cli, ["--flag", value])
            assert not result.exception
            assert result.output == "False\n"

        result = runner.invoke(cli, [])
        assert not result.exception
        assert result.output == "{}\n".format(default)


def test_file_option(runner):
    @click.command()
    @click.option("--file", type=click.File("w"))
    def input(file):
        file.write("Hello World!\n")

    @click.command()
    @click.option("--file", type=click.File("r"))
    def output(file):
        click.echo(file.read())

    with runner.isolated_filesystem():
        result_in = runner.invoke(input, ["--file=example.txt"])
        result_out = runner.invoke(output, ["--file=example.txt"])

    assert not result_in.exception
    assert result_in.output == ""
    assert not result_out.exception
    assert result_out.output == "Hello World!\n\n"


def test_file_lazy_mode(runner):
    do_io = False

    @click.command()
    @click.option("--file", type=click.File("w"))
    def input(file):
        if do_io:
            file.write("Hello World!\n")

    @click.command()
    @click.option("--file", type=click.File("r"))
    def output(file):
        pass

    with runner.isolated_filesystem():
        os.mkdir("example.txt")

        do_io = True
        result_in = runner.invoke(input, ["--file=example.txt"])
        assert result_in.exit_code == 1

        do_io = False
        result_in = runner.invoke(input, ["--file=example.txt"])
        assert result_in.exit_code == 0

        result_out = runner.invoke(output, ["--file=example.txt"])
        assert result_out.exception

    @click.command()
    @click.option("--file", type=click.File("w", lazy=False))
    def input_non_lazy(file):
        file.write("Hello World!\n")

    with runner.isolated_filesystem():
        os.mkdir("example.txt")
        result_in = runner.invoke(input_non_lazy, ["--file=example.txt"])
        assert result_in.exit_code == 2
        assert (
            "Invalid value for '--file': Could not open file: example.txt"
            in result_in.output
        )


def test_path_option(runner):
    @click.command()
    @click.option("-O", type=click.Path(file_okay=False, exists=True, writable=True))
    def write_to_dir(o):
        with open(os.path.join(o, "foo.txt"), "wb") as f:
            f.write(b"meh\n")

    with runner.isolated_filesystem():
        os.mkdir("test")

        result = runner.invoke(write_to_dir, ["-O", "test"])
        assert not result.exception

        with open("test/foo.txt", "rb") as f:
            assert f.read() == b"meh\n"

        result = runner.invoke(write_to_dir, ["-O", "test/foo.txt"])
        assert "is a file" in result.output

    @click.command()
    @click.option("-f", type=click.Path(exists=True))
    def showtype(f):
        click.echo("is_file={}".format(os.path.isfile(f)))
        click.echo("is_dir={}".format(os.path.isdir(f)))

    with runner.isolated_filesystem():
        result = runner.invoke(showtype, ["-f", "xxx"])
        assert "does not exist" in result.output

        result = runner.invoke(showtype, ["-f", "."])
        assert "is_file=False" in result.output
        assert "is_dir=True" in result.output

    @click.command()
    @click.option("-f", type=click.Path())
    def exists(f):
        click.echo("exists={}".format(os.path.exists(f)))

    with runner.isolated_filesystem():
        result = runner.invoke(exists, ["-f", "xxx"])
        assert "exists=False" in result.output

        result = runner.invoke(exists, ["-f", "."])
        assert "exists=True" in result.output


def test_choice_option(runner):
    @click.command()
    @click.option("--method", type=click.Choice(["foo", "bar", "baz"]))
    def cli(method):
        click.echo(method)

    result = runner.invoke(cli, ["--method=foo"])
    assert not result.exception
    assert result.output == "foo\n"

    result = runner.invoke(cli, ["--method=meh"])
    assert result.exit_code == 2
    assert (
        "Invalid value for '--method': invalid choice: meh."
        " (choose from foo, bar, baz)" in result.output
    )

    result = runner.invoke(cli, ["--help"])
    assert "--method [foo|bar|baz]" in result.output


def test_datetime_option_default(runner):
    @click.command()
    @click.option("--start_date", type=click.DateTime())
    def cli(start_date):
        click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))

    result = runner.invoke(cli, ["--start_date=2015-09-29"])
    assert not result.exception
    assert result.output == "2015-09-29T00:00:00\n"

    result = runner.invoke(cli, ["--start_date=2015-09-29T09:11:22"])
    assert not result.exception
    assert result.output == "2015-09-29T09:11:22\n"

    result = runner.invoke(cli, ["--start_date=2015-09"])
    assert result.exit_code == 2
    assert (
        "Invalid value for '--start_date':"
        " invalid datetime format: 2015-09."
        " (choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S, %Y-%m-%d %H:%M:%S)"
    ) in result.output

    result = runner.invoke(cli, ["--help"])
    assert (
        "--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S]" in result.output
    )


def test_datetime_option_custom(runner):
    @click.command()
    @click.option("--start_date", type=click.DateTime(formats=["%A %B %d, %Y"]))
    def cli(start_date):
        click.echo(start_date.strftime("%Y-%m-%dT%H:%M:%S"))

    result = runner.invoke(cli, ["--start_date=Wednesday June 05, 2010"])
    assert not result.exception
    assert result.output == "2010-06-05T00:00:00\n"


def test_int_range_option(runner):
    @click.command()
    @click.option("--x", type=click.IntRange(0, 5))
    def cli(x):
        click.echo(x)

    result = runner.invoke(cli, ["--x=5"])
    assert not result.exception
    assert result.output == "5\n"

    result = runner.invoke(cli, ["--x=6"])
    assert result.exit_code == 2
    assert (
        "Invalid value for '--x': 6 is not in the valid range of 0 to 5.\n"
        in result.output
    )

    @click.command()
    @click.option("--x", type=click.IntRange(0, 5, clamp=True))
    def clamp(x):
        click.echo(x)

    result = runner.invoke(clamp, ["--x=5"])
    assert not result.exception
    assert result.output == "5\n"

    result = runner.invoke(clamp, ["--x=6"])
    assert not result.exception
    assert result.output == "5\n"

    result = runner.invoke(clamp, ["--x=-1"])
    assert not result.exception
    assert result.output == "0\n"


def test_float_range_option(runner):
    @click.command()
    @click.option("--x", type=click.FloatRange(0, 5))
    def cli(x):
        click.echo(x)

    result = runner.invoke(cli, ["--x=5.0"])
    assert not result.exception
    assert result.output == "5.0\n"

    result = runner.invoke(cli, ["--x=6.0"])
    assert result.exit_code == 2
    assert (
        "Invalid value for '--x': 6.0 is not in the valid range of 0 to 5.\n"
        in result.output
    )

    @click.command()
    @click.option("--x", type=click.FloatRange(0, 5, clamp=True))
    def clamp(x):
        click.echo(x)

    result = runner.invoke(clamp, ["--x=5.0"])
    assert not result.exception
    assert result.output == "5.0\n"

    result = runner.invoke(clamp, ["--x=6.0"])
    assert not result.exception
    assert result.output == "5\n"

    result = runner.invoke(clamp, ["--x=-1.0"])
    assert not result.exception
    assert result.output == "0\n"


def test_required_option(runner):
    @click.command()
    @click.option("--foo", required=True)
    def cli(foo):
        click.echo(foo)

    result = runner.invoke(cli, [])
    assert result.exit_code == 2
    assert "Missing option '--foo'" in result.output


def test_evaluation_order(runner):
    called = []

    def memo(ctx, param, value):
        called.append(value)
        return value

    @click.command()
    @click.option("--missing", default="missing", is_eager=False, callback=memo)
    @click.option("--eager-flag1", flag_value="eager1", is_eager=True, callback=memo)
    @click.option("--eager-flag2", flag_value="eager2", is_eager=True, callback=memo)
    @click.option("--eager-flag3", flag_value="eager3", is_eager=True, callback=memo)
    @click.option("--normal-flag1", flag_value="normal1", is_eager=False, callback=memo)
    @click.option("--normal-flag2", flag_value="normal2", is_eager=False, callback=memo)
    @click.option("--normal-flag3", flag_value="normal3", is_eager=False, callback=memo)
    def cli(**x):
        pass

    result = runner.invoke(
        cli,
        [
            "--eager-flag2",
            "--eager-flag1",
            "--normal-flag2",
            "--eager-flag3",
            "--normal-flag3",
            "--normal-flag3",
            "--normal-flag1",
            "--normal-flag1",
        ],
    )
    assert not result.exception
    assert called == [
        "eager2",
        "eager1",
        "eager3",
        "normal2",
        "normal3",
        "normal1",
        "missing",
    ]


def test_hidden_option(runner):
    @click.command()
    @click.option("--nope", hidden=True)
    def cli(nope):
        click.echo(nope)

    result = runner.invoke(cli, ["--help"])
    assert result.exit_code == 0
    assert "--nope" not in result.output


def test_hidden_command(runner):
    @click.group()
    def cli():
        pass

    @cli.command(hidden=True)
    def nope():
        pass

    result = runner.invoke(cli, ["--help"])
    assert result.exit_code == 0
    assert "nope" not in result.output


def test_hidden_group(runner):
    @click.group()
    def cli():
        pass

    @cli.group(hidden=True)
    def subgroup():
        pass

    @subgroup.command()
    def nope():
        pass

    result = runner.invoke(cli, ["--help"])
    assert result.exit_code == 0
    assert "subgroup" not in result.output
    assert "nope" not in result.output