test_formatting.py
This file
Path
tests/test_formatting.py
348 lines · 8.8 KiB · Python
Last changed by
Merge pull request #1998 from MLH-Fellowship/1971-hide-defaultsDavid Lordbefore adoption5d759e0
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 command"""5657 @cli.group()58 def a_very_long():59 """Second level"""6061 @a_very_long.command()62 @click.argument("first")63 @click.argument("second")64 @click.argument("third")65 @click.argument("fourth")66 @click.argument("fifth")67 @click.argument("sixth")68 def command():69 """A command."""7071 # 54 is chosen as a length where the second line is one character72 # longer than the maximum length.73 result = runner.invoke(cli, ["a-very-long", "command", "--help"], terminal_width=54)74 assert not result.exception75 assert result.output.splitlines() == [76 "Usage: cli a-very-long command [OPTIONS] FIRST SECOND",77 " THIRD FOURTH FIFTH",78 " SIXTH",79 "",80 " A command.",81 "",82 "Options:",83 " --help Show this message and exit.",84 ]858687def test_wrapping_long_command_name(runner):88 @click.group()89 def cli():90 """Top level command"""9192 @cli.group()93 def a_very_very_very_long():94 """Second level"""9596 @a_very_very_very_long.command()97 @click.argument("first")98 @click.argument("second")99 @click.argument("third")100 @click.argument("fourth")101 @click.argument("fifth")102 @click.argument("sixth")103 def command():104 """A command."""105106 result = runner.invoke(107 cli, ["a-very-very-very-long", "command", "--help"], terminal_width=54108 )109 assert not result.exception110 assert result.output.splitlines() == [111 "Usage: cli a-very-very-very-long command ",112 " [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH",113 " SIXTH",114 "",115 " A command.",116 "",117 "Options:",118 " --help Show this message and exit.",119 ]120121122def test_formatting_empty_help_lines(runner):123 @click.command()124 def cli():125 # fmt: off126 """Top level command127128 """129 # fmt: on130131 result = runner.invoke(cli, ["--help"])132 assert not result.exception133 assert result.output.splitlines() == [134 "Usage: cli [OPTIONS]",135 "",136 " Top level command",137 "",138 "",139 "",140 "Options:",141 " --help Show this message and exit.",142 ]143144145def test_formatting_usage_error(runner):146 @click.command()147 @click.argument("arg")148 def cmd(arg):149 click.echo(f"arg:{arg}")150151 result = runner.invoke(cmd, [])152 assert result.exit_code == 2153 assert result.output.splitlines() == [154 "Usage: cmd [OPTIONS] ARG",155 "Try 'cmd --help' for help.",156 "",157 "Error: Missing argument 'ARG'.",158 ]159160161def test_formatting_usage_error_metavar_missing_arg(runner):162 """163 :author: @r-m-n164 Including attribution to #612165 """166167 @click.command()168 @click.argument("arg", metavar="metavar")169 def cmd(arg):170 pass171172 result = runner.invoke(cmd, [])173 assert result.exit_code == 2174 assert result.output.splitlines() == [175 "Usage: cmd [OPTIONS] metavar",176 "Try 'cmd --help' for help.",177 "",178 "Error: Missing argument 'metavar'.",179 ]180181182def test_formatting_usage_error_metavar_bad_arg(runner):183 @click.command()184 @click.argument("arg", type=click.INT, metavar="metavar")185 def cmd(arg):186 pass187188 result = runner.invoke(cmd, ["3.14"])189 assert result.exit_code == 2190 assert result.output.splitlines() == [191 "Usage: cmd [OPTIONS] metavar",192 "Try 'cmd --help' for help.",193 "",194 "Error: Invalid value for 'metavar': '3.14' is not a valid integer.",195 ]196197198def test_formatting_usage_error_nested(runner):199 @click.group()200 def cmd():201 pass202203 @cmd.command()204 @click.argument("bar")205 def foo(bar):206 click.echo(f"foo:{bar}")207208 result = runner.invoke(cmd, ["foo"])209 assert result.exit_code == 2210 assert result.output.splitlines() == [211 "Usage: cmd foo [OPTIONS] BAR",212 "Try 'cmd foo --help' for help.",213 "",214 "Error: Missing argument 'BAR'.",215 ]216217218def test_formatting_usage_error_no_help(runner):219 @click.command(add_help_option=False)220 @click.argument("arg")221 def cmd(arg):222 click.echo(f"arg:{arg}")223224 result = runner.invoke(cmd, [])225 assert result.exit_code == 2226 assert result.output.splitlines() == [227 "Usage: cmd [OPTIONS] ARG",228 "",229 "Error: Missing argument 'ARG'.",230 ]231232233def test_formatting_usage_custom_help(runner):234 @click.command(context_settings=dict(help_option_names=["--man"]))235 @click.argument("arg")236 def cmd(arg):237 click.echo(f"arg:{arg}")238239 result = runner.invoke(cmd, [])240 assert result.exit_code == 2241 assert result.output.splitlines() == [242 "Usage: cmd [OPTIONS] ARG",243 "Try 'cmd --man' for help.",244 "",245 "Error: Missing argument 'ARG'.",246 ]247248249def test_formatting_custom_type_metavar(runner):250 class MyType(click.ParamType):251 def get_metavar(self, param):252 return "MY_TYPE"253254 @click.command("foo")255 @click.help_option()256 @click.argument("param", type=MyType())257 def cmd(param):258 pass259260 result = runner.invoke(cmd, "--help")261 assert not result.exception262 assert result.output.splitlines() == [263 "Usage: foo [OPTIONS] MY_TYPE",264 "",265 "Options:",266 " --help Show this message and exit.",267 ]268269270def test_truncating_docstring(runner):271 @click.command()272 @click.pass_context273 def cli(ctx):274 """First paragraph.275276 This is a very long second277 paragraph and not correctly278 wrapped but it will be rewrapped.279 \f280281 :param click.core.Context ctx: Click context.282 """283284 result = runner.invoke(cli, ["--help"], terminal_width=60)285 assert not result.exception286 assert result.output.splitlines() == [287 "Usage: cli [OPTIONS]",288 "",289 " First paragraph.",290 "",291 " This is a very long second paragraph and not correctly",292 " wrapped but it will be rewrapped.",293 "",294 "Options:",295 " --help Show this message and exit.",296 ]297298299def test_removing_multiline_marker(runner):300 @click.group()301 def cli():302 pass303304 @cli.command()305 def cmd1():306 """\b307 This is command with a multiline help text308 which should not be rewrapped.309 The output of the short help text should310 not contain the multiline marker.311 """312 pass313314 result = runner.invoke(cli, ["--help"])315 assert "\b" not in result.output316317318def test_global_show_default(runner):319 @click.command(context_settings=dict(show_default=True))320 @click.option("-f", "in_file", default="out.txt", help="Output file name")321 def cli():322 pass323324 result = runner.invoke(cli, ["--help"])325 # the default to "--help" is not shown because it is False326 assert result.output.splitlines() == [327 "Usage: cli [OPTIONS]",328 "",329 "Options:",330 " -f TEXT Output file name [default: out.txt]",331 " --help Show this message and exit.",332 ]333334335def test_formatting_with_options_metavar_empty(runner):336 cli = click.Command("cli", options_metavar="", params=[click.Argument(["var"])])337 result = runner.invoke(cli, ["--help"])338 assert "Usage: cli VAR\n" in result.output339340341def test_help_formatter_write_text():342 text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"343 formatter = click.HelpFormatter(width=len(" Lorem ipsum dolor sit amet,"))344 formatter.current_indent = 2345 formatter.write_text(text)346 actual = formatter.getvalue()347 expected = " Lorem ipsum dolor sit amet,\n consectetur adipiscing elit\n"348 assert actual == expected