Skip to content

Commit

Permalink
Add a test for a data-accessing shortcode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anteru committed Dec 24, 2024
1 parent b07c478 commit f191804
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 0 additions & 2 deletions liara/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ class MarkdownDocumentNode(DocumentNode):
def __init__(self, configuration, **kwargs):
super().__init__(**kwargs)
self.__md = self._create_markdown_processor(configuration)
self.__mdext = None

def _create_markdown_processor(self, configuration):
from markdown import Markdown
Expand All @@ -445,7 +444,6 @@ def process(self, cache: Cache, **kwargs):
from .md import ShortcodeException

if '$data' in kwargs:
assert self.__mdext
self.__mdext.set_data(kwargs['$data'])

byte_content = self._raw_content.encode('utf-8')
Expand Down
44 changes: 44 additions & 0 deletions test/test_shortcode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from liara import cmdline
from liara.signals import register_markdown_shortcodes
import liara
from click.testing import CliRunner

_TEST_DATA = {}


def test_shortcode_with_data(tmp_path):
global _TEST_DATA
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=tmp_path):
result = runner.invoke(cmdline.cli, ['quickstart'])
assert result.exit_code == 0

# Add a data file to content
data = {
'root': 'some data'
}

open('content/_index.md', 'w').write("""
---
title: Hello world
---
Invoke a shortcode: <% test /%>""")

liara.yaml.dump_yaml(data, open('content/_data.yaml', 'w'))

register_markdown_shortcodes.connect(_register_test_shortcode)

s = liara.Liara()
s.build()

assert _TEST_DATA['root'] == 'some data'


def _test_shortcode(**kwargs):
global _TEST_DATA
_TEST_DATA = kwargs['$data']


def _register_test_shortcode(sender, preprocessor):
preprocessor.register('test', _test_shortcode)

0 comments on commit f191804

Please sign in to comment.