Debian Codegraph @

tests/test_formatting.py

raw
1 import click
2
3
4 def test_basic_functionality(runner):
5 @click.command()
6 def cli():
7 """First paragraph.
8
9 This is a very long second
10 paragraph and not correctly
11 wrapped but it will be rewrapped.
12
13 \b
14 This is
15 a paragraph
16 without rewrapping.
17
18 \b
19 1
20 2
21 3
22
23 And this is a paragraph
24 that will be rewrapped again.
25 """
26
27 result = runner.invoke(cli, ["--help"], terminal_width=60)
28 assert not result.exception
29 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 ]
50
51
52 def test_wrapping_long_options_strings(runner):
53 @click.group()
54 def cli():
55 """Top level command"""
56
57 @cli.group()
58 def a_very_long():
59 """Second level"""
60
61 @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."""
70
71 # 54 is chosen as a length where the second line is one character
72 # longer than the maximum length.
73 result = runner.invoke(cli, ["a-very-long", "command", "--help"], terminal_width=54)
74 assert not result.exception
75 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 ]
85
86
87 def test_wrapping_long_command_name(runner):
88 @click.group()
89 def cli():
90 """Top level command"""
91
92 @cli.group()
93 def a_very_very_very_long():
94 """Second level"""
95
96 @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."""
105
106 result = runner.invoke(
107 cli, ["a-very-very-very-long", "command", "--help"], terminal_width=54
108 )
109 assert not result.exception
110 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 ]
120
121
122 def test_formatting_empty_help_lines(runner):
123 @click.command()
124 def cli():
125 # fmt: off
126 """Top level command
127
128 """
129 # fmt: on
130
131 result = runner.invoke(cli, ["--help"])
132 assert not result.exception
133 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 ]
143
144
145 def test_formatting_usage_error(runner):
146 @click.command()
147 @click.argument("arg")
148 def cmd(arg):
149 click.echo(f"arg:{arg}")
150
151 result = runner.invoke(cmd, [])
152 assert result.exit_code == 2
153 assert result.output.splitlines() == [
154 "Usage: cmd [OPTIONS] ARG",
155 "Try 'cmd --help' for help.",
156 "",
157 "Error: Missing argument 'ARG'.",
158 ]
159
160
161 def test_formatting_usage_error_metavar_missing_arg(runner):
162 """
163 :author: @r-m-n
164 Including attribution to #612
165 """
166
167 @click.command()
168 @click.argument("arg", metavar="metavar")
169 def cmd(arg):
170 pass
171
172 result = runner.invoke(cmd, [])
173 assert result.exit_code == 2
174 assert result.output.splitlines() == [
175 "Usage: cmd [OPTIONS] metavar",
176 "Try 'cmd --help' for help.",
177 "",
178 "Error: Missing argument 'metavar'.",
179 ]
180
181
182 def 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 pass
187
188 result = runner.invoke(cmd, ["3.14"])
189 assert result.exit_code == 2
190 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 ]
196
197
198 def test_formatting_usage_error_nested(runner):
199 @click.group()
200 def cmd():
201 pass
202
203 @cmd.command()
204 @click.argument("bar")
205 def foo(bar):
206 click.echo(f"foo:{bar}")
207
208 result = runner.invoke(cmd, ["foo"])
209 assert result.exit_code == 2
210 assert result.output.splitlines() == [
211 "Usage: cmd foo [OPTIONS] BAR",
212 "Try 'cmd foo --help' for help.",
213 "",
214 "Error: Missing argument 'BAR'.",
215 ]
216
217
218 def 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}")
223
224 result = runner.invoke(cmd, [])
225 assert result.exit_code == 2
226 assert result.output.splitlines() == [
227 "Usage: cmd [OPTIONS] ARG",
228 "",
229 "Error: Missing argument 'ARG'.",
230 ]
231
232
233 def 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}")
238
239 result = runner.invoke(cmd, [])
240 assert result.exit_code == 2
241 assert result.output.splitlines() == [
242 "Usage: cmd [OPTIONS] ARG",
243 "Try 'cmd --man' for help.",
244 "",
245 "Error: Missing argument 'ARG'.",
246 ]
247
248
249 def test_formatting_custom_type_metavar(runner):
250 class MyType(click.ParamType):
251 def get_metavar(self, param: click.Parameter, ctx: click.Context):
252 return "MY_TYPE"
253
254 @click.command("foo")
255 @click.help_option()
256 @click.argument("param", type=MyType())
257 def cmd(param):
258 pass
259
260 result = runner.invoke(cmd, "--help")
261 assert not result.exception
262 assert result.output.splitlines() == [
263 "Usage: foo [OPTIONS] MY_TYPE",
264 "",
265 "Options:",
266 " --help Show this message and exit.",
267 ]
268
269
270 def test_truncating_docstring(runner):
271 @click.command()
272 @click.pass_context
273 def cli(ctx):
274 """First paragraph.
275
276 This is a very long second
277 paragraph and not correctly
278 wrapped but it will be rewrapped.
279 \f
280
281 :param click.core.Context ctx: Click context.
282 """
283
284 result = runner.invoke(cli, ["--help"], terminal_width=60)
285 assert not result.exception
286 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 ]
297
298
299 def test_truncating_docstring_no_help(runner):
300 @click.command()
301 @click.pass_context
302 def cli(ctx):
303 """
304 \f
305
306 This text should be truncated.
307 """
308
309 result = runner.invoke(cli, ["--help"], terminal_width=60)
310 assert not result.exception
311 assert result.output.splitlines() == [
312 "Usage: cli [OPTIONS]",
313 "",
314 "Options:",
315 " --help Show this message and exit.",
316 ]
317
318
319 def test_removing_multiline_marker(runner):
320 @click.group()
321 def cli():
322 pass
323
324 @cli.command()
325 def cmd1():
326 """\b
327 This is command with a multiline help text
328 which should not be rewrapped.
329 The output of the short help text should
330 not contain the multiline marker.
331 """
332 pass
333
334 result = runner.invoke(cli, ["--help"])
335 assert "\b" not in result.output
336
337
338 def test_global_show_default(runner):
339 @click.command(context_settings=dict(show_default=True))
340 @click.option("-f", "in_file", default="out.txt", help="Output file name")
341 def cli():
342 pass
343
344 result = runner.invoke(cli, ["--help"])
345 # the default to "--help" is not shown because it is False
346 assert result.output.splitlines() == [
347 "Usage: cli [OPTIONS]",
348 "",
349 "Options:",
350 " -f TEXT Output file name [default: out.txt]",
351 " --help Show this message and exit.",
352 ]
353
354
355 def 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
359
360
361 def test_help_formatter_write_text():
362 text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
363 formatter = click.HelpFormatter(width=len(" Lorem ipsum dolor sit amet,"))
364 formatter.current_indent = 2
365 formatter.write_text(text)
366 actual = formatter.getvalue()
367 expected = " Lorem ipsum dolor sit amet,\n consectetur adipiscing elit\n"
368 assert actual == expected