Skip to content

Commit 5641806

Browse files
author
Laurent Franceschetti
committed
Fixed incompatibility with d2 module (#249)
- Added test case
1 parent 3a73707 commit 5641806

File tree

8 files changed

+83
-2
lines changed

8 files changed

+83
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project are documented in this file.
44

55
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).
66

7-
## 1.3.7, 2024-10-17
7+
## 1.3.6, 2024-10-17
88
* Added: complete test framework, using pytest and Mkdocs-Test (#244)
99
A number of automated test cases are implemented.
1010
* Changed: move from setup.py to pyproject.toml (#250)

mkdocs_macros/util.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ def default(self, obj: Any) -> Any:
137137
return super().default(obj)
138138
except TypeError:
139139
debug(f"json: cannot encode {obj.__class__}")
140-
return str(obj)
140+
try:
141+
return str(obj)
142+
except Exception:
143+
# in case something happens along the line
144+
return f"!Non printable object: {obj.__class__}"
141145

142146

143147

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ test = [
4848
"mkdocs-macros-test",
4949
"mkdocs-material>=6.2",
5050
"mkdocs-test",
51+
"mkdocs-d2-plugin"
5152
]
5253

5354
[project.entry-points."mkdocs.plugins"]
32 KB
Binary file not shown.

test/plugin_d2/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
This __init__.py file is indispensable for pytest to
3+
recognize its packages.
4+
"""

test/plugin_d2/docs/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Home
2+
3+
This checks the compatibility with the d2 plugin.
4+
5+
There used to be an issue, see [Github](https://github.com/fralau/mkdocs-macros-plugin/issues/249).
6+
7+
It was due to the CustomEncoder class (in util module),
8+
which was susceptible to fail if an object was not printable.
9+
10+
```d2
11+
A -> B
12+
```

test/plugin_d2/mkdocs.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# Documentation name
3+
site_name: 'Compatibility with d2 plugin'
4+
5+
# Your repository URL and name
6+
repo_url: https://gitlab.com/karreg/mkdocs-macros-d2-issue
7+
repo_name: mkdocs-macros-d2-issue
8+
9+
# Plugins
10+
plugins:
11+
- search
12+
- d2
13+
- macros
14+
15+
# Documentation content
16+
nav:
17+
- Home: 'index.md'

test/plugin_d2/test_t2.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Testing the d2 project
3+
4+
There was an incompatibility:
5+
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`.
6+
7+
see https://github.com/fralau/mkdocs-macros-plugin/issues/249
8+
9+
Requires d2
10+
11+
(C) Laurent Franceschetti 2024
12+
"""
13+
14+
REQUIRED = "d2"
15+
16+
import pytest
17+
import subprocess
18+
19+
def is_d2_installed():
20+
try:
21+
subprocess.run(["brew", "list", REQUIRED], check=True,
22+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
23+
return True
24+
except subprocess.CalledProcessError:
25+
return False
26+
27+
28+
import test
29+
from test.fixture import MacrosDocProject
30+
31+
32+
@pytest.mark.skipif(not is_d2_installed(), reason="d2 is not installed")
33+
def test_d2():
34+
"""
35+
This test will run only if d2 library is installed;
36+
otherwise the d2 plugin will not run
37+
https://d2lang.com/tour/install/
38+
"""
39+
project = MacrosDocProject()
40+
project.build(strict=False)
41+
# did not fail
42+
print(project.build_result.stderr)
43+
assert not project.build_result.returncode, "Failed when it should not"

0 commit comments

Comments
 (0)