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