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
|
"""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
@parametrize_directives
def test_glob_include_absolute(directive, 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
{{%
{directive} "./included*.txt"
%}}
<!-- with absolute path -->
{{%
{directive} "{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
@unix_only
@parametrize_directives
def test_glob_include_fallback(directive, page, tmp_path, plugin):
includer_file = tmp_path / 'includer.txt'
includes_dir = tmp_path / 'includes'
includes_dir.mkdir()
included_01_file = includes_dir / 'included_01.txt'
included_02_file = includes_dir / 'included_02.txt'
includer_file_content = f'''foo
{{%
{directive} "includes/*.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
'''
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 -->"
%}
{%
{directive} "./included*.txt"
start="<!-- start-1 -->"
end="<!-- end-1 -->"
%}
''',
[],
id='start-end',
),
pytest.param(
'''{%
{directive} "./included*.txt"
end="<!-- end-2 -->"
%}
''',
[],
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 -->"
%}
{%
{directive} "./included*.txt"
start="<!-- start-not-found-1 -->"
end="<!-- end-not-found-1 -->"
%}
''',
[
(
"Delimiter end '<!-- end-not-found-1 -->'"
" of '{directive}' directive"
' at {includer_file}:9 not detected in'
' the files {included_file_01}, {included_file_02}'
),
(
"Delimiter end '<!-- end-not-found-2 -->'"
" of '{directive}' directive"
' at {includer_file}:3 not detected in'
' the files {included_file_01}, {included_file_02}'
),
(
"Delimiter start '<!-- start-not-found-1 -->'"
" of '{directive}' directive"
' at {includer_file}:9 not detected in'
' the files {included_file_01}, {included_file_02}'
),
(
"Delimiter start '<!-- start-not-found-2 -->'"
" of '{directive}' directive"
' at {includer_file}:3 not detected in'
' the files {included_file_01}, {included_file_02}'
),
],
id='start-end-not-found',
),
),
)
def test_glob_include(
includer_content,
directive,
expected_warnings_schemas,
page,
plugin,
caplog,
tmp_path,
):
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 []
expected_warnings = [
msg_schema.replace(
'{includer_file}',
str(includer_file.relative_to(tmp_path)),
).replace(
'{included_file_01}',
str(included_01_file.relative_to(tmp_path)),
).replace(
'{included_file_02}',
str(included_02_file.relative_to(tmp_path)),
).replace('{directive}', directive)
for msg_schema in expected_warnings_schemas
]
for record in caplog.records:
assert record.msg in expected_warnings
assert len(expected_warnings_schemas) == len(caplog.records)
|