2014-10-16 20:40:34 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import click
|
|
|
|
|
|
|
|
|
|
|
|
def test_ensure_context_objects(runner):
|
|
|
|
class Foo(object):
|
|
|
|
def __init__(self):
|
2020-07-21 08:23:42 +02:00
|
|
|
self.title = "default"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
pass_foo = click.make_pass_decorator(Foo, ensure=True)
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@pass_foo
|
|
|
|
def cli(foo):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@pass_foo
|
|
|
|
def test(foo):
|
|
|
|
click.echo(foo.title)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["test"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "default\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_context_objects(runner):
|
|
|
|
class Foo(object):
|
|
|
|
def __init__(self):
|
2020-07-21 08:23:42 +02:00
|
|
|
self.title = "default"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
pass_foo = click.make_pass_decorator(Foo, ensure=True)
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
ctx.obj = Foo()
|
2020-07-21 08:23:42 +02:00
|
|
|
ctx.obj.title = "test"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@pass_foo
|
|
|
|
def test(foo):
|
|
|
|
click.echo(foo.title)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["test"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "test\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_context_objects_no_ensuring(runner):
|
|
|
|
class Foo(object):
|
|
|
|
def __init__(self):
|
2020-07-21 08:23:42 +02:00
|
|
|
self.title = "default"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
pass_foo = click.make_pass_decorator(Foo)
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
ctx.obj = Foo()
|
2020-07-21 08:23:42 +02:00
|
|
|
ctx.obj.title = "test"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@pass_foo
|
|
|
|
def test(foo):
|
|
|
|
click.echo(foo.title)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["test"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "test\n"
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_context_objects_missing(runner):
|
|
|
|
class Foo(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
pass_foo = click.make_pass_decorator(Foo)
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@pass_foo
|
|
|
|
def test(foo):
|
|
|
|
click.echo(foo.title)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["test"])
|
2014-10-16 20:40:34 +02:00
|
|
|
assert result.exception is not None
|
|
|
|
assert isinstance(result.exception, RuntimeError)
|
2020-07-21 08:23:42 +02:00
|
|
|
assert (
|
|
|
|
"Managed to invoke callback without a context object of type"
|
|
|
|
" 'Foo' existing" in str(result.exception)
|
|
|
|
)
|
2014-10-16 20:40:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_multi_enter(runner):
|
|
|
|
called = []
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
def callback():
|
|
|
|
called.append(True)
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2014-10-16 20:40:34 +02:00
|
|
|
ctx.call_on_close(callback)
|
|
|
|
|
|
|
|
with ctx:
|
|
|
|
pass
|
|
|
|
assert not called
|
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert result.exception is None
|
|
|
|
assert called == [True]
|
2015-08-23 03:10:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_global_context_object(runner):
|
|
|
|
@click.command()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
assert click.get_current_context() is ctx
|
2020-07-21 08:23:42 +02:00
|
|
|
ctx.obj = "FOOBAR"
|
|
|
|
assert click.get_current_context().obj == "FOOBAR"
|
2015-08-23 03:10:31 +02:00
|
|
|
|
|
|
|
assert click.get_current_context(silent=True) is None
|
|
|
|
runner.invoke(cli, [], catch_exceptions=False)
|
|
|
|
assert click.get_current_context(silent=True) is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_context_meta(runner):
|
2020-07-21 08:23:42 +02:00
|
|
|
LANG_KEY = "{}.lang".format(__name__)
|
2015-08-23 03:10:31 +02:00
|
|
|
|
|
|
|
def set_language(value):
|
|
|
|
click.get_current_context().meta[LANG_KEY] = value
|
|
|
|
|
|
|
|
def get_language():
|
2020-07-21 08:23:42 +02:00
|
|
|
return click.get_current_context().meta.get(LANG_KEY, "en_US")
|
2015-08-23 03:10:31 +02:00
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
2020-07-21 08:23:42 +02:00
|
|
|
assert get_language() == "en_US"
|
|
|
|
set_language("de_DE")
|
|
|
|
assert get_language() == "de_DE"
|
2015-08-23 03:10:31 +02:00
|
|
|
|
|
|
|
runner.invoke(cli, [], catch_exceptions=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_context_pushing():
|
|
|
|
rv = []
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
ctx = click.Context(cli)
|
|
|
|
|
|
|
|
@ctx.call_on_close
|
|
|
|
def test_callback():
|
|
|
|
rv.append(42)
|
|
|
|
|
|
|
|
with ctx.scope(cleanup=False):
|
|
|
|
# Internal
|
|
|
|
assert ctx._depth == 2
|
|
|
|
|
|
|
|
assert rv == []
|
|
|
|
|
|
|
|
with ctx.scope():
|
|
|
|
# Internal
|
|
|
|
assert ctx._depth == 1
|
|
|
|
|
|
|
|
assert rv == [42]
|
|
|
|
|
|
|
|
|
|
|
|
def test_pass_obj(runner):
|
|
|
|
@click.group()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
2020-07-21 08:23:42 +02:00
|
|
|
ctx.obj = "test"
|
2015-08-23 03:10:31 +02:00
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.pass_obj
|
|
|
|
def test(obj):
|
|
|
|
click.echo(obj)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["test"])
|
2015-08-23 03:10:31 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "test\n"
|
2015-12-04 16:51:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_close_before_pop(runner):
|
|
|
|
called = []
|
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
2020-07-21 08:23:42 +02:00
|
|
|
ctx.obj = "test"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2015-12-04 16:51:02 +01:00
|
|
|
@ctx.call_on_close
|
|
|
|
def foo():
|
2020-07-21 08:23:42 +02:00
|
|
|
assert click.get_current_context().obj == "test"
|
2015-12-04 16:51:02 +01:00
|
|
|
called.append(True)
|
2020-07-21 08:23:42 +02:00
|
|
|
|
|
|
|
click.echo("aha!")
|
2015-12-04 16:51:02 +01:00
|
|
|
|
|
|
|
result = runner.invoke(cli, [])
|
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "aha!\n"
|
2015-12-04 16:51:02 +01:00
|
|
|
assert called == [True]
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_pass_decorator_args(runner):
|
|
|
|
"""
|
|
|
|
Test to check that make_pass_decorator doesn't consume arguments based on
|
|
|
|
invocation order.
|
|
|
|
"""
|
2020-07-21 08:23:42 +02:00
|
|
|
|
2018-09-06 20:55:10 +02:00
|
|
|
class Foo(object):
|
2020-07-21 08:23:42 +02:00
|
|
|
title = "foocmd"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
pass_foo = click.make_pass_decorator(Foo)
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
ctx.obj = Foo()
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.pass_context
|
|
|
|
@pass_foo
|
|
|
|
def test1(foo, ctx):
|
|
|
|
click.echo(foo.title)
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@pass_foo
|
|
|
|
@click.pass_context
|
|
|
|
def test2(ctx, foo):
|
|
|
|
click.echo(foo.title)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["test1"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "foocmd\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
result = runner.invoke(cli, ["test2"])
|
2018-09-06 20:55:10 +02:00
|
|
|
assert not result.exception
|
2020-07-21 08:23:42 +02:00
|
|
|
assert result.output == "foocmd\n"
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_exit_not_standalone():
|
|
|
|
@click.command()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
ctx.exit(1)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 1
|
2018-09-06 20:55:10 +02:00
|
|
|
|
|
|
|
@click.command()
|
|
|
|
@click.pass_context
|
|
|
|
def cli(ctx):
|
|
|
|
ctx.exit(0)
|
|
|
|
|
2020-07-21 08:23:42 +02:00
|
|
|
assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 0
|