Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than cd90e83 point at it.

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 1–26why, one step back
1import click
234def 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 """26

Merge branch '7.x'David Lordbefore adoption7239794

line 27why, one step back
27 result = runner.invoke(cli, ["--help"], terminal_width=60)

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 28–29why, one step back
28 assert not result.exception
29 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 30–48why, one step back
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.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 49–63why, one step back
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()

Merge branch '7.x'David Lordbefore adoption7239794

lines 64–69why, one step back
64 @click.argument("first")
65 @click.argument("second")66 @click.argument("third")67 @click.argument("fourth")68 @click.argument("fifth")69 @click.argument("sixth")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 70–75why, one step back
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.

Merge branch '7.x'David Lordbefore adoption7239794

line 76why, one step back
76 result = runner.invoke(cli, ["a-very-long", "command", "--help"], terminal_width=54)

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 77–78why, one step back
77 assert not result.exception
78 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 79–86why, one step back
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.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 87–101why, one step back
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()

Merge branch '7.x'David Lordbefore adoption7239794

lines 102–107why, one step back
102 @click.argument("first")
103 @click.argument("second")104 @click.argument("third")105 @click.argument("fourth")106 @click.argument("fifth")107 @click.argument("sixth")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 108–111why, one step back
108 def command():
109 """A command.110 """111

Merge branch '7.x'David Lordbefore adoption7239794

lines 112–114why, one step back
112 result = runner.invoke(
113 cli, ["a-very-very-very-long", "command", "--help"], terminal_width=54114 )

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 115–116why, one step back
115 assert not result.exception
116 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 117–124why, one step back
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.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 125–134why, one step back
125 ]
126127128def test_formatting_empty_help_lines(runner):129 @click.command()130 def cli():131 """Top level command132133 """134

Merge branch '7.x'David Lordbefore adoption7239794

line 135why, one step back
135 result = runner.invoke(cli, ["--help"])

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 136–137why, one step back
136 assert not result.exception
137 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 138–145why, one step back
138 "Usage: cli [OPTIONS]",
139 "",140 " Top level command",141 "",142 "",143 "",144 "Options:",145 " --help Show this message and exit.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 146–150why, one step back
146 ]
147148149def test_formatting_usage_error(runner):150 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 151why, one step back
151 @click.argument("arg")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 152why, one step back
152 def cmd(arg):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 153why, one step back
153 click.echo(f"arg:{arg}")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 154–157why, one step back
154
155 result = runner.invoke(cmd, [])156 assert result.exit_code == 2157 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 158–161why, one step back
158 "Usage: cmd [OPTIONS] ARG",
159 "Try 'cmd --help' for help.",160 "",161 "Error: Missing argument 'ARG'.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 162–169why, one step back
162 ]
163164165def test_formatting_usage_error_metavar_missing_arg(runner):166 """167 :author: @r-m-n168 Including attribution to #612169 """

Merge branch '7.x'David Lordbefore adoption7239794

line 170why, one step back
170

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 171why, one step back
171 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 172why, one step back
172 @click.argument("arg", metavar="metavar")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 173–178why, one step back
173 def cmd(arg):
174 pass175176 result = runner.invoke(cmd, [])177 assert result.exit_code == 2178 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 179–182why, one step back
179 "Usage: cmd [OPTIONS] metavar",
180 "Try 'cmd --help' for help.",181 "",182 "Error: Missing argument 'metavar'.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 183–187why, one step back
183 ]
184185186def test_formatting_usage_error_metavar_bad_arg(runner):187 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 188why, one step back
188 @click.argument("arg", type=click.INT, metavar="metavar")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 189–191why, one step back
189 def cmd(arg):
190 pass191

Merge branch '7.x'David Lordbefore adoption7239794

line 192why, one step back
192 result = runner.invoke(cmd, ["3.14"])

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 193–194why, one step back
193 assert result.exit_code == 2
194 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 195–197why, one step back
195 "Usage: cmd [OPTIONS] metavar",
196 "Try 'cmd --help' for help.",197 "",

Merge pull request #1687 from pallets/multiple-tupleDavid Lordbefore adoptionc067af7

line 198why, one step back
198 "Error: Invalid value for 'metavar': '3.14' is not a valid integer.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 199–207why, one step back
199 ]
200201202def test_formatting_usage_error_nested(runner):203 @click.group()204 def cmd():205 pass206207 @cmd.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 208why, one step back
208 @click.argument("bar")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 209why, one step back
209 def foo(bar):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 210why, one step back
210 click.echo(f"foo:{bar}")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 211why, one step back
211

Merge branch '7.x'David Lordbefore adoption7239794

line 212why, one step back
212 result = runner.invoke(cmd, ["foo"])

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 213–214why, one step back
213 assert result.exit_code == 2
214 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 215–218why, one step back
215 "Usage: cmd foo [OPTIONS] BAR",
216 "Try 'cmd foo --help' for help.",217 "",218 "Error: Missing argument 'BAR'.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 219–223why, one step back
219 ]
220221222def test_formatting_usage_error_no_help(runner):223 @click.command(add_help_option=False)

Merge branch '7.x'David Lordbefore adoption7239794

line 224why, one step back
224 @click.argument("arg")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 225why, one step back
225 def cmd(arg):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 226why, one step back
226 click.echo(f"arg:{arg}")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 227–230why, one step back
227
228 result = runner.invoke(cmd, [])229 assert result.exit_code == 2230 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 231–233why, one step back
231 "Usage: cmd [OPTIONS] ARG",
232 "",233 "Error: Missing argument 'ARG'.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 234–237why, one step back
234 ]
235236237def test_formatting_usage_custom_help(runner):

Merge branch '7.x'David Lordbefore adoption7239794

lines 238–239why, one step back
238 @click.command(context_settings=dict(help_option_names=["--man"]))
239 @click.argument("arg")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 240why, one step back
240 def cmd(arg):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 241why, one step back
241 click.echo(f"arg:{arg}")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 242–245why, one step back
242
243 result = runner.invoke(cmd, [])244 assert result.exit_code == 2245 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 246–249why, one step back
246 "Usage: cmd [OPTIONS] ARG",
247 "Try 'cmd --man' for help.",248 "",249 "Error: Missing argument 'ARG'.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 250–251why, one step back
250 ]
251

Merge branch '7.x'David Lordbefore adoption7239794

line 252why, one step back
252

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 253–263why, one step back
253def 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 pass263

Merge branch '7.x'David Lordbefore adoption7239794

line 264why, one step back
264 result = runner.invoke(cmd, "--help")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 265–266why, one step back
265 assert not result.exception
266 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 267–270why, one step back
267 "Usage: foo [OPTIONS] MY_TYPE",
268 "",269 "Options:",270 " --help Show this message and exit.",

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 271why, one step back
271 ]

Merge pull request #1091 from sirosen/truncate-helpDavid Lordbefore adoption011fd62

lines 272–287why, one step back
272
273274def 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 """287

Merge branch '7.x'David Lordbefore adoption7239794

line 288why, one step back
288 result = runner.invoke(cli, ["--help"], terminal_width=60)

Merge pull request #1091 from sirosen/truncate-helpDavid Lordbefore adoption011fd62

lines 289–290why, one step back
289 assert not result.exception
290 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 291–299why, one step back
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.",

Merge pull request #1091 from sirosen/truncate-helpDavid Lordbefore adoption011fd62

line 300why, one step back
300 ]

Merge branch '7.x'David Lordbefore adoptiona438322

lines 301–308why, one step back
301
302303def 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 pass308

Merge branch '7.x'David Lordbefore adoption7239794

line 309why, one step back
309 result = runner.invoke(cli, ["--help"],)

Merge branch '7.x'David Lordbefore adoptiona438322

line 310why, one step back
310 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 311–315why, one step back
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]",

Merge branch '7.x'David Lordbefore adoptiona438322

line 316why, one step back
316 ]

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 317–319why, one step back
317
318319def test_formatting_usage_multiline_option_padding(runner):

Merge branch '7.x'David Lordbefore adoption7239794

lines 320–321why, one step back
320 @click.command("foo")
321 @click.option("--bar", help="This help message will be padded if it wraps.")

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 322–324why, one step back
322 def cli():
323 pass324

Merge branch '7.x'David Lordbefore adoption7239794

line 325why, one step back
325 result = runner.invoke(cli, "--help", terminal_width=45)

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 326–327why, one step back
326 assert not result.exception
327 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 328–334why, one step back
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.",

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 335–338why, one step back
335 ]
336337338def test_formatting_usage_no_option_padding(runner):

Merge branch '7.x'David Lordbefore adoption7239794

lines 339–340why, one step back
339 @click.command("foo")
340 @click.option("--bar", help="This help message will be padded if it wraps.")

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 341–343why, one step back
341 def cli():
342 pass343

Merge branch '7.x'David Lordbefore adoption7239794

line 344why, one step back
344 result = runner.invoke(cli, "--help", terminal_width=80)

Merge branch '7.x'David Lordbefore adoption8b971f7

lines 345–346why, one step back
345 assert not result.exception
346 assert result.output.splitlines() == [

Merge branch '7.x'David Lordbefore adoption7239794

lines 347–351why, one step back
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.",

Merge branch '7.x'David Lordbefore adoption8b971f7

line 352why, one step back
352 ]

Merge pull request #1551 from yo-main/7.xDavid Lordbefore adoption4aabded

lines 353–358why, one step back
353
354355def 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