Skip to content

Commit

Permalink
Fixed incompatibility with d2 module (#249)
Browse files Browse the repository at this point in the history
  - Added test case
  • Loading branch information
Laurent Franceschetti committed Oct 18, 2024
1 parent 3a73707 commit 5641806
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion mkdocs_macros/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__}"



Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ test = [
"mkdocs-macros-test",
"mkdocs-material>=6.2",
"mkdocs-test",
"mkdocs-d2-plugin"
]

[project.entry-points."mkdocs.plugins"]
Expand Down
Binary file added test/plugin_d2/.cache/plugin/d2/db.db
Binary file not shown.
4 changes: 4 additions & 0 deletions test/plugin_d2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
This __init__.py file is indispensable for pytest to
recognize its packages.
"""
12 changes: 12 additions & 0 deletions test/plugin_d2/docs/index.md
Original file line number Diff line number Diff line change
@@ -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
```
17 changes: 17 additions & 0 deletions test/plugin_d2/mkdocs.yaml
Original file line number Diff line number Diff line change
@@ -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'
43 changes: 43 additions & 0 deletions test/plugin_d2/test_t2.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 5641806

Please sign in to comment.