Skip to content

Commit 1690934

Browse files
authored
Fix logging variables not included formatting messages (#184)
1 parent c8ab8c3 commit 1690934

File tree

7 files changed

+132
-201
lines changed

7 files changed

+132
-201
lines changed

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "6.0.3"
3+
version = "6.0.4"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"
@@ -74,15 +74,15 @@ dependencies = ["pytest~=7.0", "coverage~=6.4", "covdefaults"]
7474

7575
[[tool.hatch.envs.tests.matrix]]
7676
python = ["py38", "py39", "py310", "py311", "py312"]
77-
mkdocs = ["1.4.0", "1.4.3", "1.5.0", "1.5.1"]
77+
mkdocs = ["1.4.0", "1.4.3", "1.5.0", "1.5.3"]
7878
cache = ["yes", "no"]
7979

8080
[tool.hatch.envs.tests.overrides]
8181
matrix.mkdocs.dependencies = [
8282
{ value = "mkdocs==1.4.0", if = ["1.4.0"] },
8383
{ value = "mkdocs==1.4.3", if = ["1.4.3"] },
8484
{ value = "mkdocs==1.5.0", if = ["1.5.0"] },
85-
{ value = "mkdocs==1.5.1", if = ["1.5.1"] },
85+
{ value = "mkdocs==1.5.3", if = ["1.5.3"] },
8686
]
8787
matrix.cache.dependencies = [{ value = "platformdirs", if = ["yes"] }]
8888

@@ -148,6 +148,7 @@ select = [
148148
"COM",
149149
"SLOT",
150150
]
151+
ignore = ["G004"]
151152

152153
[tool.ruff.pydocstyle]
153154
convention = "google"

src/mkdocs_include_markdown_plugin/event.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class Settings: # noqa: D101
4848
exclude: list[str] | None
4949

5050

51-
logger = logging.getLogger('mkdocs.plugins.mkdocs_include_markdown_plugin')
51+
# TODO: when Mkdocs < 1.5.0 support is dropped, use
52+
# mkdocs.plugin.get_plugin_logger
53+
logger = logging.getLogger('mkdocs.plugins.include_markdown')
5254

5355

5456
def lineno_from_content_start(content: str, start: int) -> int:
@@ -276,26 +278,15 @@ def found_include_tag( # noqa: PLR0912, PLR0915
276278
markdown,
277279
directive_match_start,
278280
)
281+
relative_path = os.path.relpath(page_src_path, docs_dir)
279282
logger.warning(
280283
(
281-
"Delimiter {delimiter_name} '{delimiter_value}'"
282-
" of '{directive}' directive at"
283-
' {relative_path}:{line_number}'
284-
' not detected in the file{plural_suffix}'
285-
' {readable_files_to_include}'
284+
f"Delimiter {delimiter_name} '{delimiter_value}'"
285+
" of 'include' directive at"
286+
f' {relative_path}:{lineno}'
287+
f' not detected in the file{plural_suffix}'
288+
f' {readable_files_to_include}'
286289
),
287-
extra={
288-
'delimiter_name': delimiter_name,
289-
'delimiter_value': delimiter_value,
290-
'directive': 'include',
291-
'relative_path': os.path.relpath(
292-
page_src_path,
293-
docs_dir,
294-
),
295-
'line_number': lineno,
296-
'plural_suffix': plural_suffix,
297-
'readable_files_to_include': readable_files_to_include,
298-
},
299290
)
300291

301292
return text_to_include
@@ -586,26 +577,15 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
586577
markdown,
587578
directive_match_start,
588579
)
580+
relative_path = os.path.relpath(page_src_path, docs_dir)
589581
logger.warning(
590582
(
591-
"Delimiter {delimiter_name} '{delimiter_value}' of"
592-
" '{directive}' directive at"
593-
' {relative_path}:{line_number}'
594-
' not detected in the file{plural_suffix}'
595-
' {readable_files_to_include}'
583+
f"Delimiter {delimiter_name} '{delimiter_value}' of"
584+
" 'include-markdown' directive at"
585+
f' {relative_path}:{lineno}'
586+
f' not detected in the file{plural_suffix}'
587+
f' {readable_files_to_include}'
596588
),
597-
extra={
598-
'delimiter_name': delimiter_name,
599-
'delimiter_value': delimiter_value,
600-
'directive': 'include-markdown',
601-
'relative_path': os.path.relpath(
602-
page_src_path,
603-
docs_dir,
604-
),
605-
'line_number': lineno,
606-
'plural_suffix': plural_suffix,
607-
'readable_files_to_include': readable_files_to_include,
608-
},
609589
)
610590

611591
return text_to_include

tests/test_unit/test_glob_include.py

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -99,46 +99,30 @@ def test_glob_include_absolute(page, tmp_path, plugin):
9999
%}
100100
''',
101101
[
102-
{
103-
'delimiter_name': 'start',
104-
'delimiter_value': '<!-- start-not-found-2 -->',
105-
'relative_path': '{includer_file}',
106-
'line_number': 3,
107-
'plural_suffix': 's',
108-
'readable_files_to_include': (
109-
'{included_file_01}, {included_file_02}'
110-
),
111-
},
112-
{
113-
'delimiter_name': 'end',
114-
'delimiter_value': '<!-- end-not-found-2 -->',
115-
'relative_path': '{includer_file}',
116-
'line_number': 3,
117-
'plural_suffix': 's',
118-
'readable_files_to_include': (
119-
'{included_file_01}, {included_file_02}'
120-
),
121-
},
122-
{
123-
'delimiter_name': 'start',
124-
'delimiter_value': '<!-- start-not-found-1 -->',
125-
'relative_path': '{includer_file}',
126-
'line_number': 10,
127-
'plural_suffix': 's',
128-
'readable_files_to_include': (
129-
'{included_file_01}, {included_file_02}'
130-
),
131-
},
132-
{
133-
'delimiter_name': 'end',
134-
'delimiter_value': '<!-- end-not-found-1 -->',
135-
'relative_path': '{includer_file}',
136-
'line_number': 10,
137-
'plural_suffix': 's',
138-
'readable_files_to_include': (
139-
'{included_file_01}, {included_file_02}'
140-
),
141-
},
102+
(
103+
"Delimiter end '<!-- end-not-found-1 -->'"
104+
" of '{directive}' directive"
105+
' at {includer_file}:10 not detected in'
106+
' the files {included_file_01}, {included_file_02}'
107+
),
108+
(
109+
"Delimiter end '<!-- end-not-found-2 -->'"
110+
" of '{directive}' directive"
111+
' at {includer_file}:3 not detected in'
112+
' the files {included_file_01}, {included_file_02}'
113+
),
114+
(
115+
"Delimiter start '<!-- start-not-found-1 -->'"
116+
" of '{directive}' directive"
117+
' at {includer_file}:10 not detected in'
118+
' the files {included_file_01}, {included_file_02}'
119+
),
120+
(
121+
"Delimiter start '<!-- start-not-found-2 -->'"
122+
" of '{directive}' directive"
123+
' at {includer_file}:3 not detected in'
124+
' the files {included_file_01}, {included_file_02}'
125+
),
142126
],
143127
id='start-end-not-found',
144128
),
@@ -149,9 +133,9 @@ def test_glob_include(
149133
directive,
150134
expected_warnings_schemas,
151135
page,
136+
plugin,
152137
caplog,
153138
tmp_path,
154-
plugin,
155139
):
156140
includer_file = tmp_path / 'includer.txt'
157141
included_01_file = tmp_path / 'included_01.txt'
@@ -185,22 +169,20 @@ def test_glob_include(
185169

186170
# assert warnings
187171
expected_warnings_schemas = expected_warnings_schemas or []
188-
for warning in expected_warnings_schemas:
189-
warning['directive'] = directive
190-
warning['relative_path'] = warning['relative_path'].replace(
191-
'{includer_file}', str(includer_file.relative_to(tmp_path)),
192-
)
193-
warning['readable_files_to_include'] = warning[
194-
'readable_files_to_include'
195-
].replace(
172+
expected_warnings = [
173+
msg_schema.replace(
174+
'{includer_file}',
175+
str(includer_file.relative_to(tmp_path)),
176+
).replace(
196177
'{included_file_01}',
197178
str(included_01_file.relative_to(tmp_path)),
198179
).replace(
199180
'{included_file_02}',
200181
str(included_02_file.relative_to(tmp_path)),
201-
)
182+
).replace('{directive}', directive)
183+
for msg_schema in expected_warnings_schemas
184+
]
202185

203-
for i, warning in enumerate(expected_warnings_schemas):
204-
for key in warning:
205-
assert getattr(caplog.records[i], key) == warning[key]
186+
for record in caplog.records:
187+
assert record.msg in expected_warnings
206188
assert len(expected_warnings_schemas) == len(caplog.records)

tests/test_unit/test_include.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,11 @@
192192
193193
''',
194194
[
195-
{
196-
'delimiter_name': 'start',
197-
'delimiter_value': '<!--start-->',
198-
'relative_path': '{includer_file}',
199-
'line_number': 3,
200-
'readable_files_to_include': '{included_file}',
201-
},
195+
(
196+
"Delimiter start '<!--start-->' of 'include'"
197+
' directive at {filepath}:3'
198+
' not detected in the file {included_file}'
199+
),
202200
],
203201
id='start=foo (not found)-end=None',
204202
),
@@ -219,14 +217,11 @@
219217
Some text
220218
''',
221219
[
222-
{
223-
'delimiter_name': 'end',
224-
'delimiter_value': '<!--end-->',
225-
'relative_path': '{includer_file}',
226-
'line_number': 2,
227-
'readable_files_to_include': '{included_file}',
228-
'directive': 'include',
229-
},
220+
(
221+
"Delimiter end '<!--end-->' of 'include'"
222+
' directive at {filepath}:2'
223+
' not detected in the file {included_file}'
224+
),
230225
],
231226
id='start=None-end=foo (not found)',
232227
),
@@ -370,9 +365,9 @@ def test_include(
370365
expected_result,
371366
expected_warnings_schemas,
372367
page,
368+
plugin,
373369
caplog,
374370
tmp_path,
375-
plugin,
376371
):
377372
included_file = tmp_path / 'included.md'
378373
includer_file = tmp_path / 'includer.md'
@@ -394,17 +389,16 @@ def test_include(
394389

395390
# assert warnings
396391
expected_warnings_schemas = expected_warnings_schemas or []
397-
for warning in expected_warnings_schemas:
398-
warning['directive'] = 'include'
399-
warning['relative_path'] = str(includer_file.relative_to(tmp_path))
400-
warning['readable_files_to_include'] = warning[
401-
'readable_files_to_include'
402-
].replace(
392+
expected_warnings = [
393+
msg_schema.replace(
394+
'{filepath}',
395+
str(includer_file.relative_to(tmp_path)),
396+
).replace(
403397
'{included_file}',
404398
str(included_file.relative_to(tmp_path)),
405-
)
399+
) for msg_schema in expected_warnings_schemas
400+
]
406401

407-
for i, warning in enumerate(expected_warnings_schemas):
408-
for key in warning:
409-
assert getattr(caplog.records[i], key) == warning[key]
402+
for record in caplog.records:
403+
assert record.msg in expected_warnings
410404
assert len(expected_warnings_schemas) == len(caplog.records)

0 commit comments

Comments
 (0)