import importlib.util import tempfile import unittest from pathlib import Path import sys p = Path(__file__).resolve().parents[1] / 'scripts' / 'export_wordpress_url_baseline.py' spec = importlib.util.spec_from_file_location('url_baseline', p) a = importlib.util.module_from_spec(spec) sys.modules[spec.name] = a spec.loader.exec_module(a) class Tests(unittest.TestCase): def test_legacy_url_route(self): self.assertEqual(a.route_from_url('https://example.com/pickup-truck/', {'example.com'}), '/pickup-truck/') def test_query_legacy_requires_manual_mapping(self): self.assertIsNone(a.route_from_url('https://example.com/?p=22', {'example.com'})) def test_foreign_host_excluded(self): self.assertIsNone(a.route_from_url('https://bad.example.com/x/', {'example.com'})) def test_wxr_only_published_not_attachments(self): with tempfile.TemporaryDirectory() as d: f = Path(d)/'wp.xml' f.write_text(''' https://example.com/p1/pagepublish https://example.com/p2/pagedraft https://example.com/upload/attachmentpublish ''') out=Path(d)/'old.txt' self.assertEqual(a.main([str(f),'--host','example.com','--output',str(out)]),0) self.assertIn('/p1/',out.read_text()) self.assertNotIn('/p2/',out.read_text()) self.assertNotIn('/upload/',out.read_text()) def test_sitemap_support(self): with tempfile.TemporaryDirectory() as d: f=Path(d)/'s.xml' f.write_text('https://example.com/about/') out=Path(d)/'urls.txt' self.assertEqual(a.main([str(f),'--host','example.com','--output',str(out)]),0) self.assertIn('/about/',out.read_text()) def test_dtd_rejected(self): with tempfile.TemporaryDirectory() as d: f=Path(d)/'s.xml' f.write_text(']>&hacked;') self.assertNotEqual(a.main([str(f),'--host','example.com','--output',str(Path(d)/'urls.txt')]),0) if __name__=='__main__': unittest.main()