Hugoifier

hugoifier / tests / test_complete.py
Blame History Raw 120 lines
1
"""Tests for utils.complete helpers."""
2
import os
3
import tempfile
4
import unittest
5
6
from hugoifier.utils.complete import (
7
_copy_dir,
8
_find_config,
9
_pick_main_html,
10
_write_minimal_hugo_toml,
11
)
12
13
14
class TestPickMainHtml(unittest.TestCase):
15
def test_prefers_index_html(self):
16
files = ["/path/about.html", "/path/index.html", "/path/contact.html"]
17
self.assertEqual(_pick_main_html(files), "/path/index.html")
18
19
def test_prefers_home_html(self):
20
files = ["/path/about.html", "/path/home.html"]
21
self.assertEqual(_pick_main_html(files), "/path/home.html")
22
23
def test_falls_back_to_first(self):
24
files = ["/path/about.html", "/path/services.html"]
25
self.assertEqual(_pick_main_html(files), "/path/about.html")
26
27
28
class TestCopyDir(unittest.TestCase):
29
def test_copies_files(self):
30
with tempfile.TemporaryDirectory() as src, tempfile.TemporaryDirectory() as dst:
31
open(os.path.join(src, "file.txt"), "w").close()
32
_copy_dir(src, dst)
33
self.assertTrue(os.path.exists(os.path.join(dst, "file.txt")))
34
35
def test_excludes_specified_names(self):
36
with tempfile.TemporaryDirectory() as src, tempfile.TemporaryDirectory() as dst:
37
open(os.path.join(src, "keep.txt"), "w").close()
38
open(os.path.join(src, "skip.txt"), "w").close()
39
_copy_dir(src, dst, exclude={"skip.txt"})
40
self.assertTrue(os.path.exists(os.path.join(dst, "keep.txt")))
41
self.assertFalse(os.path.exists(os.path.join(dst, "skip.txt")))
42
43
def test_excludes_dotfiles_starting_with_dot_underscore(self):
44
with tempfile.TemporaryDirectory() as src, tempfile.TemporaryDirectory() as dst:
45
open(os.path.join(src, "._junk"), "w").close()
46
open(os.path.join(src, "real.txt"), "w").close()
47
_copy_dir(src, dst)
48
self.assertFalse(os.path.exists(os.path.join(dst, "._junk")))
49
self.assertTrue(os.path.exists(os.path.join(dst, "real.txt")))
50
51
def test_nonexistent_src_is_safe(self):
52
with tempfile.TemporaryDirectory() as dst:
53
_copy_dir("/nonexistent/src", dst) # should not raise
54
55
def test_recurses_into_subdirs(self):
56
with tempfile.TemporaryDirectory() as src, tempfile.TemporaryDirectory() as dst:
57
nested = os.path.join(src, "sub")
58
os.makedirs(nested)
59
open(os.path.join(nested, "nested.txt"), "w").close()
60
_copy_dir(src, dst)
61
self.assertTrue(os.path.exists(os.path.join(dst, "sub", "nested.txt")))
62
63
64
class TestFindConfig(unittest.TestCase):
65
def test_finds_hugo_toml(self):
66
with tempfile.TemporaryDirectory() as tmp:
67
path = os.path.join(tmp, "hugo.toml")
68
open(path, "w").close()
69
self.assertEqual(_find_config(tmp), path)
70
71
def test_finds_config_toml(self):
72
with tempfile.TemporaryDirectory() as tmp:
73
path = os.path.join(tmp, "config.toml")
74
open(path, "w").close()
75
self.assertEqual(_find_config(tmp), path)
76
77
def test_prefers_hugo_toml_over_config_toml(self):
78
with tempfile.TemporaryDirectory() as tmp:
79
hugo = os.path.join(tmp, "hugo.toml")
80
config = os.path.join(tmp, "config.toml")
81
open(hugo, "w").close()
82
open(config, "w").close()
83
self.assertEqual(_find_config(tmp), hugo)
84
85
def test_finds_nested_config(self):
86
with tempfile.TemporaryDirectory() as tmp:
87
nested = os.path.join(tmp, "config", "_default")
88
os.makedirs(nested)
89
path = os.path.join(nested, "config.toml")
90
open(path, "w").close()
91
self.assertEqual(_find_config(tmp), path)
92
93
def test_returns_none_when_missing(self):
94
with tempfile.TemporaryDirectory() as tmp:
95
self.assertIsNone(_find_config(tmp))
96
97
98
class TestWriteMinimalHugoToml(unittest.TestCase):
99
def test_writes_valid_toml(self):
100
with tempfile.TemporaryDirectory() as tmp:
101
_write_minimal_hugo_toml(tmp, "my-theme")
102
path = os.path.join(tmp, "hugo.toml")
103
self.assertTrue(os.path.exists(path))
104
with open(path) as f:
105
content = f.read()
106
self.assertIn('theme = "my-theme"', content)
107
self.assertIn("My Theme", content)
108
109
def test_sanitizes_quotes_in_theme_name(self):
110
with tempfile.TemporaryDirectory() as tmp:
111
_write_minimal_hugo_toml(tmp, 'evil"theme')
112
with open(os.path.join(tmp, "hugo.toml")) as f:
113
content = f.read()
114
# Should not contain unescaped quote that breaks TOML
115
self.assertNotIn('theme = "evil"theme"', content)
116
117
118
if __name__ == "__main__":
119
unittest.main()
120

Keyboard Shortcuts

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