|
1
|
"""Tests for utils.theme_patcher.""" |
|
2
|
import os |
|
3
|
import tempfile |
|
4
|
import unittest |
|
5
|
|
|
6
|
from hugoifier.utils.theme_patcher import patch_config, patch_theme |
|
7
|
|
|
8
|
|
|
9
|
class TestPatchTheme(unittest.TestCase): |
|
10
|
def _write_layout(self, layouts_dir, name, content): |
|
11
|
path = os.path.join(layouts_dir, name) |
|
12
|
with open(path, "w") as f: |
|
13
|
f.write(content) |
|
14
|
return path |
|
15
|
|
|
16
|
def test_patches_disqus_shortname(self): |
|
17
|
with tempfile.TemporaryDirectory() as tmp: |
|
18
|
layouts = os.path.join(tmp, "layouts") |
|
19
|
os.makedirs(layouts) |
|
20
|
self._write_layout(layouts, "single.html", "{{ .Site.DisqusShortname }}") |
|
21
|
patch_theme(tmp) |
|
22
|
with open(os.path.join(layouts, "single.html")) as f: |
|
23
|
result = f.read() |
|
24
|
self.assertIn(".Site.Config.Services.Disqus.Shortname", result) |
|
25
|
self.assertNotIn(".Site.DisqusShortname", result) |
|
26
|
|
|
27
|
def test_patches_google_analytics(self): |
|
28
|
with tempfile.TemporaryDirectory() as tmp: |
|
29
|
layouts = os.path.join(tmp, "layouts") |
|
30
|
os.makedirs(layouts) |
|
31
|
self._write_layout(layouts, "head.html", "{{ .Site.GoogleAnalytics }}") |
|
32
|
patch_theme(tmp) |
|
33
|
with open(os.path.join(layouts, "head.html")) as f: |
|
34
|
result = f.read() |
|
35
|
self.assertIn(".Site.Config.Services.GoogleAnalytics.ID", result) |
|
36
|
|
|
37
|
def test_no_change_when_already_patched(self): |
|
38
|
with tempfile.TemporaryDirectory() as tmp: |
|
39
|
layouts = os.path.join(tmp, "layouts") |
|
40
|
os.makedirs(layouts) |
|
41
|
content = "{{ .Site.Config.Services.Disqus.Shortname }}" |
|
42
|
self._write_layout(layouts, "single.html", content) |
|
43
|
patch_theme(tmp) |
|
44
|
with open(os.path.join(layouts, "single.html")) as f: |
|
45
|
result = f.read() |
|
46
|
self.assertEqual(result, content) |
|
47
|
|
|
48
|
def test_no_layouts_dir_is_safe(self): |
|
49
|
with tempfile.TemporaryDirectory() as tmp: |
|
50
|
# Should not raise even if layouts/ doesn't exist |
|
51
|
patch_theme(tmp) |
|
52
|
|
|
53
|
|
|
54
|
class TestPatchConfig(unittest.TestCase): |
|
55
|
def _write_config(self, path, content): |
|
56
|
with open(path, "w") as f: |
|
57
|
f.write(content) |
|
58
|
|
|
59
|
def test_patches_paginate(self): |
|
60
|
with tempfile.TemporaryDirectory() as tmp: |
|
61
|
cfg = os.path.join(tmp, "hugo.toml") |
|
62
|
self._write_config(cfg, 'paginate = 10\ntitle = "My Site"\n') |
|
63
|
patch_config(cfg) |
|
64
|
with open(cfg) as f: |
|
65
|
result = f.read() |
|
66
|
self.assertIn("[pagination]", result) |
|
67
|
self.assertIn("pagerSize = 10", result) |
|
68
|
self.assertNotIn("paginate = 10", result) |
|
69
|
|
|
70
|
def test_patches_google_analytics_key(self): |
|
71
|
with tempfile.TemporaryDirectory() as tmp: |
|
72
|
cfg = os.path.join(tmp, "hugo.toml") |
|
73
|
self._write_config(cfg, 'googleAnalytics = "UA-12345"\n') |
|
74
|
patch_config(cfg) |
|
75
|
with open(cfg) as f: |
|
76
|
result = f.read() |
|
77
|
self.assertIn("[services.googleAnalytics]", result) |
|
78
|
self.assertIn('id = "UA-12345"', result) |
|
79
|
|
|
80
|
def test_patches_disqus_shortname(self): |
|
81
|
with tempfile.TemporaryDirectory() as tmp: |
|
82
|
cfg = os.path.join(tmp, "hugo.toml") |
|
83
|
self._write_config(cfg, 'disqusShortname = "mysite"\n') |
|
84
|
patch_config(cfg) |
|
85
|
with open(cfg) as f: |
|
86
|
result = f.read() |
|
87
|
self.assertIn("[services.disqus]", result) |
|
88
|
self.assertIn('shortname = "mysite"', result) |
|
89
|
|
|
90
|
def test_no_change_when_nothing_to_patch(self): |
|
91
|
with tempfile.TemporaryDirectory() as tmp: |
|
92
|
cfg = os.path.join(tmp, "hugo.toml") |
|
93
|
content = 'title = "Clean Site"\n' |
|
94
|
self._write_config(cfg, content) |
|
95
|
patch_config(cfg) |
|
96
|
with open(cfg) as f: |
|
97
|
result = f.read() |
|
98
|
self.assertEqual(result, content) |
|
99
|
|
|
100
|
|
|
101
|
if __name__ == "__main__": |
|
102
|
unittest.main() |
|
103
|
|