Skip to content
Myr

test_context.py

This file

Path

tests/test_context.py

112 lines · 2.3 KiB · Python

Last changed by

Added a test for multi command entering.Armin Ronacherbefore adoptionf92c4f4

History of this file

Take it

1# -*- coding: utf-8 -*-2import click345def test_ensure_context_objects(runner):6    class Foo(object):7        def __init__(self):8            self.title = 'default'910    pass_foo = click.make_pass_decorator(Foo, ensure=True)1112    @click.group()13    @pass_foo14    def cli(foo):15        pass1617    @cli.command()18    @pass_foo19    def test(foo):20        click.echo(foo.title)2122    result = runner.invoke(cli, ['test'])23    assert not result.exception24    assert result.output == 'default\n'252627def test_get_context_objects(runner):28    class Foo(object):29        def __init__(self):30            self.title = 'default'3132    pass_foo = click.make_pass_decorator(Foo, ensure=True)3334    @click.group()35    @click.pass_context36    def cli(ctx):37        ctx.obj = Foo()38        ctx.obj.title = 'test'3940    @cli.command()41    @pass_foo42    def test(foo):43        click.echo(foo.title)4445    result = runner.invoke(cli, ['test'])46    assert not result.exception47    assert result.output == 'test\n'484950def test_get_context_objects_no_ensuring(runner):51    class Foo(object):52        def __init__(self):53            self.title = 'default'5455    pass_foo = click.make_pass_decorator(Foo)5657    @click.group()58    @click.pass_context59    def cli(ctx):60        ctx.obj = Foo()61        ctx.obj.title = 'test'6263    @cli.command()64    @pass_foo65    def test(foo):66        click.echo(foo.title)6768    result = runner.invoke(cli, ['test'])69    assert not result.exception70    assert result.output == 'test\n'717273def test_get_context_objects_missing(runner):74    class Foo(object):75        pass7677    pass_foo = click.make_pass_decorator(Foo)7879    @click.group()80    @click.pass_context81    def cli(ctx):82        pass8384    @cli.command()85    @pass_foo86    def test(foo):87        click.echo(foo.title)8889    result = runner.invoke(cli, ['test'])90    assert result.exception is not None91    assert isinstance(result.exception, RuntimeError)92    assert "Managed to invoke callback without a context object " \93        "of type 'Foo' existing" in str(result.exception)949596def test_multi_enter(runner):97    called = []9899    @click.command()100    @click.pass_context101    def cli(ctx):102        def callback():103            called.append(True)104        ctx.call_on_close(callback)105106        with ctx:107            pass108        assert not called109110    result = runner.invoke(cli, [])111    assert result.exception is None112    assert called == [True]