Skip to content

Commit 3351d5f

Browse files
committed
Expand environment variables when injecting the mike plugin; resolves #217
1 parent e4d83ed commit 3351d5f

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changes
22

3+
## v2.1.1 (in progress)
4+
5+
### Bug fixes
6+
- Support using environment variables for `INHERIT` when injecting the `mike`
7+
plugin into `mkdocs.yml`
8+
9+
---
10+
311
## v2.1.0 (2024-05-01)
412

513
### New features

mike/mkdocs_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import subprocess
77
import yaml
8+
import yaml_env_tag
89
from collections.abc import Iterable, Mapping
910
from contextlib import contextmanager
1011
from tempfile import NamedTemporaryFile
@@ -32,6 +33,12 @@ class RoundTripLoader(yaml.Loader):
3233
pass
3334

3435

36+
# We need to expand environment variables in our round trip loader (making it
37+
# less of a "round trip"), or else `INHERIT: !ENV ...` will fail when injecting
38+
# the mike plugin. MkDocs really doesn't make this easy on us...
39+
yaml.add_constructor('!ENV', yaml_env_tag.construct_env_tag,
40+
Loader=RoundTripLoader)
41+
3542
yaml.add_multi_constructor('!', RoundTrippableTag.constructor,
3643
Loader=RoundTripLoader)
3744
yaml.add_multi_representer(RoundTrippableTag, RoundTrippableTag.representer)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def run(self):
8888

8989
install_requires=(['importlib_metadata', 'importlib_resources',
9090
'jinja2 >= 2.7', 'mkdocs >= 1.0', 'pyparsing >= 3.0',
91-
'pyyaml >= 5.1', 'verspec']),
91+
'pyyaml >= 5.1', 'pyyaml_env_tag', 'verspec']),
9292
extras_require={
9393
'dev': ['coverage', 'flake8 >= 3.0', 'flake8-quotes', 'shtab'],
9494
'test': ['coverage', 'flake8 >= 3.0', 'flake8-quotes', 'shtab'],

test/unit/test_mkdocs_utils.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,21 @@ def test_round_trip(self):
167167
cfg = ('plugins:\n' +
168168
' - foo:\n option: !relative $config_dir\n' +
169169
' - bar:\n option: !ENV variable\n' +
170-
' - baz:\n option: !ENV [variable, default]'
171-
)
170+
' - baz:\n option: !ENV [variable, default]')
172171
with mock.patch('builtins.open',
173172
mock_open_files({'mkdocs.yml': cfg})), \
174173
mock.patch('mike.mkdocs_utils.NamedTemporaryFile',
175174
return_value=self.out), \
176-
mock.patch('os.remove') as mremove:
175+
mock.patch('os.remove') as mremove, \
176+
mock.patch.dict(os.environ, {'variable': 'mock_val'}, clear=True):
177177
with mkdocs_utils.inject_plugin('mkdocs.yml') as f:
178178
self.assertEqual(f, self.out.name)
179179
mremove.assert_called_once()
180180

181181
expected = ('plugins:\n- mike\n' +
182182
"- foo:\n option: !relative '$config_dir'\n" +
183-
"- bar:\n option: !ENV 'variable'\n"
184-
'- baz:\n option: !ENV [variable, default]\n')
183+
'- bar:\n option: mock_val\n'
184+
'- baz:\n option: mock_val\n')
185185
self.assertEqual(self.out.getvalue(), expected)
186186

187187
def test_python_tag(self):
@@ -224,6 +224,30 @@ def test_inherit(self):
224224
[('mike', {}), ('bar', {}), ('foo', {})]
225225
)
226226

227+
def test_inherit_env(self):
228+
main_cfg = 'INHERIT: !ENV base_file\nplugins:\n foo: {}\n'
229+
base_cfg = 'plugins:\n bar: {}\n'
230+
files = {'mkdocs.yml': main_cfg, 'mkdocs-base.yml': base_cfg}
231+
with mock.patch('builtins.open', mock_open_files(files)), \
232+
mock.patch('mike.mkdocs_utils.NamedTemporaryFile',
233+
return_value=self.out), \
234+
mock.patch('os.path.exists', return_value=True), \
235+
mock.patch('os.remove') as mremove, \
236+
mock.patch.dict(os.environ, {'base_file': 'mkdocs-base.yml'},
237+
clear=True):
238+
with mkdocs_utils.inject_plugin('mkdocs.yml') as f:
239+
self.assertEqual(f, 'mike-mkdocs.yml')
240+
newcfg = yaml.safe_load(self.out.getvalue())
241+
mremove.assert_called_once()
242+
243+
self.assertEqual(newcfg, {'plugins': {
244+
'mike': {}, 'bar': {}, 'foo': {},
245+
}})
246+
self.assertEqual(
247+
list(newcfg['plugins'].items()),
248+
[('mike', {}), ('bar', {}), ('foo', {})]
249+
)
250+
227251

228252
class TestBuild(unittest.TestCase):
229253
def test_build(self):

0 commit comments

Comments
 (0)