File: test_config.py

package info (click to toggle)
mkdocs-include-markdown-plugin 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,316 kB
  • sloc: python: 5,360; makefile: 6
file content (247 lines) | stat: -rw-r--r-- 6,541 bytes parent folder | download | duplicates (2)
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
"""``include`` directive tests."""

import pytest

from mkdocs_include_markdown_plugin.event import on_page_markdown
from testing_helpers import unix_only


TESTS_ARGUMENTS = (
    'includer_schema',
    'content_to_include',
    'expected_result',
    'plugin',
)


def _run_test(
    includer_schema,
    content_to_include,
    expected_result,
    plugin,
    page,
    caplog,
    tmp_path,
):
    included_file = tmp_path / 'included.md'
    includer_file = tmp_path / 'includer.md'

    included_file.write_text(content_to_include)
    includer_file.write_text(
        content_to_include.replace('{filepath}', included_file.as_posix()),
    )

    # assert content
    page_content = includer_schema.replace(
        '{filepath}',
        included_file.as_posix(),
    )
    includer_file.write_text(page_content)

    expected_result = expected_result.replace(
        '{filepath}',
        included_file.as_posix(),
    )

    assert (
        on_page_markdown(
            page_content,
            page(includer_file),
            tmp_path,
            plugin,
        )
        == expected_result
    )

    assert len(caplog.records) == 0


@pytest.mark.parametrize(
    TESTS_ARGUMENTS,
    (
        # opening_tag and closing_tag
        pytest.param(
            '# Header\n\n{! include "{filepath}" !}\n',
            'This must be included.',
            '# Header\n\nThis must be included.\n',
            {'opening_tag': '{!', 'closing_tag': '!}'},
            id='custom-tag {! ... !}',
        ),
        pytest.param(
            '# Header\n\n{* include "{filepath}" *}\n',
            'This must be included.',
            '# Header\n\nThis must be included.\n',
            {'opening_tag': '{*', 'closing_tag': '*}'},
            id='custom-tag {* ... *}',
        ),
        pytest.param(
            '# Header\n\n#INC[ include "{filepath}" ]\n',
            'This must be included.',
            '# Header\n\nThis must be included.\n',
            {'opening_tag': '#INC[', 'closing_tag': ']'},
            id='custom-tag #INC[ ...]',
        ),
        pytest.param(
            '# Header\n\n.^$*+-?{}[]\\|():<>=!/#%,; include "{filepath}" }\n',
            'This must be included.',
            '# Header\n\nThis must be included.\n',
            {'opening_tag': '.^$*+-?{}[]\\|():<>=!/#%,;', 'closing_tag': '}'},
            id='custom-tag-all-escaped-char',
        ),

        # preserve_includer_indent
        pytest.param(
            '  {% include "{filepath}" %}',
            'foo\nbar\n',
            '  foo\n  bar\n',
            {},
            id='default-preserve_includer_indent',
        ),
        pytest.param(
            '  {% include "{filepath}" %}',
            'foo\nbar\n',
            '  foo\nbar\n',
            {'preserve_includer_indent': False},
            id='custom-preserve_includer_indent',
        ),

        # dedent
        pytest.param(
            '{% include "{filepath}" %}',
            'foo\n  bar\n',
            'foo\n  bar\n',
            {},
            id='default-dedent',
        ),
        pytest.param(
            '{% include "{filepath}" %}',
            '  foo\n  bar\n',
            'foo\nbar\n',
            {'dedent': True},
            id='custom-dedent',
        ),

        # trailing_newlines
        pytest.param(
            '{% include "{filepath}" %}',
            'foo\n\n\n',
            'foo\n\n\n',
            {},
            id='default-trailing_newlines',
        ),
        pytest.param(
            '{% include "{filepath}" %}',
            'foo\n\n\n',
            'foo',
            {'trailing_newlines': False},
            id='custom-trailing_newlines',
        ),

        # comments
        pytest.param(
            '{% include-markdown "{filepath}" %}',
            'foo\n',
            'foo\n',
            {},
            id='default-comments',
        ),
        pytest.param(
            '{% include-markdown "{filepath}" %}',
            'foo\n',
            '<!-- BEGIN INCLUDE {filepath} -->\nfoo\n\n<!-- END INCLUDE -->',
            {'comments': True},
            id='custom-comments',
        ),

        # directives
        pytest.param(
            '{% foo "{filepath}" %}bar\n',
            'baz\n',
            'baz\nbar\n',
            {'comments': False, 'directives': {'include-markdown': 'foo'}},
            id='custom-include-markdown-directive',
        ),
        pytest.param(
            '{% my-include "{filepath}" %}bar\n',
            'baz\n',
            'baz\nbar\n',
            {'comments': False, 'directives': {'include': 'my-include'}},
            id='custom-include-directive',
        ),
        pytest.param(
            '{% foo "{filepath}" %}bar\n{% include-markdown "{filepath}" %}',
            'baz\n',
            '{% foo "{filepath}" %}bar\nbaz\n',
            {'comments': False, 'directives': {'non-existent': 'foo'}},
            id='default-include-markdown-directive',
        ),
        pytest.param(
            '{% foo "{filepath}" %}bar\n{% include "{filepath}" %}',
            'baz\n',
            '{% foo "{filepath}" %}bar\nbaz\n',
            {'comments': False, 'directives': {'non-existent': 'foo'}},
            id='default-include-directive',
        ),
    ),
    indirect=['plugin'],
)
def test_config_options(
    includer_schema,
    content_to_include,
    expected_result,
    plugin,
    page,
    caplog,
    tmp_path,
):
    return _run_test(
        includer_schema,
        content_to_include,
        expected_result,
        plugin,
        page,
        caplog,
        tmp_path,
    )


@unix_only
@pytest.mark.parametrize(
    TESTS_ARGUMENTS,
    (
        # encoding
        pytest.param(
            '# Header\n\n{% include "{filepath}" %}',
            'bóg wąż wąską dróżką',
            '# Header\n\nbóg wąż wąską dróżką',
            {},
            id='default-encoding',
        ),
        pytest.param(
            '# Header\n\n{% include "{filepath}" %}',
            'bóg wąż wąską dróżką',
            '# Header\n\nbóg wąż wąską dróżką',
            {'encoding': 'cp1250'},
            id='custom-encoding',
        ),
    ),
    indirect=['plugin'],
)
def test_config_encoding_option_on_unix(
    includer_schema,
    content_to_include,
    expected_result,
    plugin,
    page,
    caplog,
    tmp_path,
):
    return _run_test(
        includer_schema,
        content_to_include,
        expected_result,
        plugin,
        page,
        caplog,
        tmp_path,
    )