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