Skip to content

Commit

Permalink
Fix logging variables not included formatting messages (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Nov 10, 2023
1 parent c8ab8c3 commit 1690934
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 201 deletions.
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mkdocs-include-markdown-plugin"
version = "6.0.3"
version = "6.0.4"
description = "Mkdocs Markdown includer plugin."
readme = "README.md"
license = "Apache-2.0"
Expand Down Expand Up @@ -74,15 +74,15 @@ dependencies = ["pytest~=7.0", "coverage~=6.4", "covdefaults"]

[[tool.hatch.envs.tests.matrix]]
python = ["py38", "py39", "py310", "py311", "py312"]
mkdocs = ["1.4.0", "1.4.3", "1.5.0", "1.5.1"]
mkdocs = ["1.4.0", "1.4.3", "1.5.0", "1.5.3"]
cache = ["yes", "no"]

[tool.hatch.envs.tests.overrides]
matrix.mkdocs.dependencies = [
{ value = "mkdocs==1.4.0", if = ["1.4.0"] },
{ value = "mkdocs==1.4.3", if = ["1.4.3"] },
{ value = "mkdocs==1.5.0", if = ["1.5.0"] },
{ value = "mkdocs==1.5.1", if = ["1.5.1"] },
{ value = "mkdocs==1.5.3", if = ["1.5.3"] },
]
matrix.cache.dependencies = [{ value = "platformdirs", if = ["yes"] }]

Expand Down Expand Up @@ -148,6 +148,7 @@ select = [
"COM",
"SLOT",
]
ignore = ["G004"]

[tool.ruff.pydocstyle]
convention = "google"
Expand Down
50 changes: 15 additions & 35 deletions src/mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class Settings: # noqa: D101
exclude: list[str] | None


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


def lineno_from_content_start(content: str, start: int) -> int:
Expand Down Expand Up @@ -276,26 +278,15 @@ def found_include_tag( # noqa: PLR0912, PLR0915
markdown,
directive_match_start,
)
relative_path = os.path.relpath(page_src_path, docs_dir)
logger.warning(
(
"Delimiter {delimiter_name} '{delimiter_value}'"
" of '{directive}' directive at"
' {relative_path}:{line_number}'
' not detected in the file{plural_suffix}'
' {readable_files_to_include}'
f"Delimiter {delimiter_name} '{delimiter_value}'"
" of 'include' directive at"
f' {relative_path}:{lineno}'
f' not detected in the file{plural_suffix}'
f' {readable_files_to_include}'
),
extra={
'delimiter_name': delimiter_name,
'delimiter_value': delimiter_value,
'directive': 'include',
'relative_path': os.path.relpath(
page_src_path,
docs_dir,
),
'line_number': lineno,
'plural_suffix': plural_suffix,
'readable_files_to_include': readable_files_to_include,
},
)

return text_to_include
Expand Down Expand Up @@ -586,26 +577,15 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
markdown,
directive_match_start,
)
relative_path = os.path.relpath(page_src_path, docs_dir)
logger.warning(
(
"Delimiter {delimiter_name} '{delimiter_value}' of"
" '{directive}' directive at"
' {relative_path}:{line_number}'
' not detected in the file{plural_suffix}'
' {readable_files_to_include}'
f"Delimiter {delimiter_name} '{delimiter_value}' of"
" 'include-markdown' directive at"
f' {relative_path}:{lineno}'
f' not detected in the file{plural_suffix}'
f' {readable_files_to_include}'
),
extra={
'delimiter_name': delimiter_name,
'delimiter_value': delimiter_value,
'directive': 'include-markdown',
'relative_path': os.path.relpath(
page_src_path,
docs_dir,
),
'line_number': lineno,
'plural_suffix': plural_suffix,
'readable_files_to_include': readable_files_to_include,
},
)

return text_to_include
Expand Down
88 changes: 35 additions & 53 deletions tests/test_unit/test_glob_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,46 +99,30 @@ def test_glob_include_absolute(page, tmp_path, plugin):
%}
''',
[
{
'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}'
),
},
(
"Delimiter end '<!-- end-not-found-1 -->'"
" of '{directive}' directive"
' at {includer_file}:10 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}:10 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',
),
Expand All @@ -149,9 +133,9 @@ def test_glob_include(
directive,
expected_warnings_schemas,
page,
plugin,
caplog,
tmp_path,
plugin,
):
includer_file = tmp_path / 'includer.txt'
included_01_file = tmp_path / 'included_01.txt'
Expand Down Expand Up @@ -185,22 +169,20 @@ def test_glob_include(

# 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(
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 i, warning in enumerate(expected_warnings_schemas):
for key in warning:
assert getattr(caplog.records[i], key) == warning[key]
for record in caplog.records:
assert record.msg in expected_warnings
assert len(expected_warnings_schemas) == len(caplog.records)
46 changes: 20 additions & 26 deletions tests/test_unit/test_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,11 @@
''',
[
{
'delimiter_name': 'start',
'delimiter_value': '<!--start-->',
'relative_path': '{includer_file}',
'line_number': 3,
'readable_files_to_include': '{included_file}',
},
(
"Delimiter start '<!--start-->' of 'include'"
' directive at {filepath}:3'
' not detected in the file {included_file}'
),
],
id='start=foo (not found)-end=None',
),
Expand All @@ -219,14 +217,11 @@
Some text
''',
[
{
'delimiter_name': 'end',
'delimiter_value': '<!--end-->',
'relative_path': '{includer_file}',
'line_number': 2,
'readable_files_to_include': '{included_file}',
'directive': 'include',
},
(
"Delimiter end '<!--end-->' of 'include'"
' directive at {filepath}:2'
' not detected in the file {included_file}'
),
],
id='start=None-end=foo (not found)',
),
Expand Down Expand Up @@ -370,9 +365,9 @@ def test_include(
expected_result,
expected_warnings_schemas,
page,
plugin,
caplog,
tmp_path,
plugin,
):
included_file = tmp_path / 'included.md'
includer_file = tmp_path / 'includer.md'
Expand All @@ -394,17 +389,16 @@ def test_include(

# assert warnings
expected_warnings_schemas = expected_warnings_schemas or []
for warning in expected_warnings_schemas:
warning['directive'] = 'include'
warning['relative_path'] = str(includer_file.relative_to(tmp_path))
warning['readable_files_to_include'] = warning[
'readable_files_to_include'
].replace(
expected_warnings = [
msg_schema.replace(
'{filepath}',
str(includer_file.relative_to(tmp_path)),
).replace(
'{included_file}',
str(included_file.relative_to(tmp_path)),
)
) for msg_schema in expected_warnings_schemas
]

for i, warning in enumerate(expected_warnings_schemas):
for key in warning:
assert getattr(caplog.records[i], key) == warning[key]
for record in caplog.records:
assert record.msg in expected_warnings
assert len(expected_warnings_schemas) == len(caplog.records)
Loading

0 comments on commit 1690934

Please sign in to comment.