Hugoifier

hugoifier / tests / test_hugoify.py
Source Blame History 70 lines
6272677… lmata 1 """Tests for utils.hugoify."""
6272677… lmata 2 import os
6272677… lmata 3 import tempfile
48176db… ragelink 4 import unittest
6272677… lmata 5
91515c0… lmata 6 from hugoifier.utils.hugoify import _fallback_baseof, _parse_layout_json, hugoify_dir
6272677… lmata 7
6272677… lmata 8
6272677… lmata 9 class TestParseLayoutJson(unittest.TestCase):
6272677… lmata 10 def test_parses_valid_json(self):
6272677… lmata 11 response = '{"_default/baseof.html": "<!doctype html>", "index.html": "{{ define \\"main\\" }}{{ end }}"}'
6272677… lmata 12 result = _parse_layout_json(response)
6272677… lmata 13 self.assertIn("_default/baseof.html", result)
6272677… lmata 14 self.assertEqual(result["_default/baseof.html"], "<!doctype html>")
6272677… lmata 15
6272677… lmata 16 def test_extracts_json_from_prose(self):
6272677… lmata 17 response = 'Here is the converted theme:\n{"_default/baseof.html": "<html></html>"}\nDone.'
6272677… lmata 18 result = _parse_layout_json(response)
6272677… lmata 19 self.assertIn("_default/baseof.html", result)
6272677… lmata 20
6272677… lmata 21 def test_falls_back_on_invalid_json(self):
6272677… lmata 22 result = _parse_layout_json("This is not JSON at all.")
6272677… lmata 23 self.assertIn("_default/baseof.html", result)
6272677… lmata 24 self.assertIn("partials/header.html", result)
6272677… lmata 25 self.assertIn("partials/footer.html", result)
6272677… lmata 26 self.assertIn("index.html", result)
6272677… lmata 27
6272677… lmata 28 def test_fallback_contains_valid_hugo_syntax(self):
6272677… lmata 29 result = _parse_layout_json("not json")
6272677… lmata 30 baseof = result["_default/baseof.html"]
6272677… lmata 31 self.assertIn("block", baseof)
6272677… lmata 32 self.assertIn("partial", baseof)
6272677… lmata 33
6272677… lmata 34
6272677… lmata 35 class TestFallbackBaseof(unittest.TestCase):
6272677… lmata 36 def test_contains_required_hugo_blocks(self):
6272677… lmata 37 result = _fallback_baseof()
6272677… lmata 38 self.assertIn('block "main"', result)
6272677… lmata 39 self.assertIn('partial "header.html"', result)
6272677… lmata 40 self.assertIn('partial "footer.html"', result)
6272677… lmata 41 self.assertIn("<!DOCTYPE html>", result)
6272677… lmata 42
6272677… lmata 43 def test_contains_language_code(self):
6272677… lmata 44 result = _fallback_baseof()
6272677… lmata 45 self.assertIn(".Site.LanguageCode", result)
6272677… lmata 46
6272677… lmata 47
6272677… lmata 48 class TestHugoifyDir(unittest.TestCase):
6272677… lmata 49 def test_valid_theme_passes(self):
6272677… lmata 50 with tempfile.TemporaryDirectory() as tmp:
6272677… lmata 51 baseof = os.path.join(tmp, "layouts", "_default", "baseof.html")
6272677… lmata 52 os.makedirs(os.path.dirname(baseof))
6272677… lmata 53 open(baseof, "w").close()
6272677… lmata 54 result = hugoify_dir(tmp)
6272677… lmata 55 self.assertIn("Valid", result)
6272677… lmata 56
6272677… lmata 57 def test_missing_layouts_fails(self):
6272677… lmata 58 with tempfile.TemporaryDirectory() as tmp:
6272677… lmata 59 result = hugoify_dir(tmp)
6272677… lmata 60 self.assertIn("failed", result.lower())
6272677… lmata 61
6272677… lmata 62 def test_missing_baseof_reports_issue(self):
6272677… lmata 63 with tempfile.TemporaryDirectory() as tmp:
6272677… lmata 64 os.makedirs(os.path.join(tmp, "layouts", "_default"))
6272677… lmata 65 result = hugoify_dir(tmp)
6272677… lmata 66 self.assertIn("baseof.html", result)
6272677… lmata 67
6272677… lmata 68
6272677… lmata 69 if __name__ == "__main__":
6272677… lmata 70 unittest.main()

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button