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
|
import asyncclick as click
import pytest
@pytest.mark.anyio
async def test_command_context_class():
"""A command with a custom ``context_class`` should produce a
context using that type.
"""
class CustomContext(click.Context):
pass
class CustomCommand(click.Command):
context_class = CustomContext
command = CustomCommand("test")
context = await command.make_context("test", [])
assert isinstance(context, CustomContext)
def test_context_invoke_type(runner):
"""A command invoked from a custom context should have a new
context with the same type.
"""
class CustomContext(click.Context):
pass
class CustomCommand(click.Command):
context_class = CustomContext
@click.command()
@click.argument("first_id", type=int)
@click.pass_context
def second(ctx, first_id):
assert isinstance(ctx, CustomContext)
assert id(ctx) != first_id
@click.command(cls=CustomCommand)
@click.pass_context
async def first(ctx):
assert isinstance(ctx, CustomContext)
await ctx.invoke(second, first_id=id(ctx))
assert not runner.invoke(first).exception
def test_context_formatter_class():
"""A context with a custom ``formatter_class`` should format help
using that type.
"""
class CustomFormatter(click.HelpFormatter):
def write_heading(self, heading):
heading = click.style(heading, fg="yellow")
return super().write_heading(heading)
class CustomContext(click.Context):
formatter_class = CustomFormatter
context = CustomContext(
click.Command("test", params=[click.Option(["--value"])]), color=True
)
assert "\x1b[33mOptions\x1b[0m:" in context.get_help()
def test_group_command_class(runner):
"""A group with a custom ``command_class`` should create subcommands
of that type by default.
"""
class CustomCommand(click.Command):
pass
class CustomGroup(click.Group):
command_class = CustomCommand
group = CustomGroup()
subcommand = group.command()(lambda: None)
assert type(subcommand) is CustomCommand
subcommand = group.command(cls=click.Command)(lambda: None)
assert type(subcommand) is click.Command
def test_group_group_class(runner):
"""A group with a custom ``group_class`` should create subgroups
of that type by default.
"""
class CustomSubGroup(click.Group):
pass
class CustomGroup(click.Group):
group_class = CustomSubGroup
group = CustomGroup()
subgroup = group.group()(lambda: None)
assert type(subgroup) is CustomSubGroup
subgroup = group.command(cls=click.Group)(lambda: None)
assert type(subgroup) is click.Group
def test_group_group_class_self(runner):
"""A group with ``group_class = type`` should create subgroups of
the same type as itself.
"""
class CustomGroup(click.Group):
group_class = type
group = CustomGroup()
subgroup = group.group()(lambda: None)
assert type(subgroup) is CustomGroup
|