Skip to content

Commit

Permalink
Fixed null test and completed added opt_in and opt_out
Browse files Browse the repository at this point in the history
  - changed name from opt-in and opt-out
  • Loading branch information
Laurent Franceschetti committed Sep 29, 2024
1 parent b76efef commit 9186fe6
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 18 deletions.
12 changes: 6 additions & 6 deletions mkdocs_macros/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ def on_page_markdown(self, markdown, page:Page,
"""
self._page = page
if not self.variables:
return markdown
self.markdown = markdown
else:
trace("Rendering source page:", page.file.src_path)
# Update the page info in the document
Expand Down Expand Up @@ -986,12 +986,12 @@ def on_page_markdown(self, markdown, page:Page,
for func in self.post_macro_functions:
func(self)

# save the rendered page, with its YAML header
if get_log_level('DEBUG'):
self._save_debug_file(page,
rendered_markdown=self.markdown)
# save the rendered page, with its YAML header
if get_log_level('DEBUG'):
self._save_debug_file(page,
rendered_markdown=self.markdown)

return self.markdown
return self.markdown

def on_post_build(self, config: config_options.Config):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Initialization
# --------------------

VERSION_NUMBER = '1.3.0'
VERSION_NUMBER = '1.3.1'

# required if you want to run document/test
# pip install 'mkdocs-macros-plugin[test]'
Expand Down
47 changes: 42 additions & 5 deletions test/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import List
import json
from typing import Any, List
import difflib
import inspect


# from rich import print
Expand Down Expand Up @@ -306,12 +306,25 @@ def get_tables(markdown_text:str) -> dict[pd.DataFrame]:




# ---------------------------
# OS Functions
# ---------------------------
def run_command(command, *args) -> subprocess.CompletedProcess:
"Execute a command"
full_command = [command] + list(args)
return subprocess.run(full_command, capture_output=True, text=True)

def get_caller_directory():
"Get the caller's directory name (to be called from a function)"
# Get the current frame
current_frame = inspect.currentframe()
# Get the caller's frame
caller_frame = inspect.getouterframes(current_frame, 2)
# Get the file name of the caller
caller_file = caller_frame[1].filename
# Get the absolute path of the directory containing the caller file
directory_abspath = os.path.abspath(os.path.dirname(caller_file))
return directory_abspath

# ---------------------------
# Log parsing
Expand Down Expand Up @@ -523,9 +536,15 @@ def is_rendered(self) -> bool:
or in `on_post_page_macro()`) but the text itself
was not modified.
"""
return self.source_page.markdown not in self.markdown
# make sure that the source is stripped, to be sure.
return self.source_page.markdown.strip() not in self.markdown


def __repr__(self):
"""
Important for error printout
"""
return f"Markdown page ({self.filename}):\n{self.text}"

# ---------------------------
# Main class
Expand All @@ -535,7 +554,11 @@ class DocProject(object):

def __init__(self, directory:str=''):
"Initialize"
self._project_dir = os.path.join(REF_DIR, directory)
project_dir = os.path.join(REF_DIR, directory)
if not os.path.isdir(project_dir):
raise FileNotFoundError(f"Doc directory '{directory}' "
"does not exist.")
self._project_dir = project_dir
# test existence of YAML file or fail
self.config_file

Expand Down Expand Up @@ -802,9 +825,23 @@ def pages(self) -> List[TestMarkdownPage]:

def get_page(self, name:str):
"Get the page by its filename or a substring"
print("SEARCHING:", name)
for page in self.pages:
if name in page.filename:
# give priority to exact matches
if name == page.filename:
return page
# try without extension
stem, _ = os.path.splitext(page.filename)
if name == stem:
return page
# try again without full path
for page in self.pages:
if page.filename.endswith(name):
return page
stem, _ = os.path.splitext(page.filename)
if stem.endswith(name):
return page
print("- NOT FOUND")

def get_plugin(self, name:str) -> SuperDict:
"Get the plugin by its plugin name"
Expand Down
5 changes: 3 additions & 2 deletions test/null/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from test.fixture import DocProject

CURRENT_PROJECT = 'simple'
CURRENT_PROJECT = 'null'



Expand All @@ -27,7 +27,8 @@ def test_pages():


page = PROJECT.get_page('index')
assert not page.is_rendered
ERROR_MSG = f"Is rendered!:\n{page.markdown}\n---SOURCE:\n{page.source_page.markdown}\n---"
assert not page.is_rendered, ERROR_MSG
assert not page.has_error


Expand Down
4 changes: 4 additions & 0 deletions test/opt_in/__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.
"""
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
title: Opt-in by paths
signal: Rendered!!!
---
# {{ title }}

The signal is: {{ signal }}.

This page must be rendered because there is `render_*.md` in the
`force_render_paths` variable, and the name of this page is
**`{{page.file.src_uri}}`**.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
render_macros: false # opt-in
render_macros: false # opt-out
---
# Opt-out by page header (priority)

Expand Down
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions test/opt_in/test_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Testing the project
(C) Laurent Franceschetti 2024
"""
import pytest

from test.fixture import DocProject

CURRENT_PROJECT = 'opt_in'




def test_opt_in():
PROJECT = DocProject(CURRENT_PROJECT)
PROJECT.build()
# did not fail
assert not PROJECT.build_result.returncode



# ---------------------------
# which pages are rendered?
# ---------------------------
# test the config:
macros = PROJECT.macros_plugin
assert macros.render_by_default == False

page = PROJECT.get_page('render_this_one')
assert page.is_rendered
assert page.find(page.metadata.signal), f"Did not find signal '{page.metadata.signal}'"

print([page.filename for page in PROJECT.pages])
page2 = PROJECT.get_page('rendered/noname')
assert page2.filename == 'rendered/noname.md', f"is: {page2.filename}"
assert page2.find("0: Hello world")
assert page2.is_rendered


assert not PROJECT.get_page('not_rendered/noname').is_rendered

# exception in the metadata:
exception_page = PROJECT.get_page('rendered/exception')
assert exception_page.metadata.render_macros == False
assert not exception_page.is_rendered
assert exception_page.find('macros_info')
4 changes: 4 additions & 0 deletions test/opt_out/__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.
"""
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---
render_macros: true # opt-in
title: Rendered title
signal: Hello World
---
# {{ title }}

{{ title }}

{{ macros_info() }}
2 changes: 1 addition & 1 deletion test/opt-out/mkdocs.yml → test/opt_out/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ nav:
plugins:
- search
- macros:
# do not render the pages by default
# do render the pages by default
# this is the default case
render_by_default: true

42 changes: 42 additions & 0 deletions test/opt_out/test_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Testing the project
(C) Laurent Franceschetti 2024
"""
import pytest

from test.fixture import DocProject

CURRENT_PROJECT = 'opt_out'




def test_opt_in():
PROJECT = DocProject(CURRENT_PROJECT)
PROJECT.build()
# did not fail
assert not PROJECT.build_result.returncode



# ---------------------------
# which pages are rendered?
# ---------------------------
# test the config (this is the default anyway)
macros = PROJECT.macros_plugin
assert macros.render_by_default == True

# opt-out:
page = PROJECT.get_page('index')
assert page.metadata.render_macros == False
assert not page.is_rendered
assert "macros_info" in page.markdown


# Normal:
page = PROJECT.get_page('rendered')
assert "render_macros" not in page.metadata
assert page.is_rendered
assert page.metadata.signal in page.markdown

4 changes: 3 additions & 1 deletion test/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ def test_low_level_fixtures():
print(find_in_html(html_doc, 'under the', header='sub header'))




def test_high_level_fixtures():
"""
Test a project
"""
MYPROJECT = 'opt-in'
MYPROJECT = 'opt_in'
# MYPROJECT = 'simple'
h1(f"TESTING MKDOCS-MACROS PROJECT ({MYPROJECT})")

Expand Down

0 comments on commit 9186fe6

Please sign in to comment.