test_custom_classes.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import click
  2. def test_command_context_class():
  3. """A command with a custom ``context_class`` should produce a
  4. context using that type.
  5. """
  6. class CustomContext(click.Context):
  7. pass
  8. class CustomCommand(click.Command):
  9. context_class = CustomContext
  10. command = CustomCommand("test")
  11. context = command.make_context("test", [])
  12. assert isinstance(context, CustomContext)
  13. def test_context_invoke_type(runner):
  14. """A command invoked from a custom context should have a new
  15. context with the same type.
  16. """
  17. class CustomContext(click.Context):
  18. pass
  19. class CustomCommand(click.Command):
  20. context_class = CustomContext
  21. @click.command()
  22. @click.argument("first_id", type=int)
  23. @click.pass_context
  24. def second(ctx, first_id):
  25. assert isinstance(ctx, CustomContext)
  26. assert id(ctx) != first_id
  27. @click.command(cls=CustomCommand)
  28. @click.pass_context
  29. def first(ctx):
  30. assert isinstance(ctx, CustomContext)
  31. ctx.invoke(second, first_id=id(ctx))
  32. assert not runner.invoke(first).exception
  33. def test_context_formatter_class():
  34. """A context with a custom ``formatter_class`` should format help
  35. using that type.
  36. """
  37. class CustomFormatter(click.HelpFormatter):
  38. def write_heading(self, heading):
  39. heading = click.style(heading, fg="yellow")
  40. return super().write_heading(heading)
  41. class CustomContext(click.Context):
  42. formatter_class = CustomFormatter
  43. context = CustomContext(
  44. click.Command("test", params=[click.Option(["--value"])]), color=True
  45. )
  46. assert "\x1b[33mOptions\x1b[0m:" in context.get_help()
  47. def test_group_command_class(runner):
  48. """A group with a custom ``command_class`` should create subcommands
  49. of that type by default.
  50. """
  51. class CustomCommand(click.Command):
  52. pass
  53. class CustomGroup(click.Group):
  54. command_class = CustomCommand
  55. group = CustomGroup()
  56. subcommand = group.command()(lambda: None)
  57. assert type(subcommand) is CustomCommand
  58. subcommand = group.command(cls=click.Command)(lambda: None)
  59. assert type(subcommand) is click.Command
  60. def test_group_group_class(runner):
  61. """A group with a custom ``group_class`` should create subgroups
  62. of that type by default.
  63. """
  64. class CustomSubGroup(click.Group):
  65. pass
  66. class CustomGroup(click.Group):
  67. group_class = CustomSubGroup
  68. group = CustomGroup()
  69. subgroup = group.group()(lambda: None)
  70. assert type(subgroup) is CustomSubGroup
  71. subgroup = group.command(cls=click.Group)(lambda: None)
  72. assert type(subgroup) is click.Group
  73. def test_group_group_class_self(runner):
  74. """A group with ``group_class = type`` should create subgroups of
  75. the same type as itself.
  76. """
  77. class CustomGroup(click.Group):
  78. group_class = type
  79. group = CustomGroup()
  80. subgroup = group.group()(lambda: None)
  81. assert type(subgroup) is CustomGroup