test_testing.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import os
  2. import sys
  3. import pytest
  4. import click
  5. from click._compat import PY2
  6. from click._compat import WIN
  7. from click.testing import CliRunner
  8. # Use the most reasonable io that users would use for the python version.
  9. if PY2:
  10. from cStringIO import StringIO as ReasonableBytesIO
  11. else:
  12. from io import BytesIO as ReasonableBytesIO
  13. def test_runner():
  14. @click.command()
  15. def test():
  16. i = click.get_binary_stream("stdin")
  17. o = click.get_binary_stream("stdout")
  18. while 1:
  19. chunk = i.read(4096)
  20. if not chunk:
  21. break
  22. o.write(chunk)
  23. o.flush()
  24. runner = CliRunner()
  25. result = runner.invoke(test, input="Hello World!\n")
  26. assert not result.exception
  27. assert result.output == "Hello World!\n"
  28. runner = CliRunner(echo_stdin=True)
  29. result = runner.invoke(test, input="Hello World!\n")
  30. assert not result.exception
  31. assert result.output == "Hello World!\nHello World!\n"
  32. def test_runner_with_stream():
  33. @click.command()
  34. def test():
  35. i = click.get_binary_stream("stdin")
  36. o = click.get_binary_stream("stdout")
  37. while 1:
  38. chunk = i.read(4096)
  39. if not chunk:
  40. break
  41. o.write(chunk)
  42. o.flush()
  43. runner = CliRunner()
  44. result = runner.invoke(test, input=ReasonableBytesIO(b"Hello World!\n"))
  45. assert not result.exception
  46. assert result.output == "Hello World!\n"
  47. runner = CliRunner(echo_stdin=True)
  48. result = runner.invoke(test, input=ReasonableBytesIO(b"Hello World!\n"))
  49. assert not result.exception
  50. assert result.output == "Hello World!\nHello World!\n"
  51. def test_prompts():
  52. @click.command()
  53. @click.option("--foo", prompt=True)
  54. def test(foo):
  55. click.echo("foo={}".format(foo))
  56. runner = CliRunner()
  57. result = runner.invoke(test, input="wau wau\n")
  58. assert not result.exception
  59. assert result.output == "Foo: wau wau\nfoo=wau wau\n"
  60. @click.command()
  61. @click.option("--foo", prompt=True, hide_input=True)
  62. def test(foo):
  63. click.echo("foo={}".format(foo))
  64. runner = CliRunner()
  65. result = runner.invoke(test, input="wau wau\n")
  66. assert not result.exception
  67. assert result.output == "Foo: \nfoo=wau wau\n"
  68. def test_getchar():
  69. @click.command()
  70. def continue_it():
  71. click.echo(click.getchar())
  72. runner = CliRunner()
  73. result = runner.invoke(continue_it, input="y")
  74. assert not result.exception
  75. assert result.output == "y\n"
  76. def test_catch_exceptions():
  77. class CustomError(Exception):
  78. pass
  79. @click.command()
  80. def cli():
  81. raise CustomError(1)
  82. runner = CliRunner()
  83. result = runner.invoke(cli)
  84. assert isinstance(result.exception, CustomError)
  85. assert type(result.exc_info) is tuple
  86. assert len(result.exc_info) == 3
  87. with pytest.raises(CustomError):
  88. runner.invoke(cli, catch_exceptions=False)
  89. CustomError = SystemExit
  90. result = runner.invoke(cli)
  91. assert result.exit_code == 1
  92. @pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")
  93. def test_with_color():
  94. @click.command()
  95. def cli():
  96. click.secho("hello world", fg="blue")
  97. runner = CliRunner()
  98. result = runner.invoke(cli)
  99. assert result.output == "hello world\n"
  100. assert not result.exception
  101. result = runner.invoke(cli, color=True)
  102. assert result.output == "{}\n".format(click.style("hello world", fg="blue"))
  103. assert not result.exception
  104. def test_with_color_but_pause_not_blocking():
  105. @click.command()
  106. def cli():
  107. click.pause()
  108. runner = CliRunner()
  109. result = runner.invoke(cli, color=True)
  110. assert not result.exception
  111. assert result.output == ""
  112. def test_exit_code_and_output_from_sys_exit():
  113. # See issue #362
  114. @click.command()
  115. def cli_string():
  116. click.echo("hello world")
  117. sys.exit("error")
  118. @click.command()
  119. @click.pass_context
  120. def cli_string_ctx_exit(ctx):
  121. click.echo("hello world")
  122. ctx.exit("error")
  123. @click.command()
  124. def cli_int():
  125. click.echo("hello world")
  126. sys.exit(1)
  127. @click.command()
  128. @click.pass_context
  129. def cli_int_ctx_exit(ctx):
  130. click.echo("hello world")
  131. ctx.exit(1)
  132. @click.command()
  133. def cli_float():
  134. click.echo("hello world")
  135. sys.exit(1.0)
  136. @click.command()
  137. @click.pass_context
  138. def cli_float_ctx_exit(ctx):
  139. click.echo("hello world")
  140. ctx.exit(1.0)
  141. @click.command()
  142. def cli_no_error():
  143. click.echo("hello world")
  144. runner = CliRunner()
  145. result = runner.invoke(cli_string)
  146. assert result.exit_code == 1
  147. assert result.output == "hello world\nerror\n"
  148. result = runner.invoke(cli_string_ctx_exit)
  149. assert result.exit_code == 1
  150. assert result.output == "hello world\nerror\n"
  151. result = runner.invoke(cli_int)
  152. assert result.exit_code == 1
  153. assert result.output == "hello world\n"
  154. result = runner.invoke(cli_int_ctx_exit)
  155. assert result.exit_code == 1
  156. assert result.output == "hello world\n"
  157. result = runner.invoke(cli_float)
  158. assert result.exit_code == 1
  159. assert result.output == "hello world\n1.0\n"
  160. result = runner.invoke(cli_float_ctx_exit)
  161. assert result.exit_code == 1
  162. assert result.output == "hello world\n1.0\n"
  163. result = runner.invoke(cli_no_error)
  164. assert result.exit_code == 0
  165. assert result.output == "hello world\n"
  166. def test_env():
  167. @click.command()
  168. def cli_env():
  169. click.echo("ENV={}".format(os.environ["TEST_CLICK_ENV"]))
  170. runner = CliRunner()
  171. env_orig = dict(os.environ)
  172. env = dict(env_orig)
  173. assert "TEST_CLICK_ENV" not in env
  174. env["TEST_CLICK_ENV"] = "some_value"
  175. result = runner.invoke(cli_env, env=env)
  176. assert result.exit_code == 0
  177. assert result.output == "ENV=some_value\n"
  178. assert os.environ == env_orig
  179. def test_stderr():
  180. @click.command()
  181. def cli_stderr():
  182. click.echo("stdout")
  183. click.echo("stderr", err=True)
  184. runner = CliRunner(mix_stderr=False)
  185. result = runner.invoke(cli_stderr)
  186. assert result.output == "stdout\n"
  187. assert result.stdout == "stdout\n"
  188. assert result.stderr == "stderr\n"
  189. runner_mix = CliRunner(mix_stderr=True)
  190. result_mix = runner_mix.invoke(cli_stderr)
  191. assert result_mix.output == "stdout\nstderr\n"
  192. assert result_mix.stdout == "stdout\nstderr\n"
  193. with pytest.raises(ValueError):
  194. result_mix.stderr
  195. @click.command()
  196. def cli_empty_stderr():
  197. click.echo("stdout")
  198. runner = CliRunner(mix_stderr=False)
  199. result = runner.invoke(cli_empty_stderr)
  200. assert result.output == "stdout\n"
  201. assert result.stdout == "stdout\n"
  202. assert result.stderr == ""
  203. @pytest.mark.parametrize(
  204. "args, expected_output",
  205. [
  206. (None, "bar\n"),
  207. ([], "bar\n"),
  208. ("", "bar\n"),
  209. (["--foo", "one two"], "one two\n"),
  210. ('--foo "one two"', "one two\n"),
  211. ],
  212. )
  213. def test_args(args, expected_output):
  214. @click.command()
  215. @click.option("--foo", default="bar")
  216. def cli_args(foo):
  217. click.echo(foo)
  218. runner = CliRunner()
  219. result = runner.invoke(cli_args, args=args)
  220. assert result.exit_code == 0
  221. assert result.output == expected_output
  222. def test_setting_prog_name_in_extra():
  223. @click.command()
  224. def cli():
  225. click.echo("ok")
  226. runner = CliRunner()
  227. result = runner.invoke(cli, prog_name="foobar")
  228. assert not result.exception
  229. assert result.output == "ok\n"