test_context.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- coding: utf-8 -*-
  2. import click
  3. def test_ensure_context_objects(runner):
  4. class Foo(object):
  5. def __init__(self):
  6. self.title = "default"
  7. pass_foo = click.make_pass_decorator(Foo, ensure=True)
  8. @click.group()
  9. @pass_foo
  10. def cli(foo):
  11. pass
  12. @cli.command()
  13. @pass_foo
  14. def test(foo):
  15. click.echo(foo.title)
  16. result = runner.invoke(cli, ["test"])
  17. assert not result.exception
  18. assert result.output == "default\n"
  19. def test_get_context_objects(runner):
  20. class Foo(object):
  21. def __init__(self):
  22. self.title = "default"
  23. pass_foo = click.make_pass_decorator(Foo, ensure=True)
  24. @click.group()
  25. @click.pass_context
  26. def cli(ctx):
  27. ctx.obj = Foo()
  28. ctx.obj.title = "test"
  29. @cli.command()
  30. @pass_foo
  31. def test(foo):
  32. click.echo(foo.title)
  33. result = runner.invoke(cli, ["test"])
  34. assert not result.exception
  35. assert result.output == "test\n"
  36. def test_get_context_objects_no_ensuring(runner):
  37. class Foo(object):
  38. def __init__(self):
  39. self.title = "default"
  40. pass_foo = click.make_pass_decorator(Foo)
  41. @click.group()
  42. @click.pass_context
  43. def cli(ctx):
  44. ctx.obj = Foo()
  45. ctx.obj.title = "test"
  46. @cli.command()
  47. @pass_foo
  48. def test(foo):
  49. click.echo(foo.title)
  50. result = runner.invoke(cli, ["test"])
  51. assert not result.exception
  52. assert result.output == "test\n"
  53. def test_get_context_objects_missing(runner):
  54. class Foo(object):
  55. pass
  56. pass_foo = click.make_pass_decorator(Foo)
  57. @click.group()
  58. @click.pass_context
  59. def cli(ctx):
  60. pass
  61. @cli.command()
  62. @pass_foo
  63. def test(foo):
  64. click.echo(foo.title)
  65. result = runner.invoke(cli, ["test"])
  66. assert result.exception is not None
  67. assert isinstance(result.exception, RuntimeError)
  68. assert (
  69. "Managed to invoke callback without a context object of type"
  70. " 'Foo' existing" in str(result.exception)
  71. )
  72. def test_multi_enter(runner):
  73. called = []
  74. @click.command()
  75. @click.pass_context
  76. def cli(ctx):
  77. def callback():
  78. called.append(True)
  79. ctx.call_on_close(callback)
  80. with ctx:
  81. pass
  82. assert not called
  83. result = runner.invoke(cli, [])
  84. assert result.exception is None
  85. assert called == [True]
  86. def test_global_context_object(runner):
  87. @click.command()
  88. @click.pass_context
  89. def cli(ctx):
  90. assert click.get_current_context() is ctx
  91. ctx.obj = "FOOBAR"
  92. assert click.get_current_context().obj == "FOOBAR"
  93. assert click.get_current_context(silent=True) is None
  94. runner.invoke(cli, [], catch_exceptions=False)
  95. assert click.get_current_context(silent=True) is None
  96. def test_context_meta(runner):
  97. LANG_KEY = "{}.lang".format(__name__)
  98. def set_language(value):
  99. click.get_current_context().meta[LANG_KEY] = value
  100. def get_language():
  101. return click.get_current_context().meta.get(LANG_KEY, "en_US")
  102. @click.command()
  103. @click.pass_context
  104. def cli(ctx):
  105. assert get_language() == "en_US"
  106. set_language("de_DE")
  107. assert get_language() == "de_DE"
  108. runner.invoke(cli, [], catch_exceptions=False)
  109. def test_context_pushing():
  110. rv = []
  111. @click.command()
  112. def cli():
  113. pass
  114. ctx = click.Context(cli)
  115. @ctx.call_on_close
  116. def test_callback():
  117. rv.append(42)
  118. with ctx.scope(cleanup=False):
  119. # Internal
  120. assert ctx._depth == 2
  121. assert rv == []
  122. with ctx.scope():
  123. # Internal
  124. assert ctx._depth == 1
  125. assert rv == [42]
  126. def test_pass_obj(runner):
  127. @click.group()
  128. @click.pass_context
  129. def cli(ctx):
  130. ctx.obj = "test"
  131. @cli.command()
  132. @click.pass_obj
  133. def test(obj):
  134. click.echo(obj)
  135. result = runner.invoke(cli, ["test"])
  136. assert not result.exception
  137. assert result.output == "test\n"
  138. def test_close_before_pop(runner):
  139. called = []
  140. @click.command()
  141. @click.pass_context
  142. def cli(ctx):
  143. ctx.obj = "test"
  144. @ctx.call_on_close
  145. def foo():
  146. assert click.get_current_context().obj == "test"
  147. called.append(True)
  148. click.echo("aha!")
  149. result = runner.invoke(cli, [])
  150. assert not result.exception
  151. assert result.output == "aha!\n"
  152. assert called == [True]
  153. def test_make_pass_decorator_args(runner):
  154. """
  155. Test to check that make_pass_decorator doesn't consume arguments based on
  156. invocation order.
  157. """
  158. class Foo(object):
  159. title = "foocmd"
  160. pass_foo = click.make_pass_decorator(Foo)
  161. @click.group()
  162. @click.pass_context
  163. def cli(ctx):
  164. ctx.obj = Foo()
  165. @cli.command()
  166. @click.pass_context
  167. @pass_foo
  168. def test1(foo, ctx):
  169. click.echo(foo.title)
  170. @cli.command()
  171. @pass_foo
  172. @click.pass_context
  173. def test2(ctx, foo):
  174. click.echo(foo.title)
  175. result = runner.invoke(cli, ["test1"])
  176. assert not result.exception
  177. assert result.output == "foocmd\n"
  178. result = runner.invoke(cli, ["test2"])
  179. assert not result.exception
  180. assert result.output == "foocmd\n"
  181. def test_exit_not_standalone():
  182. @click.command()
  183. @click.pass_context
  184. def cli(ctx):
  185. ctx.exit(1)
  186. assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 1
  187. @click.command()
  188. @click.pass_context
  189. def cli(ctx):
  190. ctx.exit(0)
  191. assert cli.main([], "test_exit_not_standalone", standalone_mode=False) == 0