|
1
|
"""Tests for utils.theme_finder.""" |
|
2
|
import os |
|
3
|
import tempfile |
|
4
|
import unittest |
|
5
|
|
|
6
|
from hugoifier.utils.theme_finder import find_hugo_theme, find_raw_html_files |
|
7
|
|
|
8
|
|
|
9
|
def _make_hugo_theme(base_dir, theme_name="test-theme"): |
|
10
|
"""Create a minimal Hugo theme structure.""" |
|
11
|
layouts = os.path.join(base_dir, theme_name, "layouts", "_default") |
|
12
|
os.makedirs(layouts) |
|
13
|
open(os.path.join(layouts, "baseof.html"), "w").close() |
|
14
|
return os.path.join(base_dir, theme_name) |
|
15
|
|
|
16
|
|
|
17
|
class TestFindHugoTheme(unittest.TestCase): |
|
18
|
def test_finds_theme_with_layouts(self): |
|
19
|
with tempfile.TemporaryDirectory() as tmp: |
|
20
|
theme_path = _make_hugo_theme(tmp) |
|
21
|
result = find_hugo_theme(theme_path) |
|
22
|
self.assertIsNotNone(result) |
|
23
|
self.assertTrue(result["is_hugo_theme"]) |
|
24
|
self.assertEqual(result["theme_name"], "test-theme") |
|
25
|
|
|
26
|
def test_finds_example_site(self): |
|
27
|
with tempfile.TemporaryDirectory() as tmp: |
|
28
|
theme_path = _make_hugo_theme(tmp) |
|
29
|
example = os.path.join(theme_path, "exampleSite") |
|
30
|
os.makedirs(example) |
|
31
|
result = find_hugo_theme(theme_path) |
|
32
|
self.assertEqual(result["example_site"], example) |
|
33
|
|
|
34
|
def test_returns_none_for_non_hugo_dir(self): |
|
35
|
with tempfile.TemporaryDirectory() as tmp: |
|
36
|
os.makedirs(os.path.join(tmp, "css")) |
|
37
|
open(os.path.join(tmp, "index.html"), "w").close() |
|
38
|
result = find_hugo_theme(tmp) |
|
39
|
self.assertIsNone(result) |
|
40
|
|
|
41
|
def test_skips_macosx_dirs(self): |
|
42
|
with tempfile.TemporaryDirectory() as tmp: |
|
43
|
# MACOSX junk should not be mistaken for a theme |
|
44
|
junk = os.path.join(tmp, "__MACOSX", "layouts", "_default") |
|
45
|
os.makedirs(junk) |
|
46
|
result = find_hugo_theme(tmp) |
|
47
|
self.assertIsNone(result) |
|
48
|
|
|
49
|
|
|
50
|
class TestFindRawHtmlFiles(unittest.TestCase): |
|
51
|
def test_finds_html_files(self): |
|
52
|
with tempfile.TemporaryDirectory() as tmp: |
|
53
|
open(os.path.join(tmp, "index.html"), "w").close() |
|
54
|
open(os.path.join(tmp, "about.html"), "w").close() |
|
55
|
result = find_raw_html_files(tmp) |
|
56
|
self.assertEqual(len(result), 2) |
|
57
|
basenames = {os.path.basename(f) for f in result} |
|
58
|
self.assertIn("index.html", basenames) |
|
59
|
self.assertIn("about.html", basenames) |
|
60
|
|
|
61
|
def test_excludes_example_site(self): |
|
62
|
with tempfile.TemporaryDirectory() as tmp: |
|
63
|
example = os.path.join(tmp, "exampleSite") |
|
64
|
os.makedirs(example) |
|
65
|
open(os.path.join(example, "index.html"), "w").close() |
|
66
|
open(os.path.join(tmp, "page.html"), "w").close() |
|
67
|
result = find_raw_html_files(tmp) |
|
68
|
self.assertEqual(len(result), 1) |
|
69
|
self.assertEqual(os.path.basename(result[0]), "page.html") |
|
70
|
|
|
71
|
def test_returns_empty_for_no_html(self): |
|
72
|
with tempfile.TemporaryDirectory() as tmp: |
|
73
|
open(os.path.join(tmp, "style.css"), "w").close() |
|
74
|
result = find_raw_html_files(tmp) |
|
75
|
self.assertEqual(result, []) |
|
76
|
|
|
77
|
|
|
78
|
if __name__ == "__main__": |
|
79
|
unittest.main() |
|
80
|
|