-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_glob_include.py
206 lines (180 loc) · 5.51 KB
/
test_glob_include.py
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
"""Tests for multiple inclusions across directives."""
import os
import pytest
from mkdocs_include_markdown_plugin.event import on_page_markdown
from testing_helpers import parametrize_directives, unix_only
@unix_only
def test_glob_include_absolute(page, tmp_path, plugin):
includer_file = tmp_path / 'includer.txt'
included_01_file = tmp_path / 'included_01.txt'
included_02_file = tmp_path / 'included_02.txt'
includer_file_content = f'''foo
{{%
include "./included*.txt"
%}}
<!-- with absolute path -->
{{%
include "{tmp_path}{os.sep}included*.txt"
%}}
'''
included_01_content = 'bar'
included_02_content = 'baz'
includer_file.write_text(includer_file_content)
included_01_file.write_text(included_01_content)
included_02_file.write_text(included_02_content)
expected_result = '''foo
barbaz
<!-- with absolute path -->
barbaz
'''
assert on_page_markdown(
includer_file_content, page(includer_file), tmp_path, plugin,
) == expected_result
@parametrize_directives
@pytest.mark.parametrize(
(
'includer_content',
'expected_warnings_schemas',
),
(
pytest.param(
'''{%
{directive} "./included*.txt"
start="<!-- start-2 -->"
end="<!-- end-2 -->"
comments=false
%}
{%
{directive} "./included*.txt"
start="<!-- start-1 -->"
end="<!-- end-1 -->"
comments=false
%}
''',
[],
id='start-end',
),
pytest.param(
'''{%
{directive} "./included*.txt"
end="<!-- end-2 -->"
comments=false
%}
''',
[],
id='end',
),
# both start and end specified but not found in files to include
pytest.param(
'''{%
{directive} "./included*.txt"
start="<!-- start-not-found-2 -->"
end="<!-- end-not-found-2 -->"
comments=false
%}
{%
{directive} "./included*.txt"
start="<!-- start-not-found-1 -->"
end="<!-- end-not-found-1 -->"
comments=false
%}
''',
[
{
'delimiter_name': 'start',
'delimiter_value': '<!-- start-not-found-2 -->',
'relative_path': '{includer_file}',
'line_number': 3,
'plural_suffix': 's',
'readable_files_to_include': (
'{included_file_01}, {included_file_02}'
),
},
{
'delimiter_name': 'end',
'delimiter_value': '<!-- end-not-found-2 -->',
'relative_path': '{includer_file}',
'line_number': 3,
'plural_suffix': 's',
'readable_files_to_include': (
'{included_file_01}, {included_file_02}'
),
},
{
'delimiter_name': 'start',
'delimiter_value': '<!-- start-not-found-1 -->',
'relative_path': '{includer_file}',
'line_number': 10,
'plural_suffix': 's',
'readable_files_to_include': (
'{included_file_01}, {included_file_02}'
),
},
{
'delimiter_name': 'end',
'delimiter_value': '<!-- end-not-found-1 -->',
'relative_path': '{includer_file}',
'line_number': 10,
'plural_suffix': 's',
'readable_files_to_include': (
'{included_file_01}, {included_file_02}'
),
},
],
id='start-end-not-found',
),
),
)
def test_glob_include(
includer_content,
directive,
expected_warnings_schemas,
page,
caplog,
tmp_path,
plugin,
):
includer_file = tmp_path / 'includer.txt'
included_01_file = tmp_path / 'included_01.txt'
included_02_file = tmp_path / 'included_02.txt'
includer_file_content = f'''foo
{includer_content.replace('{directive}', directive)}
'''
included_01_content = '''This 01 must appear only without specifying start.
<!-- start-1 -->
bar
<!-- end-1 -->
This 01 must appear only without specifying end.
'''
included_02_content = '''This 02 must appear only without specifying start.
<!-- start-2 -->
baz
<!-- end-2 -->
This 02 must appear only without specifying end.
'''
includer_file.write_text(includer_file_content)
included_01_file.write_text(included_01_content)
included_02_file.write_text(included_02_content)
on_page_markdown(
includer_file_content, page(includer_file), tmp_path, plugin,
)
# assert warnings
expected_warnings_schemas = expected_warnings_schemas or []
for warning in expected_warnings_schemas:
warning['directive'] = directive
warning['relative_path'] = warning['relative_path'].replace(
'{includer_file}', str(includer_file.relative_to(tmp_path)),
)
warning['readable_files_to_include'] = warning[
'readable_files_to_include'
].replace(
'{included_file_01}',
str(included_01_file.relative_to(tmp_path)),
).replace(
'{included_file_02}',
str(included_02_file.relative_to(tmp_path)),
)
for i, warning in enumerate(expected_warnings_schemas):
for key in warning:
assert getattr(caplog.records[i], key) == warning[key]
assert len(expected_warnings_schemas) == len(caplog.records)