|
1
|
"""Tests for boilerworks.renderer.""" |
|
2
|
|
|
3
|
from __future__ import annotations |
|
4
|
|
|
5
|
from pathlib import Path |
|
6
|
|
|
7
|
from boilerworks.renderer import ( |
|
8
|
_SKIP_DIRS, |
|
9
|
_SKIP_EXTENSIONS, |
|
10
|
build_replacements, |
|
11
|
rename_boilerworks_paths, |
|
12
|
render_directory, |
|
13
|
render_file, |
|
14
|
) |
|
15
|
|
|
16
|
|
|
17
|
class TestBuildReplacements: |
|
18
|
def test_lowercase_replacement(self) -> None: |
|
19
|
r = build_replacements("my-app") |
|
20
|
assert r["boilerworks"] == "my-app" |
|
21
|
|
|
22
|
def test_uppercase_replacement(self) -> None: |
|
23
|
r = build_replacements("my-app") |
|
24
|
assert r["BOILERWORKS"] == "MY-APP" |
|
25
|
|
|
26
|
def test_title_replacement(self) -> None: |
|
27
|
r = build_replacements("my-app") |
|
28
|
assert r["Boilerworks"] == "My-App" |
|
29
|
|
|
30
|
def test_underscore_prefix_replacement(self) -> None: |
|
31
|
r = build_replacements("my-app") |
|
32
|
assert r["boilerworks_"] == "my_app_" |
|
33
|
|
|
34
|
def test_underscore_suffix_replacement(self) -> None: |
|
35
|
r = build_replacements("my-app") |
|
36
|
assert r["_boilerworks"] == "_my_app" |
|
37
|
|
|
38
|
def test_single_word_project(self) -> None: |
|
39
|
r = build_replacements("myapp") |
|
40
|
assert r["boilerworks"] == "myapp" |
|
41
|
assert r["BOILERWORKS"] == "MYAPP" |
|
42
|
|
|
43
|
|
|
44
|
class TestRenderFile: |
|
45
|
def test_replaces_lowercase(self, tmp_path: Path) -> None: |
|
46
|
f = tmp_path / "test.txt" |
|
47
|
f.write_text("APP_NAME=boilerworks") |
|
48
|
replacements = build_replacements("my-app") |
|
49
|
changed = render_file(f, replacements) |
|
50
|
assert changed is True |
|
51
|
assert f.read_text() == "APP_NAME=my-app" |
|
52
|
|
|
53
|
def test_replaces_uppercase(self, tmp_path: Path) -> None: |
|
54
|
f = tmp_path / "test.env" |
|
55
|
f.write_text("DB_NAME=BOILERWORKS_DB") |
|
56
|
replacements = build_replacements("my-app") |
|
57
|
render_file(f, replacements) |
|
58
|
assert f.read_text() == "DB_NAME=MY-APP_DB" |
|
59
|
|
|
60
|
def test_no_match_returns_false(self, tmp_path: Path) -> None: |
|
61
|
f = tmp_path / "test.txt" |
|
62
|
f.write_text("hello world") |
|
63
|
changed = render_file(f, build_replacements("my-app")) |
|
64
|
assert changed is False |
|
65
|
assert f.read_text() == "hello world" |
|
66
|
|
|
67
|
def test_empty_file_unchanged(self, tmp_path: Path) -> None: |
|
68
|
f = tmp_path / "empty.txt" |
|
69
|
f.write_text("") |
|
70
|
changed = render_file(f, build_replacements("my-app")) |
|
71
|
assert changed is False |
|
72
|
assert f.read_text() == "" |
|
73
|
|
|
74
|
def test_binary_extension_skipped(self, tmp_path: Path) -> None: |
|
75
|
f = tmp_path / "image.png" |
|
76
|
f.write_bytes(b"\x89PNG boilerworks") |
|
77
|
changed = render_file(f, build_replacements("my-app")) |
|
78
|
assert changed is False |
|
79
|
# File content unchanged |
|
80
|
assert b"boilerworks" in f.read_bytes() |
|
81
|
|
|
82
|
def test_lock_extension_skipped(self, tmp_path: Path) -> None: |
|
83
|
f = tmp_path / "uv.lock" |
|
84
|
f.write_text("boilerworks = 1.0") |
|
85
|
changed = render_file(f, build_replacements("my-app")) |
|
86
|
assert changed is False |
|
87
|
|
|
88
|
def test_multiple_replacements_in_one_file(self, tmp_path: Path) -> None: |
|
89
|
f = tmp_path / "settings.py" |
|
90
|
content = "APP = 'boilerworks'\nNAME = 'Boilerworks'\nKEY = 'BOILERWORKS'" |
|
91
|
f.write_text(content) |
|
92
|
render_file(f, build_replacements("cool-app")) |
|
93
|
result = f.read_text() |
|
94
|
assert "cool-app" in result |
|
95
|
assert "Cool-App" in result |
|
96
|
assert "COOL-APP" in result |
|
97
|
assert "boilerworks" not in result |
|
98
|
|
|
99
|
def test_case_variant_python_module(self, tmp_path: Path) -> None: |
|
100
|
f = tmp_path / "settings.py" |
|
101
|
f.write_text("from boilerworks_config import settings") |
|
102
|
replacements = build_replacements("myproject") |
|
103
|
render_file(f, replacements) |
|
104
|
assert f.read_text() == "from myproject_config import settings" |
|
105
|
|
|
106
|
|
|
107
|
class TestRenderDirectory: |
|
108
|
def test_renders_text_files(self, tmp_path: Path) -> None: |
|
109
|
(tmp_path / "app.py").write_text("APP = 'boilerworks'") |
|
110
|
(tmp_path / "README.md").write_text("# boilerworks\n") |
|
111
|
modified = render_directory(tmp_path, build_replacements("newapp")) |
|
112
|
assert len(modified) == 2 |
|
113
|
|
|
114
|
def test_skips_excluded_dirs(self, tmp_path: Path) -> None: |
|
115
|
skip_dir = tmp_path / "node_modules" |
|
116
|
skip_dir.mkdir() |
|
117
|
(skip_dir / "file.js").write_text("module.exports = 'boilerworks'") |
|
118
|
modified = render_directory(tmp_path, build_replacements("newapp")) |
|
119
|
assert not any("node_modules" in str(p) for p in modified) |
|
120
|
# Original file unchanged |
|
121
|
assert "boilerworks" in (skip_dir / "file.js").read_text() |
|
122
|
|
|
123
|
def test_skips_git_dir(self, tmp_path: Path) -> None: |
|
124
|
git_dir = tmp_path / ".git" |
|
125
|
git_dir.mkdir() |
|
126
|
(git_dir / "config").write_text("url = boilerworks") |
|
127
|
modified = render_directory(tmp_path, build_replacements("newapp")) |
|
128
|
assert not any(".git" in str(p) for p in modified) |
|
129
|
|
|
130
|
def test_skips_binary_extensions(self, tmp_path: Path) -> None: |
|
131
|
(tmp_path / "logo.png").write_bytes(b"\x89PNG boilerworks") |
|
132
|
(tmp_path / "app.py").write_text("# boilerworks") |
|
133
|
modified = render_directory(tmp_path, build_replacements("newapp")) |
|
134
|
modified_names = [p.name for p in modified] |
|
135
|
assert "logo.png" not in modified_names |
|
136
|
assert "app.py" in modified_names |
|
137
|
|
|
138
|
def test_nested_directories(self, tmp_path: Path) -> None: |
|
139
|
subdir = tmp_path / "src" / "config" |
|
140
|
subdir.mkdir(parents=True) |
|
141
|
(subdir / "settings.py").write_text("APP = 'boilerworks'") |
|
142
|
modified = render_directory(tmp_path, build_replacements("newapp")) |
|
143
|
assert len(modified) == 1 |
|
144
|
assert (subdir / "settings.py").read_text() == "APP = 'newapp'" |
|
145
|
|
|
146
|
|
|
147
|
class TestRenameBoilerworksPaths: |
|
148
|
def test_renames_file_with_boilerworks(self, tmp_path: Path) -> None: |
|
149
|
f = tmp_path / "boilerworks.iml" |
|
150
|
f.write_text("") |
|
151
|
renames = rename_boilerworks_paths(tmp_path, "my-project") |
|
152
|
assert len(renames) == 1 |
|
153
|
assert not (tmp_path / "boilerworks.iml").exists() |
|
154
|
assert (tmp_path / "my-project.iml").exists() |
|
155
|
|
|
156
|
def test_renames_directory_with_boilerworks(self, tmp_path: Path) -> None: |
|
157
|
subdir = tmp_path / "boilerworks_config" |
|
158
|
subdir.mkdir() |
|
159
|
(subdir / "settings.py").write_text("") |
|
160
|
rename_boilerworks_paths(tmp_path, "myproject") |
|
161
|
assert not (tmp_path / "boilerworks_config").exists() |
|
162
|
assert (tmp_path / "myproject_config").exists() |
|
163
|
|
|
164
|
def test_no_boilerworks_paths_unchanged(self, tmp_path: Path) -> None: |
|
165
|
f = tmp_path / "other_file.txt" |
|
166
|
f.write_text("content") |
|
167
|
renames = rename_boilerworks_paths(tmp_path, "myproject") |
|
168
|
assert len(renames) == 0 |
|
169
|
assert f.exists() |
|
170
|
|
|
171
|
|
|
172
|
class TestSkipSets: |
|
173
|
def test_skip_dirs_contains_expected(self) -> None: |
|
174
|
assert ".git" in _SKIP_DIRS |
|
175
|
assert "node_modules" in _SKIP_DIRS |
|
176
|
assert "__pycache__" in _SKIP_DIRS |
|
177
|
assert ".venv" in _SKIP_DIRS |
|
178
|
|
|
179
|
def test_skip_extensions_contains_expected(self) -> None: |
|
180
|
assert ".png" in _SKIP_EXTENSIONS |
|
181
|
assert ".lock" in _SKIP_EXTENSIONS |
|
182
|
assert ".pyc" in _SKIP_EXTENSIONS |
|
183
|
assert ".woff" in _SKIP_EXTENSIONS |
|
184
|
|