test_formatting.py
This file
Path
tests/test_formatting.py
315 lines · 7.7 KiB · Python
Last changed by
Merge pull request #1225 from antimirov/click-1018-show-defaultsStephen Rosenbefore adoption909956d
History of this fileTake it
1# -*- coding: utf-8 -*-2import click345def test_basic_functionality(runner):6 @click.command()7 def cli():8 """First paragraph.910 This is a very long second11 paragraph and not correctly12 wrapped but it will be rewrapped.1314 \b15 This is16 a paragraph17 without rewrapping.1819 \b20 121 222 32324 And this is a paragraph25 that will be rewrapped again.26 """2728 result = runner.invoke(cli, ['--help'], terminal_width=60)29 assert not result.exception30 assert result.output.splitlines() == [31 'Usage: cli [OPTIONS]',32 '',33 ' First paragraph.',34 '',35 ' This is a very long second paragraph and not correctly',36 ' wrapped but it will be rewrapped.',37 '',38 ' This is',39 ' a paragraph',40 ' without rewrapping.',41 '',42 ' 1',43 ' 2',44 ' 3',45 '',46 ' And this is a paragraph that will be rewrapped again.',47 '',48 'Options:',49 ' --help Show this message and exit.',50 ]515253def test_wrapping_long_options_strings(runner):54 @click.group()55 def cli():56 """Top level command57 """5859 @cli.group()60 def a_very_long():61 """Second level62 """6364 @a_very_long.command()65 @click.argument('first')66 @click.argument('second')67 @click.argument('third')68 @click.argument('fourth')69 @click.argument('fifth')70 @click.argument('sixth')71 def command():72 """A command.73 """7475 # 54 is chosen as a length where the second line is one character76 # longer than the maximum length.77 result = runner.invoke(cli, ['a-very-long', 'command', '--help'],78 terminal_width=54)79 assert not result.exception80 assert result.output.splitlines() == [81 'Usage: cli a-very-long command [OPTIONS] FIRST SECOND',82 ' THIRD FOURTH FIFTH',83 ' SIXTH',84 '',85 ' A command.',86 '',87 'Options:',88 ' --help Show this message and exit.',89 ]909192def test_wrapping_long_command_name(runner):93 @click.group()94 def cli():95 """Top level command96 """9798 @cli.group()99 def a_very_very_very_long():100 """Second level101 """102103 @a_very_very_very_long.command()104 @click.argument('first')105 @click.argument('second')106 @click.argument('third')107 @click.argument('fourth')108 @click.argument('fifth')109 @click.argument('sixth')110 def command():111 """A command.112 """113114 result = runner.invoke(cli, ['a-very-very-very-long', 'command', '--help'],115 terminal_width=54)116 assert not result.exception117 assert result.output.splitlines() == [118 'Usage: cli a-very-very-very-long command ',119 ' [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH',120 ' SIXTH',121 '',122 ' A command.',123 '',124 'Options:',125 ' --help Show this message and exit.',126 ]127128129def test_formatting_empty_help_lines(runner):130 @click.command()131 def cli():132 """Top level command133134 """135136 result = runner.invoke(cli, ['--help'])137 assert not result.exception138 assert result.output.splitlines() == [139 'Usage: cli [OPTIONS]',140 '',141 ' Top level command',142 '',143 '',144 '',145 'Options:',146 ' --help Show this message and exit.',147 ]148149150def test_formatting_usage_error(runner):151 @click.command()152 @click.argument('arg')153 def cmd(arg):154 click.echo('arg:' + arg)155156 result = runner.invoke(cmd, [])157 assert result.exit_code == 2158 assert result.output.splitlines() == [159 'Usage: cmd [OPTIONS] ARG',160 'Try "cmd --help" for help.',161 '',162 'Error: Missing argument "ARG".'163 ]164165166def test_formatting_usage_error_metavar_missing_arg(runner):167 """168 :author: @r-m-n169 Including attribution to #612170 """171 @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('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('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('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 ]251252def test_formatting_custom_type_metavar(runner):253 class MyType(click.ParamType):254 def get_metavar(self, param):255 return "MY_TYPE"256257 @click.command("foo")258 @click.help_option()259 @click.argument("param", type=MyType())260 def cmd(param):261 pass262263 result = runner.invoke(cmd, '--help')264 assert not result.exception265 assert result.output.splitlines() == [266 'Usage: foo [OPTIONS] MY_TYPE',267 '',268 'Options:',269 ' --help Show this message and exit.'270 ]271272273def test_truncating_docstring(runner):274 @click.command()275 @click.pass_context276 def cli(ctx):277 """First paragraph.278279 This is a very long second280 paragraph and not correctly281 wrapped but it will be rewrapped.282 \f283284 :param click.core.Context ctx: Click context.285 """286287 result = runner.invoke(cli, ['--help'], terminal_width=60)288 assert not result.exception289 assert result.output.splitlines() == [290 'Usage: cli [OPTIONS]',291 '',292 ' First paragraph.',293 '',294 ' This is a very long second paragraph and not correctly',295 ' wrapped but it will be rewrapped.',296 '',297 'Options:',298 ' --help Show this message and exit.',299 ]300301302def test_global_show_default(runner):303 @click.command(context_settings=dict(show_default=True))304 @click.option("-f", "in_file", default="out.txt", help="Output file name")305 def cli():306 pass307308 result = runner.invoke(cli, ['--help'],)309 assert result.output.splitlines() == [310 'Usage: cli [OPTIONS]',311 '',312 'Options:',313 ' -f TEXT Output file name [default: out.txt]',314 ' --help Show this message and exit. [default: False]'315 ]