File: test_extension.py

package info (click to toggle)
mkdocs-click 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: python: 715; sh: 17; makefile: 6
file content (189 lines) | stat: -rw-r--r-- 5,288 bytes parent folder | download
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
# (C) Datadog, Inc. 2020-present
# All rights reserved
# Licensed under the Apache license (see LICENSE)
from pathlib import Path
from textwrap import dedent

import pytest
from markdown import Markdown

import mkdocs_click

EXPECTED = (Path(__file__).parent / "app" / "expected.md").read_text()
EXPECTED_ENHANCED = (Path(__file__).parent / "app" / "expected-enhanced.md").read_text()
EXPECTED_SUB = (Path(__file__).parent / "app" / "expected-sub.md").read_text()
EXPECTED_SUB_ENHANCED = (Path(__file__).parent / "app" / "expected-sub-enhanced.md").read_text()


@pytest.mark.parametrize(
    "command, expected_name",
    [
        pytest.param("cli", "cli", id="cli-simple"),
        pytest.param("cli_named", "cli", id="cli-explicit-name"),
        pytest.param("group_named", "group", id="group-explicit-name"),
        pytest.param("group", "group", id="no-name"),
    ],
)
def test_extension(command, expected_name):
    """
    Markdown output for a relatively complex Click application is correct.
    """
    md = Markdown(extensions=[mkdocs_click.makeExtension()])

    source = dedent(
        f"""
        ::: mkdocs-click
            :module: tests.app.cli
            :command: {command}
        """
    ).rstrip()

    expected = EXPECTED.replace("cli", expected_name)

    assert md.convert(source) == md.convert(expected)


def test_prog_name():
    """
    The :prog_name: attribute determines the name to display for the command.
    """
    md = Markdown(extensions=[mkdocs_click.makeExtension()])

    source = dedent(
        """
        ::: mkdocs-click
            :module: tests.app.cli
            :command: cli
            :prog_name: custom
        """
    )

    expected = EXPECTED.replace("cli", "custom")

    assert md.convert(source) == md.convert(expected)


def test_depth():
    """
    The :depth: attribute increases the level of headers.
    """
    md = Markdown(extensions=[mkdocs_click.makeExtension()])

    source = dedent(
        """
        # CLI Reference

        ::: mkdocs-click
            :module: tests.app.cli
            :command: cli
            :depth: 1
        """
    )

    expected = f"# CLI Reference\n\n{EXPECTED.replace('# ', '## ')}"

    assert md.convert(source) == md.convert(expected)


@pytest.mark.parametrize("option", ["module", "command"])
def test_required_options(option):
    """
    The module and command options are required.
    """
    md = Markdown(extensions=[mkdocs_click.makeExtension()])

    source = dedent(
        """
        ::: mkdocs-click
            :module: tests.app.cli
            :command: cli
        """
    )

    source = source.replace(f":{option}:", ":somethingelse:")

    with pytest.raises(mkdocs_click.MkDocsClickException):
        md.convert(source)


def test_enhanced_titles():
    """
    If `attr_list` extension is registered, section titles are enhanced with full command paths.

    See: https://github.com/mkdocs/mkdocs-click/issues/35
    """
    md = Markdown(extensions=["attr_list"])
    # Register our extension as a second step, so that we see `attr_list`.
    # This is what MkDocs does, so there's no hidden usage constraint here.
    md.registerExtensions([mkdocs_click.makeExtension()], {})

    source = dedent(
        """
        ::: mkdocs-click
            :module: tests.app.cli
            :command: cli
        """
    )

    assert md.convert(source) == md.convert(EXPECTED_ENHANCED)


@pytest.mark.parametrize(
    "command, expected_name",
    [
        pytest.param("cli", "cli", id="cli-simple"),
        pytest.param("cli_named", "cli", id="cli-explicit-name"),
        pytest.param("group_named", "group", id="group-explicit-name"),
        pytest.param("group", "group", id="no-name"),
    ],
)
def test_extension_with_subcommand(command, expected_name):
    """
    Markdown output for a relatively complex Click application is correct.
    """
    md = Markdown(extensions=[mkdocs_click.makeExtension()])

    source = dedent(
        f"""
        ::: mkdocs-click
            :module: tests.app.cli
            :command: {command}
            :list_subcommands: True
        """
    )

    expected = EXPECTED_SUB.replace("cli", expected_name)

    assert md.convert(source) == md.convert(expected)


@pytest.mark.parametrize(
    "command, expected_name",
    [
        pytest.param("cli", "cli", id="cli-simple"),
        pytest.param("cli_named", "cli", id="cli-explicit-name"),
        pytest.param("group_named", "group", id="group-explicit-name"),
        pytest.param("group", "group", id="no-name"),
    ],
)
def test_enhanced_titles_with_subcommand(command, expected_name):
    """
    Markdown output for a relatively complex Click application is correct.
    """
    md = Markdown(extensions=["attr_list"])
    # Register our extension as a second step, so that we see `attr_list`.
    # This is what MkDocs does, so there's no hidden usage constraint here.
    md.registerExtensions([mkdocs_click.makeExtension()], {})

    source = dedent(
        f"""
        ::: mkdocs-click
            :module: tests.app.cli
            :command: {command}
            :list_subcommands: True
        """
    )

    expected = EXPECTED_SUB_ENHANCED.replace("cli", expected_name)

    assert md.convert(source) == md.convert(expected)