test_context.py
This file
Path
tests/test_context.py
93 lines · 2.0 KiB · Python
Last changed by
Not known.
History of this fileTake 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)