When generating my own notes, it’s easy for me to fat-finger the latex or similar. To address this, I have a few tests to ensure I don’t have formatting errors.
sudo apt install -y python3-pip python3-venv
python3 -m venv /tmp/venv/
. /tmp/venv/bin/activate
pip install pytest
pip install requests parsel
import os
import os.path
import pytest
import requests
from parsel import Selector
def files_to_urls(d: os.PathLike = '/home/justin/quartz/content/posts') -> list[str]:
urls = []
for _root, _dirs, files in os.walk(d):
for f in files:
basename, _ext = os.path.splitext(f)
urls.append(f'http://localhost:8080/posts/{basename}')
return urls
all_urls = files_to_urls()
@pytest.mark.parametrize("url", all_urls)
def test_has_latex_warnings(url):
content = requests.get(url)
print(url)
z = Selector(text=content.text)
found = ['#cc0000' in x for x in z.css('.katex *').getall()]
assert any(found) == False, f"{url} has malformed (red) content"
@pytest.mark.parametrize("url", all_urls)
def test_no_unmarked_sections(url):
content = requests.get(url)
z = Selector(text=content.text)
print(url)
assert '\\begin' not in z.css('body').getall(), f"Missing $$ around a latex \\begin section on {url}"
@pytest.mark.parametrize("url", all_urls)
def test_no_latex_errors(url):
content = requests.get(url)
z = Selector(text=content.text)
print(url)
err = z.css('.katex-error')
assert len(err) == 0, err.attrib['title']