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