test_formatting.py
This file
Path
tests/test_formatting.py
358 lines · 9.0 KiB · Python
Last changed by
Merge pull request #1551 from yo-main/7.xDavid Lordbefore adoption4aabded
History of this fileTake it
1import click234def test_basic_functionality(runner):5 @click.command()6 def cli():7 """First paragraph.89 This is a very long second10 paragraph and not correctly11 wrapped but it will be rewrapped.1213 \b14 This is15 a paragraph16 without rewrapping.1718 \b19 120 221 32223 And this is a paragraph24 that will be rewrapped again.25 """2627 result = runner.invoke(cli, ["--help"], terminal_width=60)28 assert not result.exception29 assert result.output.splitlines() == [30 "Usage: cli [OPTIONS]",31 "",32 " First paragraph.",33 "",34 " This is a very long second paragraph and not correctly",35 " wrapped but it will be rewrapped.",36 "",37 " This is",38 " a paragraph",39 " without rewrapping.",40 "",41 " 1",42 " 2",43 " 3",44 "",45 " And this is a paragraph that will be rewrapped again.",46 "",47 "Options:",48 " --help Show this message and exit.",49 ]505152def test_wrapping_long_options_strings(runner):53 @click.group()54 def cli():55 """Top level command56 """5758 @cli.group()59 def a_very_long():60 """Second level61 """6263 @a_very_long.command()64 @click.argument("first")65 @click.argument("second")66 @click.argument("third")67 @click.argument("fourth")68 @click.argument("fifth")69 @click.argument("sixth")70 def command():71 """A command.72 """7374 # 54 is chosen as a length where the second line is one character75 # longer than the maximum length.76 result = runner.invoke(cli, ["a-very-long", "command", "--help"], terminal_width=54)77 assert not result.exception78 assert result.output.splitlines() == [79 "Usage: cli a-very-long command [OPTIONS] FIRST SECOND",80 " THIRD FOURTH FIFTH",81 " SIXTH",82 "",83 " A command.",84 "",85 "Options:",86 " --help Show this message and exit.",87 ]888990def test_wrapping_long_command_name(runner):91 @click.group()92 def cli():93 """Top level command94 """9596 @cli.group()97 def a_very_very_very_long():98 """Second level99 """100101 @a_very_very_very_long.command()102 @click.argument("first")103 @click.argument("second")104 @click.argument("third")105 @click.argument("fourth")106 @click.argument("fifth")107 @click.argument("sixth")108 def command():109 """A command.110 """111112 result = runner.invoke(113 cli, ["a-very-very-very-long", "command", "--help"], terminal_width=54114 )115 assert not result.exception116 assert result.output.splitlines() == [117 "Usage: cli a-very-very-very-long command ",118 " [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH",119 " SIXTH",120 "",121 " A command.",122 "",123 "Options:",124 " --help Show this message and exit.",125 ]126127128def test_formatting_empty_help_lines(runner):129 @click.command()130 def cli():131 """Top level command132133 """134135 result = runner.invoke(cli, ["--help"])136 assert not result.exception137 assert result.output.splitlines() == [138 "Usage: cli [OPTIONS]",139 "",140 " Top level command",141 "",142 "",143 "",144 "Options:",145 " --help Show this message and exit.",146 ]147148149def test_formatting_usage_error(runner):150 @click.command()151 @click.argument("arg")152 def cmd(arg):153 click.echo(f"arg:{arg}")154155 result = runner.invoke(cmd, [])156 assert result.exit_code == 2157 assert result.output.splitlines() == [158 "Usage: cmd [OPTIONS] ARG",159 "Try 'cmd --help' for help.",160 "",161 "Error: Missing argument 'ARG'.",162 ]163164165def test_formatting_usage_error_metavar_missing_arg(runner):166 """167 :author: @r-m-n168 Including attribution to #612169 """170171 @click.command()172 @click.argument("arg", metavar="metavar")173 def cmd(arg):174 pass175176 result = runner.invoke(cmd, [])177 assert result.exit_code == 2178 assert result.output.splitlines() == [179 "Usage: cmd [OPTIONS] metavar",180 "Try 'cmd --help' for help.",181 "",182 "Error: Missing argument 'metavar'.",183 ]184185186def test_formatting_usage_error_metavar_bad_arg(runner):187 @click.command()188 @click.argument("arg", type=click.INT, metavar="metavar")189 def cmd(arg):190 pass191192 result = runner.invoke(cmd, ["3.14"])193 assert result.exit_code == 2194 assert result.output.splitlines() == [195 "Usage: cmd [OPTIONS] metavar",196 "Try 'cmd --help' for help.",197 "",198 "Error: Invalid value for 'metavar': '3.14' is not a valid integer.",199 ]200201202def test_formatting_usage_error_nested(runner):203 @click.group()204 def cmd():205 pass206207 @cmd.command()208 @click.argument("bar")209 def foo(bar):210 click.echo(f"foo:{bar}")211212 result = runner.invoke(cmd, ["foo"])213 assert result.exit_code == 2214 assert result.output.splitlines() == [215 "Usage: cmd foo [OPTIONS] BAR",216 "Try 'cmd foo --help' for help.",217 "",218 "Error: Missing argument 'BAR'.",219 ]220221222def test_formatting_usage_error_no_help(runner):223 @click.command(add_help_option=False)224 @click.argument("arg")225 def cmd(arg):226 click.echo(f"arg:{arg}")227228 result = runner.invoke(cmd, [])229 assert result.exit_code == 2230 assert result.output.splitlines() == [231 "Usage: cmd [OPTIONS] ARG",232 "",233 "Error: Missing argument 'ARG'.",234 ]235236237def test_formatting_usage_custom_help(runner):238 @click.command(context_settings=dict(help_option_names=["--man"]))239 @click.argument("arg")240 def cmd(arg):241 click.echo(f"arg:{arg}")242243 result = runner.invoke(cmd, [])244 assert result.exit_code == 2245 assert result.output.splitlines() == [246 "Usage: cmd [OPTIONS] ARG",247 "Try 'cmd --man' for help.",248 "",249 "Error: Missing argument 'ARG'.",250 ]251252253def test_formatting_custom_type_metavar(runner):254 class MyType(click.ParamType):255 def get_metavar(self, param):256 return "MY_TYPE"257258 @click.command("foo")259 @click.help_option()260 @click.argument("param", type=MyType())261 def cmd(param):262 pass263264 result = runner.invoke(cmd, "--help")265 assert not result.exception266 assert result.output.splitlines() == [267 "Usage: foo [OPTIONS] MY_TYPE",268 "",269 "Options:",270 " --help Show this message and exit.",271 ]272273274def test_truncating_docstring(runner):275 @click.command()276 @click.pass_context277 def cli(ctx):278 """First paragraph.279280 This is a very long second281 paragraph and not correctly282 wrapped but it will be rewrapped.283 \f284285 :param click.core.Context ctx: Click context.286 """287288 result = runner.invoke(cli, ["--help"], terminal_width=60)289 assert not result.exception290 assert result.output.splitlines() == [291 "Usage: cli [OPTIONS]",292 "",293 " First paragraph.",294 "",295 " This is a very long second paragraph and not correctly",296 " wrapped but it will be rewrapped.",297 "",298 "Options:",299 " --help Show this message and exit.",300 ]301302303def test_global_show_default(runner):304 @click.command(context_settings=dict(show_default=True))305 @click.option("-f", "in_file", default="out.txt", help="Output file name")306 def cli():307 pass308309 result = runner.invoke(cli, ["--help"],)310 assert result.output.splitlines() == [311 "Usage: cli [OPTIONS]",312 "",313 "Options:",314 " -f TEXT Output file name [default: out.txt]",315 " --help Show this message and exit. [default: False]",316 ]317318319def test_formatting_usage_multiline_option_padding(runner):320 @click.command("foo")321 @click.option("--bar", help="This help message will be padded if it wraps.")322 def cli():323 pass324325 result = runner.invoke(cli, "--help", terminal_width=45)326 assert not result.exception327 assert result.output.splitlines() == [328 "Usage: foo [OPTIONS]",329 "",330 "Options:",331 " --bar TEXT This help message will be",332 " padded if it wraps.",333 "",334 " --help Show this message and exit.",335 ]336337338def test_formatting_usage_no_option_padding(runner):339 @click.command("foo")340 @click.option("--bar", help="This help message will be padded if it wraps.")341 def cli():342 pass343344 result = runner.invoke(cli, "--help", terminal_width=80)345 assert not result.exception346 assert result.output.splitlines() == [347 "Usage: foo [OPTIONS]",348 "",349 "Options:",350 " --bar TEXT This help message will be padded if it wraps.",351 " --help Show this message and exit.",352 ]353354355def test_formatting_with_options_metavar_empty(runner):356 cli = click.Command("cli", options_metavar="", params=[click.Argument(["var"])])357 result = runner.invoke(cli, ["--help"])358 assert "Usage: cli VAR\n" in result.output