1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
# (C) Datadog, Inc. 2020-present
# All rights reserved
# Licensed under the Apache license (see LICENSE)
from textwrap import dedent
from typing import cast
import click
import pytest
from mkdocs_click._docs import _show_options, make_command_docs
from mkdocs_click._exceptions import MkDocsClickException
@click.command()
@click.option("-d", "--debug", help="Include debug output")
def hello():
"""Hello, world!"""
@click.command()
@click.option("-d", "--debug", help="Include debug output")
def hello_escape_marker():
"""
\b
Hello, world!
"""
@click.command()
@click.option("-d", "--debug", help="Include debug output")
def hello_ascii_art():
"""
\b
______ __ __ ______ __ ___
/ || | | | / || |/ /
| ,----'| | | | | ,----'| ' /
| | | | | | | | | <
| `----.| `----.| | | `----.| . \\
\\______||_______||__| \\______||__|\\__\\
Hello, world!
"""
@click.command(context_settings={"help_option_names": []})
def hello_no_options():
"""
Hello, world!
"""
HELLO_EXPECTED = dedent(
"""
# hello
Hello, world!
**Usage:**
```text
hello [OPTIONS]
```
**Options:**
```text
-d, --debug TEXT Include debug output
--help Show this message and exit.
```
"""
).lstrip()
HELLO_EXPECTED_NO_OPTIONS = dedent(
"""
# hello
Hello, world!
**Usage:**
```text
hello [OPTIONS]
```
"""
).lstrip()
def test_basic():
output = "\n".join(make_command_docs("hello", hello))
assert output == HELLO_EXPECTED
def test_depth():
output = "\n".join(make_command_docs("hello", hello, depth=2))
assert output == HELLO_EXPECTED.replace("# ", "### ")
def test_prog_name():
output = "\n".join(make_command_docs("hello-world", hello))
assert output == HELLO_EXPECTED.replace("hello", "hello-world")
def test_style_invalid():
with pytest.raises(
MkDocsClickException,
match="invalid is not a valid option style, which must be either `plain` or `table`.",
):
list(make_command_docs("hello", hello, style="invalid"))
def test_basic_escape_marker():
output = "\n".join(make_command_docs("hello", hello_escape_marker))
assert output == HELLO_EXPECTED
def test_basic_ascii_art():
output = "\n".join(make_command_docs("hello", hello_ascii_art, remove_ascii_art=True))
assert output == HELLO_EXPECTED
def test_basic_no_options():
output = "\n".join(make_command_docs("hello", hello_no_options))
assert output == HELLO_EXPECTED_NO_OPTIONS
@click.command()
@click.option("-d", "--debug", help="Include debug output")
@click.option("--choice", type=click.Choice(["foo", "bar"]), default="foo")
@click.option("--date", type=click.DateTime(["%Y-%m-%d"]))
@click.option("--range-a", type=click.FloatRange(0, 1), default=0)
@click.option("--range-b", type=click.FloatRange(0))
@click.option("--range-c", type=click.FloatRange(None, 1), default=0)
@click.option("--flag/--no-flag")
def hello_full():
"""Hello, world!"""
HELLO_FULL_TABLE_EXPECTED = dedent(
"""
# hello
Hello, world!
**Usage:**
```text
hello [OPTIONS]
```
**Options:**
| Name | Type | Description | Default |
| ---- | ---- | ----------- | ------- |
| `-d`, `--debug` | text | Include debug output | None |
| `--choice` | choice (`foo` | `bar`) | N/A | `foo` |
| `--date` | datetime (`%Y-%m-%d`) | N/A | None |
| `--range-a` | float range (between `0` and `1`) | N/A | `0` |
| `--range-b` | float range (`0` and above) | N/A | None |
| `--range-c` | float range (`1` and below) | N/A | `0` |
| `--flag` / `--no-flag` | boolean | N/A | `False` |
| `--help` | boolean | Show this message and exit. | `False` |
"""
).lstrip()
@click.command()
def hello_minimal():
"""Hello, world!"""
HELLO_MINIMAL_TABLE_EXPECTED = dedent(
"""
# hello
Hello, world!
**Usage:**
```text
hello [OPTIONS]
```
**Options:**
| Name | Type | Description | Default |
| ---- | ---- | ----------- | ------- |
| `--help` | boolean | Show this message and exit. | `False` |
"""
).lstrip()
@pytest.mark.parametrize(
"command, expected",
[
pytest.param(hello_full, HELLO_FULL_TABLE_EXPECTED, id="full"),
pytest.param(hello_minimal, HELLO_MINIMAL_TABLE_EXPECTED, id="minimal"),
],
)
def test_style_table(command, expected):
output = "\n".join(make_command_docs("hello", command, style="table"))
assert output == expected
class GroupCLI(click.Group):
def list_commands(self, ctx):
return ["single-command"]
def get_command(self, ctx, name):
return hello
@pytest.mark.parametrize(
"group",
[
pytest.param(GroupCLI("group", help="Group help"), id="explicit-name"),
pytest.param(GroupCLI(help="Group help"), id="no-name"),
],
)
def test_custom_group(group):
"""
Custom `Group` objects are supported.
"""
expected = dedent(
"""
# group
Group help
**Usage:**
```text
group [OPTIONS] COMMAND [ARGS]...
```
**Options:**
```text
--help Show this message and exit.
```
## hello
Hello, world!
**Usage:**
```text
group hello [OPTIONS]
```
**Options:**
```text
-d, --debug TEXT Include debug output
--help Show this message and exit.
```
"""
).lstrip()
output = "\n".join(make_command_docs("group", group))
assert output == expected
@pytest.mark.parametrize(
"group",
[
pytest.param(GroupCLI("group", help="Group help"), id="explicit-name"),
pytest.param(GroupCLI(help="Group help"), id="no-name"),
],
)
def test_custom_group_with_list_subcommands(group):
"""
Custom `Group` objects are supported.
"""
expected = dedent(
"""
# group
Group help
**Usage:**
```text
group [OPTIONS] COMMAND [ARGS]...
```
**Options:**
```text
--help Show this message and exit.
```
**Subcommands**
- *hello*: Hello, world!
## hello
Hello, world!
**Usage:**
```text
group hello [OPTIONS]
```
**Options:**
```text
-d, --debug TEXT Include debug output
--help Show this message and exit.
```
"""
).lstrip()
output = "\n".join(make_command_docs("group", group, list_subcommands=True))
assert output == expected
@pytest.mark.parametrize("show_hidden", [True, False])
@pytest.mark.parametrize("style", ["plain", "table"])
def test_show_hidden_option(show_hidden, style):
@click.command()
@click.option("--hidden", hidden=True)
def _test_cmd(hidden):
"""Test cmd."""
output = "\n".join(
make_command_docs("_test_cmd", _test_cmd, style=style, show_hidden=show_hidden)
)
assert ("--hidden" in output) == show_hidden
@pytest.mark.parametrize("show_hidden", [True, False])
def test_show_hidden_command(show_hidden):
@click.command(hidden=True)
def _test_cmd():
"""Test cmd."""
output = "\n".join(make_command_docs("_test_cmd", _test_cmd, show_hidden=show_hidden))
assert (output != "") == show_hidden
@pytest.mark.parametrize("show_hidden", [True, False])
def test_show_hidden_group(show_hidden):
@click.group(hidden=True)
def _test_group():
"""Test group."""
@_test_group.command(hidden=False)
def _test_cmd():
"""Test cmd."""
output = "\n".join(make_command_docs("_test_group", _test_group, show_hidden=show_hidden))
assert (output != "") == show_hidden
def test_show_options():
@click.command()
@click.option("--hidden", hidden=True)
@click.option("--normal", hidden=False)
def _test_cmd(hidden, normal):
"""Test cmd."""
ctx = click.Context(cast(click.Command, _test_cmd), info_name="_test_cmd")
opt_hidden = cast(click.Option, ctx.command.params[0])
opt_normal = cast(click.Option, ctx.command.params[1])
assert opt_hidden.hidden
assert not opt_normal.hidden
with _show_options(ctx):
assert not opt_hidden.hidden
assert not opt_normal.hidden
assert opt_hidden.hidden
assert not opt_normal.hidden
|