Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas committed Sep 2, 2024
1 parent a793276 commit 9958f53
Showing 1 changed file with 40 additions and 47 deletions.
87 changes: 40 additions & 47 deletions pelican/plugins/quarto/tests/test_quarto.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def create_article(temp_path):
article_path.write_text(article_content)
return article_path


def test_plugin_functionality(create_article, temp_path):
"""Test basic plugin functionality: Header extraction."""

Expand All @@ -46,50 +47,42 @@ def test_plugin_functionality(create_article, temp_path):
with open(script_dir / "test_data" / "quarto_test_output.html", "r", encoding="utf-8") as f:
mock_html_content = f.read()

# Mock the QuartoHTML class to return a mock object with the desired HTML output
with patch("quarto.quarto.QuartoHTML", autospec=True) as MockQuartoHTML:
mock_instance = MockQuartoHTML.return_value
mock_instance.body = mock_html_content
mock_instance.header_scripts_links = []
mock_instance.header_styles = []

with patch.dict('os.environ', {'PATH': ''}):
path = Path(temp_path)
output_path = path / "output"
content_path = path / "content"
settings = read_settings(
override={
"PATH": content_path,
"OUTPUT_PATH": output_path,
"PLUGIN_PATHS": ["../"],
"PLUGINS": ["quarto"],
}
)
pelican = Pelican(settings=settings)
pelican.run()

articles=os.listdir(output_path)
assert f"{TESTFILE_NAME}.html" in articles, "An article should have been written"

filepath = output_path / f"{TESTFILE_NAME}.html"
with open(filepath, "r", encoding="utf-8") as f:
html_content = f.read()

print(html_content)

soup = BeautifulSoup(html_content, "html.parser")

contents=os.listdir(content_path)
assert "_quarto.yml" in contents, "A quarto config file should have been prepared"

script_tags = soup.find_all("script")
link_tags = soup.find_all("link")

# check if body contains Quarto content
body = soup.find("body")
assert body is not None, "The body of the HTML should exist"
quarto_script = body.find("script", id="quarto-html-after-body")
assert quarto_script is not None, "Quarto-specific script not found in body"

assert any("site_lib" in script.get("src", "") for script in script_tags), "No script link to site_lib found in header"
assert any("site_lib" in link.get("href", "") for link in link_tags), "No link to site_lib found in header"
# Patch the Quarto.run method to return the specific HTML content
with patch('quarto.quarto.Quarto.run_quarto', return_value=mock_html_content):
path = Path(temp_path)
output_path = path / "output"
content_path = path / "content"
settings = read_settings(
override={
"PATH": content_path,
"OUTPUT_PATH": output_path,
"PLUGIN_PATHS": ["../"],
"PLUGINS": ["quarto"],
}
)
pelican = Pelican(settings=settings)
pelican.run()

articles = os.listdir(output_path)
assert f"{TESTFILE_NAME}.html" in articles, "An article should have been written"

filepath = output_path / f"{TESTFILE_NAME}.html"
with open(filepath, "r", encoding="utf-8") as f:
html_content = f.read()

soup = BeautifulSoup(html_content, "html.parser")

contents = os.listdir(content_path)
assert "_quarto.yml" in contents, "A quarto config file should have been prepared"

script_tags = soup.find_all("script")
link_tags = soup.find_all("link")

# check if body contains Quarto content
body = soup.find("body")
assert body is not None, "The body of the HTML should exist"
quarto_script = body.find("script", id="quarto-html-after-body")
assert quarto_script is not None, "Quarto-specific script not found in body"

assert any("site_lib" in script.get("src", "") for script in script_tags), "No script link to site_lib found in header"
assert any("site_lib" in link.get("href", "") for link in link_tags), "No link to site_lib found in header"

0 comments on commit 9958f53

Please sign in to comment.