kmalloc.org/tools/test_compare.py
squid d2f9bba46b feat: move the pages to Markdown and the news to the dated layout
Pages are Markdown files in content/<lang>/ with a Key: value header, the
news carry Title, Author, Date, Categories and Description and are published
as /news/YYYY/MM/name.html, with a page for every category. The templates, the
language files and site.json shrink to the chrome and the shared strings, and
the footer credits motorino. The output matches the one of build.py page by
page, checked with tools/compare.py and the Nu HTML Checker.
2026-11-15 09:58:10 +01:00

64 lines
3.2 KiB
Python

"""Tests of the old-versus-new output comparison."""
import unittest
import compare
def page(body, footer="", head=""):
return f"<html><head>{head}</head><body>\n<header>h</header>\n\n{body}\n<footer>\n{footer}\n</footer>\n</body></html>"
class ComparePages(unittest.TestCase):
def test_identical_pages_have_no_differences(self):
html = page('<h1>T</h1>\n<p>\nHello <a href="/x.html" hreflang="en">link</a>.\n</p>')
self.assertEqual(compare.compare_pages(html, html), [])
def test_whitespace_and_attribute_order_do_not_matter(self):
old = page('<p>\nHello\n world <a href="/x" lang="en" hreflang="en">l</a>\n</p>')
new = page('<p>Hello world <a hreflang="en" href="/x" lang="en">l</a></p>')
self.assertEqual(compare.compare_pages(old, new), [])
def test_a_changed_link_is_reported(self):
old = page('<p>\n<a href="/a.html">x</a>\n</p>')
new = page('<p>\n<a href="/b.html">x</a>\n</p>')
diffs = compare.compare_pages(old, new)
self.assertTrue(diffs)
self.assertTrue(any("/a.html" in d or "/b.html" in d for d in diffs))
def test_changed_text_is_reported(self):
self.assertTrue(compare.compare_pages(page("<p>one</p>"), page("<p>two</p>")))
def test_comments_are_compared(self):
self.assertTrue(compare.compare_pages(page("<!-- TODO a --><p>x</p>"), page("<p>x</p>")))
def test_the_footer_credit_and_generator_meta_are_ignored(self):
old = page("<p>x</p>", footer="<p>\nlicense\n</p>")
new = page("<p>x</p>", footer='<p>\nlicense\n</p>\n<p>\nPowered by <a href="https://git.kmalloc.org/squid/motorino">motorino</a>.\n</p>',
head='<meta name="generator" content="motorino 1.0.0">')
self.assertEqual(compare.compare_pages(old, new), [])
def test_post_byline_and_description_are_ignored_but_the_body_is_not(self):
old = page('<h1>T</h1>\n\n<p>\n<time datetime="2026-11-14">2026-11-14</time>\n</p>\n\n<p>\nBody.\n</p>',
head='<meta name="description" content="Title">')
new = page('<h1>T</h1>\n\n<p>\n<time datetime="2026-11-14">2026-11-14</time> &middot; squid &middot; <a href="/news/category/news.html">News</a>\n</p>\n\n<p>\nBody.\n</p>',
head='<meta name="description" content="First sentence.">')
self.assertEqual(compare.compare_pages(old, new, post=True), [])
changed = new.replace("Body.", "Other body.")
self.assertTrue(compare.compare_pages(old, changed, post=True))
class PostPaths(unittest.TestCase):
def test_percent_encoded_addresses_are_moved_too(self):
mapping = {"/news/shop.html": "/news/2026/11/shop.html"}
html = "uri=https%3A%2F%2Fkmalloc.org%2Fnews%2Fshop.html"
self.assertEqual(compare.remap(html, mapping), "uri=https%3A%2F%2Fkmalloc.org%2Fnews%2F2026%2F11%2Fshop.html")
def test_old_post_links_are_moved_to_the_new_paths(self):
mapping = {"/news/shop.html": "/news/2026/11/shop.html"}
html = '<a href="/news/shop.html">shop</a> and https://kmalloc.org/news/shop.html'
self.assertEqual(compare.remap(html, mapping),
'<a href="/news/2026/11/shop.html">shop</a> and https://kmalloc.org/news/2026/11/shop.html')
if __name__ == "__main__":
unittest.main()