diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a106a2..44a3e6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## 1.3.7, 2024-10-17 +## 1.3.6, 2024-10-17 * Added: complete test framework, using pytest and Mkdocs-Test (#244) A number of automated test cases are implemented. * Changed: move from setup.py to pyproject.toml (#250) diff --git a/mkdocs_macros/util.py b/mkdocs_macros/util.py index 09f48dc..6796dfc 100644 --- a/mkdocs_macros/util.py +++ b/mkdocs_macros/util.py @@ -137,7 +137,11 @@ def default(self, obj: Any) -> Any: return super().default(obj) except TypeError: debug(f"json: cannot encode {obj.__class__}") - return str(obj) + try: + return str(obj) + except Exception: + # in case something happens along the line + return f"!Non printable object: {obj.__class__}" diff --git a/pyproject.toml b/pyproject.toml index 54ce588..5731e53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,7 @@ test = [ "mkdocs-macros-test", "mkdocs-material>=6.2", "mkdocs-test", + "mkdocs-d2-plugin" ] [project.entry-points."mkdocs.plugins"] diff --git a/test/plugin_d2/.cache/plugin/d2/db.db b/test/plugin_d2/.cache/plugin/d2/db.db new file mode 100644 index 0000000..04f07d6 Binary files /dev/null and b/test/plugin_d2/.cache/plugin/d2/db.db differ diff --git a/test/plugin_d2/__init__.py b/test/plugin_d2/__init__.py new file mode 100644 index 0000000..7a6bb57 --- /dev/null +++ b/test/plugin_d2/__init__.py @@ -0,0 +1,4 @@ +""" +This __init__.py file is indispensable for pytest to +recognize its packages. +""" \ No newline at end of file diff --git a/test/plugin_d2/docs/index.md b/test/plugin_d2/docs/index.md new file mode 100644 index 0000000..ce5c57b --- /dev/null +++ b/test/plugin_d2/docs/index.md @@ -0,0 +1,12 @@ +# Home + +This checks the compatibility with the d2 plugin. + +There used to be an issue, see [Github](https://github.com/fralau/mkdocs-macros-plugin/issues/249). + +It was due to the CustomEncoder class (in util module), +which was susceptible to fail if an object was not printable. + +```d2 +A -> B +``` diff --git a/test/plugin_d2/mkdocs.yaml b/test/plugin_d2/mkdocs.yaml new file mode 100644 index 0000000..0cf7e00 --- /dev/null +++ b/test/plugin_d2/mkdocs.yaml @@ -0,0 +1,17 @@ + +# Documentation name +site_name: 'Compatibility with d2 plugin' + +# Your repository URL and name +repo_url: https://gitlab.com/karreg/mkdocs-macros-d2-issue +repo_name: mkdocs-macros-d2-issue + +# Plugins +plugins: + - search + - d2 + - macros + +# Documentation content +nav: + - Home: 'index.md' diff --git a/test/plugin_d2/test_t2.py b/test/plugin_d2/test_t2.py new file mode 100644 index 0000000..d27f20f --- /dev/null +++ b/test/plugin_d2/test_t2.py @@ -0,0 +1,43 @@ +""" +Testing the d2 project + +There was an incompatibility: +Error: The current file is not set for the '!relative' tag. It cannot be used in this context; the intended usage is within `markdown_extensions`. + +see https://github.com/fralau/mkdocs-macros-plugin/issues/249 + +Requires d2 + +(C) Laurent Franceschetti 2024 +""" + +REQUIRED = "d2" + +import pytest +import subprocess + +def is_d2_installed(): + try: + subprocess.run(["brew", "list", REQUIRED], check=True, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + return True + except subprocess.CalledProcessError: + return False + + +import test +from test.fixture import MacrosDocProject + + +@pytest.mark.skipif(not is_d2_installed(), reason="d2 is not installed") +def test_d2(): + """ + This test will run only if d2 library is installed; + otherwise the d2 plugin will not run + https://d2lang.com/tour/install/ + """ + project = MacrosDocProject() + project.build(strict=False) + # did not fail + print(project.build_result.stderr) + assert not project.build_result.returncode, "Failed when it should not" \ No newline at end of file