kmalloc.org/tools/test_migrate.py

101 lines
5.4 KiB
Python

"""Tests of the one-off migration helpers (run with: python3 -m unittest discover -s tools)."""
import unittest
import migrate
class HtmlToMarkdown(unittest.TestCase):
def test_paragraph_with_inline_markup(self):
html = '<p>\nHello <strong>world</strong> and <a href="/x.html">a link</a> and <em>this</em> and <code>code</code>.\n</p>'
self.assertEqual(migrate.html_to_md(html), "Hello **world** and [a link](/x.html) and *this* and `code`.\n")
def test_link_around_code(self):
html = '<p>\nSend to <a href="bitcoin:bc1q"><code>bc1q</code></a>\n</p>'
self.assertEqual(migrate.html_to_md(html), "Send to [`bc1q`](bitcoin:bc1q)\n")
def test_heading_with_id(self):
self.assertEqual(migrate.html_to_md('<h2 id="account">Request an account</h2>'), "## Request an account {#account}\n")
self.assertEqual(migrate.html_to_md("<h1>Services</h1>"), "# Services\n")
def test_list(self):
html = '<ul>\n<li>one</li>\n<li><a href="/a">two</a></li>\n</ul>'
self.assertEqual(migrate.html_to_md(html), "- one\n- [two](/a)\n")
def test_blocks_are_separated_by_a_blank_line(self):
html = "<h1>T</h1>\n\n<p>\nBody.\n</p>\n<ul>\n<li>x</li>\n</ul>\n"
self.assertEqual(migrate.html_to_md(html), "# T\n\nBody.\n\n- x\n")
def test_table_stays_raw(self):
table = '<table class="data">\n<caption>C</caption>\n<tbody>\n<tr>\n<td data-label="Kept">14 days</td>\n</tr>\n</tbody>\n</table>'
self.assertEqual(migrate.html_to_md(table), table + "\n")
def test_comment_stays_raw(self):
self.assertEqual(migrate.html_to_md("<!-- TODO verificare: x -->"), "<!-- TODO verificare: x -->\n")
def test_raw_when_there_are_attributes_or_other_tags(self):
cases = [
'<p>\n<img class="logo" src="/logo-auto.svg" alt="kmalloc" width="112">\n</p>',
'<h1 class="hero">Technology.<br>Freedom.<br><span class="accent">Dissent.</span></h1>',
'<h2 id="v1.2">Version 1.2, <time datetime="2026-11-14">2026-11-14</time></h2>',
'<p class="note" role="note">\nA note.\n</p>',
'<p>\n<a href="https://x.org/" rel="noreferrer">shop</a>.\n</p>',
]
for html in cases:
with self.subTest(html=html):
self.assertEqual(migrate.html_to_md(html), html + "\n")
def test_unknown_text_between_blocks_fails(self):
with self.assertRaises(ValueError):
migrate.html_to_md("<p>\nfine\n</p>\nstray text\n<p>\nmore\n</p>")
class Placeholders(unittest.TestCase):
def test_restore_replaces_literal_values(self):
values = {"btc": "bc1qabc", "contact_shown": "squid (at) kmalloc.org", "gpg_key": "/squid.asc"}
md = "Write to **squid (at) kmalloc.org**. [squid.asc](/squid.asc) and bc1qabc."
self.assertEqual(migrate.restore(md, values),
"Write to **{{contact_shown}}**. [squid.asc]({{gpg_key}}) and {{btc}}.")
def test_restore_prefers_the_longest_value(self):
values = {"a": "https://x.org/", "b": "https://x.org/search?q=1"}
self.assertEqual(migrate.restore("see https://x.org/search?q=1", values), "see {{b}}")
class Content(unittest.TestCase):
def test_extract_content_between_header_and_footer(self):
page = "<body>\n<header>\n<nav>x</nav>\n</header>\n\n<h1>T</h1>\n\n<p>\nB\n</p>\n\n<footer>\nf\n</footer>\n</body>"
self.assertEqual(migrate.extract_content(page), "<h1>T</h1>\n\n<p>\nB\n</p>\n")
def test_page_file_name(self):
cases = [("/", "en", "en", "index.md"), ("/manifesto.html", "en", "en", "manifesto.md"),
("/news/", "en", "en", "news/index.md"), ("/it/", "en", "it", "index.md"),
("/it/servizi.html", "en", "it", "servizi.md")]
for path, default, lang, want in cases:
with self.subTest(path=path):
self.assertEqual(migrate.page_file(path, default, lang), want)
def test_page_header(self):
header = migrate.page_header("kmalloc.org: News", "news", "Updates.", "bare", note="", switch={"en": "/?lang=en"})
self.assertEqual(header, "Title: kmalloc.org: News\nID: news\nDescription: Updates.\nLayout: bare\nSwitch: en=/?lang=en\n-\n")
def test_post_conversion(self):
old = "---\ntitle: A shop\nsummary: About the shop.\n---\n\nThere is a shop.\n"
name, text = migrate.convert_post("2026-11-14-shop.md", old)
self.assertEqual(name, "shop.md")
self.assertEqual(text, "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: News\n"
"Description: About the shop.\n-\n\nThere is a shop.\n")
def test_post_without_summary_has_no_description(self):
_, text = migrate.convert_post("2026-11-12-x.md", "---\ntitle: X\n---\nBody.\n")
self.assertNotIn("Description:", text)
def test_block_markers(self):
home = '<p>\nA\n</p>\n\n<h2 id="news">News</h2>\n\n<p>\nIntro. <a href="/news/">All</a>.\n</p>\n\n<ul>\n<li>x</li>\n</ul>\n'
self.assertEqual(migrate.mark_latest_news(home), '<p>\nA\n</p>\n\n<p>{{latest_news}}</p>\n')
news = '<h1>News</h1>\n\n<p>\nIntro.\n</p>\n\n<ul>\n<li>x</li>\n</ul>\n\n<p>\n<a href="/news/feed.xml">RSS feed</a>\n</p>\n'
self.assertEqual(migrate.mark_news_list(news),
'<h1>News</h1>\n\n<p>\nIntro.\n</p>\n\n<p>{{news_list}}</p>\n\n<p>\n<a href="/news/feed.xml">RSS feed</a>\n</p>\n')
if __name__ == "__main__":
unittest.main()