test_chain.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import sys
  2. import pytest
  3. import click
  4. def debug():
  5. click.echo(
  6. "{}={}".format(
  7. sys._getframe(1).f_code.co_name, "|".join(click.get_current_context().args)
  8. )
  9. )
  10. def test_basic_chaining(runner):
  11. @click.group(chain=True)
  12. def cli():
  13. pass
  14. @cli.command("sdist")
  15. def sdist():
  16. click.echo("sdist called")
  17. @cli.command("bdist")
  18. def bdist():
  19. click.echo("bdist called")
  20. result = runner.invoke(cli, ["bdist", "sdist", "bdist"])
  21. assert not result.exception
  22. assert result.output.splitlines() == [
  23. "bdist called",
  24. "sdist called",
  25. "bdist called",
  26. ]
  27. def test_chaining_help(runner):
  28. @click.group(chain=True)
  29. def cli():
  30. """ROOT HELP"""
  31. pass
  32. @cli.command("sdist")
  33. def sdist():
  34. """SDIST HELP"""
  35. click.echo("sdist called")
  36. @cli.command("bdist")
  37. def bdist():
  38. """BDIST HELP"""
  39. click.echo("bdist called")
  40. result = runner.invoke(cli, ["--help"])
  41. assert not result.exception
  42. assert "COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]..." in result.output
  43. assert "ROOT HELP" in result.output
  44. result = runner.invoke(cli, ["sdist", "--help"])
  45. assert not result.exception
  46. assert "SDIST HELP" in result.output
  47. result = runner.invoke(cli, ["bdist", "--help"])
  48. assert not result.exception
  49. assert "BDIST HELP" in result.output
  50. result = runner.invoke(cli, ["bdist", "sdist", "--help"])
  51. assert not result.exception
  52. assert "SDIST HELP" in result.output
  53. def test_chaining_with_options(runner):
  54. @click.group(chain=True)
  55. def cli():
  56. pass
  57. @cli.command("sdist")
  58. @click.option("--format")
  59. def sdist(format):
  60. click.echo("sdist called {}".format(format))
  61. @cli.command("bdist")
  62. @click.option("--format")
  63. def bdist(format):
  64. click.echo("bdist called {}".format(format))
  65. result = runner.invoke(cli, ["bdist", "--format=1", "sdist", "--format=2"])
  66. assert not result.exception
  67. assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]
  68. def test_chaining_with_arguments(runner):
  69. @click.group(chain=True)
  70. def cli():
  71. pass
  72. @cli.command("sdist")
  73. @click.argument("format")
  74. def sdist(format):
  75. click.echo("sdist called {}".format(format))
  76. @cli.command("bdist")
  77. @click.argument("format")
  78. def bdist(format):
  79. click.echo("bdist called {}".format(format))
  80. result = runner.invoke(cli, ["bdist", "1", "sdist", "2"])
  81. assert not result.exception
  82. assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]
  83. def test_pipeline(runner):
  84. @click.group(chain=True, invoke_without_command=True)
  85. @click.option("-i", "--input", type=click.File("r"))
  86. def cli(input):
  87. pass
  88. @cli.resultcallback()
  89. def process_pipeline(processors, input):
  90. iterator = (x.rstrip("\r\n") for x in input)
  91. for processor in processors:
  92. iterator = processor(iterator)
  93. for item in iterator:
  94. click.echo(item)
  95. @cli.command("uppercase")
  96. def make_uppercase():
  97. def processor(iterator):
  98. for line in iterator:
  99. yield line.upper()
  100. return processor
  101. @cli.command("strip")
  102. def make_strip():
  103. def processor(iterator):
  104. for line in iterator:
  105. yield line.strip()
  106. return processor
  107. result = runner.invoke(cli, ["-i", "-"], input="foo\nbar")
  108. assert not result.exception
  109. assert result.output.splitlines() == ["foo", "bar"]
  110. result = runner.invoke(cli, ["-i", "-", "strip"], input="foo \n bar")
  111. assert not result.exception
  112. assert result.output.splitlines() == ["foo", "bar"]
  113. result = runner.invoke(cli, ["-i", "-", "strip", "uppercase"], input="foo \n bar")
  114. assert not result.exception
  115. assert result.output.splitlines() == ["FOO", "BAR"]
  116. def test_args_and_chain(runner):
  117. @click.group(chain=True)
  118. def cli():
  119. debug()
  120. @cli.command()
  121. def a():
  122. debug()
  123. @cli.command()
  124. def b():
  125. debug()
  126. @cli.command()
  127. def c():
  128. debug()
  129. result = runner.invoke(cli, ["a", "b", "c"])
  130. assert not result.exception
  131. assert result.output.splitlines() == ["cli=", "a=", "b=", "c="]
  132. def test_multicommand_arg_behavior(runner):
  133. with pytest.raises(RuntimeError):
  134. @click.group(chain=True)
  135. @click.argument("forbidden", required=False)
  136. def bad_cli():
  137. pass
  138. with pytest.raises(RuntimeError):
  139. @click.group(chain=True)
  140. @click.argument("forbidden", nargs=-1)
  141. def bad_cli2():
  142. pass
  143. @click.group(chain=True)
  144. @click.argument("arg")
  145. def cli(arg):
  146. click.echo("cli:{}".format(arg))
  147. @cli.command()
  148. def a():
  149. click.echo("a")
  150. result = runner.invoke(cli, ["foo", "a"])
  151. assert not result.exception
  152. assert result.output.splitlines() == ["cli:foo", "a"]
  153. @pytest.mark.xfail
  154. def test_multicommand_chaining(runner):
  155. @click.group(chain=True)
  156. def cli():
  157. debug()
  158. @cli.group()
  159. def l1a():
  160. debug()
  161. @l1a.command()
  162. def l2a():
  163. debug()
  164. @l1a.command()
  165. def l2b():
  166. debug()
  167. @cli.command()
  168. def l1b():
  169. debug()
  170. result = runner.invoke(cli, ["l1a", "l2a", "l1b"])
  171. assert not result.exception
  172. assert result.output.splitlines() == ["cli=", "l1a=", "l2a=", "l1b="]