-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_cli_commands.py
More file actions
380 lines (309 loc) · 12.7 KB
/
test_cli_commands.py
File metadata and controls
380 lines (309 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import datetime
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
from click.testing import CliRunner
from render_engine_cli.cli import app
@pytest.fixture
def runner():
"""CLI test runner fixture"""
yield CliRunner()
@pytest.fixture
def test_site_module(tmp_path):
"""Creates a test site module file"""
site_content = """
from render_engine import Site, Page, Collection
site = Site()
site.output_path = "output"
@site.page
class TestPage(Page):
content = "Test content"
@site.collection
class TestCollection(Collection):
content_path = "content"
"""
site_file = tmp_path / "test_app.py"
site_file.write_text(site_content)
yield tmp_path, "test_app:site"
def test_build_command_success(test_site_module, monkeypatch, runner):
"""Tests build command with valid module:site"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
mock_site = Mock()
mock_site.render = Mock()
mock_get_site = Mock()
mock_get_site.return_value = mock_site
monkeypatch.setattr("render_engine_cli.cli.get_site", mock_get_site)
result = runner.invoke(app, ["build", "--module-site", module_site])
assert result.exit_code == 0
mock_get_site.assert_called_once_with("test_app", "site")
mock_site.render.assert_called_once()
def test_build_command_with_clean(runner, test_site_module, monkeypatch):
"""Tests build command with --clean flag"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
with (
patch("render_engine_cli.cli.get_site") as mock_get_site,
patch("render_engine_cli.cli.remove_output_folder") as mock_remove,
):
mock_site = Mock()
mock_site.render = Mock()
mock_site.output_path = "output"
mock_get_site.return_value = mock_site
result = runner.invoke(app, ["build", "--module-site", module_site, "--clean"])
assert result.exit_code == 0
mock_remove.assert_called_once()
mock_site.render.assert_called_once()
def test_build_command_invalid_module_site(runner):
"""Tests build command with invalid module:site format"""
result = runner.invoke(app, ["build", "invalid_format"])
assert result.exit_code != 0
assert "Error: Invalid value for '--module-site': module_site must be in the form of module:site" in result.output
def test_templates_command_success(runner, test_site_module, monkeypatch):
"""Tests templates command with valid module:site"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
with patch("render_engine_cli.cli.get_site") as mock_get_site:
mock_site = Mock()
mock_theme_manager = Mock()
mock_prefix = Mock()
mock_prefix.list_templates.return_value = ["template1.html", "template2.html"]
mock_theme_manager.prefix = {"test_theme": mock_prefix}
mock_site.theme_manager = mock_theme_manager
mock_get_site.return_value = mock_site
result = runner.invoke(app, ["templates", "--module-site", module_site, "--theme-name", "test_theme"])
assert result.exit_code == 0, result.output
mock_get_site.assert_called_once_with("test_app", "site")
def test_templates_command_no_theme_specified(runner, test_site_module, monkeypatch):
"""Tests templates command without theme name (lists all themes)"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
with patch("render_engine_cli.cli.get_site") as mock_get_site:
mock_site = Mock()
mock_theme_manager = Mock()
mock_prefix = Mock()
mock_prefix.list_templates.return_value = ["template1.html", "template2.html"]
mock_theme_manager.prefix = {"theme1": mock_prefix, "theme2": mock_prefix}
mock_site.theme_manager = mock_theme_manager
mock_get_site.return_value = mock_site
result = runner.invoke(app, ["templates", "--module-site", module_site])
assert result.exit_code == 0, result.output
assert "No theme name specified" in result.output
def test_new_entry_command_success(runner, test_site_module, monkeypatch):
"""Tests new_entry command with valid parameters"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
# Create content directory
content_dir = tmp_path / "content"
content_dir.mkdir()
with (
patch("render_engine_cli.cli.get_site") as mock_get_site,
patch("render_engine_cli.cli.create_collection_entry") as mock_create_entry,
):
mock_site = Mock()
mock_collection = Mock()
mock_collection.content_path = str(content_dir)
type(mock_collection).__name__ = "TestCollection"
mock_site.route_list = {"test": mock_collection}
mock_get_site.return_value = mock_site
mock_create_entry.return_value = "---\ntitle: Test Entry\n---\nTest content"
result = runner.invoke(
app,
[
"new-entry",
"--module-site",
module_site,
"--collection",
"testcollection",
"-f",
"test.md",
"--content",
"Test content",
],
)
assert result.exit_code == 0, result.output
mock_create_entry.assert_called_once()
# Check that the file was created
created_file = content_dir / "test.md"
assert created_file.exists()
def test_new_entry_command_with_args(runner, test_site_module, monkeypatch):
"""Tests new_entry command with --args parameter"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
content_dir = tmp_path / "content"
content_dir.mkdir()
with (
patch("render_engine_cli.cli.get_site") as mock_get_site,
patch("render_engine_cli.cli.create_collection_entry") as mock_create_entry,
):
mock_site = Mock()
mock_collection = Mock()
mock_collection.content_path = str(content_dir)
type(mock_collection).__name__ = "TestCollection"
mock_site.route_list = {"test": mock_collection}
mock_get_site.return_value = mock_site
mock_create_entry.return_value = "---\ntitle: Custom Title\nauthor: Test Author\n---\nTest content"
result = runner.invoke(
app,
[
"new-entry",
"--module-site",
module_site,
"--collection",
"testcollection",
"-f",
"test.md",
"--args",
"author=Test Author",
"--title",
"Custom Title",
],
)
assert result.exit_code == 0, result.output
mock_create_entry.assert_called_once()
def test_new_entry_command_missing_required_args(runner):
"""Tests new_entry command with missing required arguments"""
result = runner.invoke(app, ["new-entry"])
assert result.exit_code != 0
@pytest.mark.parametrize(
"options, expected",
[
(
{
"collection": "testcollection",
"filename": "test.md",
"args": ["date=May 23, 2025"],
"title": "New Entry",
"slug": "slug1",
"content": "content",
},
{"date": datetime.datetime.fromisoformat("2025-05-23T00:00:00"), "slug": "slug1", "content": "content"},
),
(
{
"collection": "testcollection",
"filename": "test.md",
"args": ["date=May 23, 2025"],
"title": "New Entry",
"slug": "slug1",
"content": "content",
"include-date": True,
},
{"date": datetime.datetime.fromisoformat("2025-05-23T00:00:00"), "slug": "slug1", "content": "content"},
),
(
{
"collection": "testcollection",
"filename": "test.md",
"title": "New Entry",
"slug": "slug1",
"content": "content",
},
{"slug": "slug1", "content": "content"},
),
],
)
def test_new_entry_date_options(options, expected, monkeypatch, test_site_module, runner):
"""Test arg parsing and handling is correct"""
passed_args = {}
def mock_create_collection_entry(**kwargs):
"""Preserve the arguments passed to create_collection_entry for inspection"""
print("In the mock!")
nonlocal passed_args
passed_args = kwargs
return "---\ntitle: Custom Title\nauthor: Test Author\n---\nTest content"
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
monkeypatch.setattr("render_engine_cli.cli.create_collection_entry", mock_create_collection_entry)
content_dir = tmp_path / "content"
content_dir.mkdir()
with (
patch("render_engine_cli.cli.get_site") as mock_get_site,
):
mock_site = Mock()
mock_collection = Mock()
mock_collection.content_path = str(content_dir)
type(mock_collection).__name__ = "TestCollection"
mock_site.route_list = {"test": mock_collection}
mock_get_site.return_value = mock_site
formatted_options = list()
for key, value in options.items():
if key == "args":
for arg in value:
formatted_options.extend([f"--{key}", arg])
continue
if key == "include-date":
formatted_options.append(f"--{key}")
else:
formatted_options.extend([f"--{key}", value])
print(formatted_options)
result = runner.invoke(app, ["new-entry", "--module-site", module_site, *formatted_options])
print(result.output)
# Pop the collection from the passed arguments since it's not relevant.
passed_args.pop("collection", None)
# include_date needs some special handling.
if "include_date" in options:
if not any(arg.startswith("date=") or arg.startswith("date:") for arg in options.get("args", [])):
assert "date" in passed_args
assert (
datetime.datetime.fromisoformat(passed_args.pop("date")).date() == datetime.datetime.today().date()
)
assert passed_args == expected
def test_new_entry_content_and_content_file(runner):
"""Test failure with both content and content-file"""
result = runner.invoke(
app,
[
"new-entry",
"app:app",
"testcollection",
"test.md",
"--content",
"lorem ipsum",
"--content-file",
"llama.txt",
],
)
assert result.exit_code != 0
def test_serve_command_basic(runner, test_site_module, monkeypatch):
"""Tests serve command basic functionality"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
with (
patch("render_engine_cli.cli.get_site") as mock_get_site,
patch("render_engine_cli.cli.ServerEventHandler") as mock_handler_class,
):
mock_site = Mock()
mock_site.render = Mock()
mock_site.output_path = "output"
mock_get_site.return_value = mock_site
mock_handler = Mock()
mock_handler.__enter__ = Mock(return_value=mock_handler)
mock_handler.__exit__ = Mock(return_value=None)
mock_handler_class.return_value = mock_handler
result = runner.invoke(app, ["serve", "--module-site", module_site, "--port", "8080"])
assert result.exit_code == 0, result.output
mock_get_site.assert_called_once_with("test_app", "site")
mock_site.render.assert_called_once()
mock_handler_class.assert_called_once()
def test_serve_command_with_reload(runner, test_site_module, monkeypatch):
"""Tests serve command with --reload flag"""
tmp_path, module_site = test_site_module
monkeypatch.chdir(tmp_path)
with (
patch("render_engine_cli.cli.get_site") as mock_get_site,
patch("render_engine_cli.cli.ServerEventHandler") as mock_handler_class,
patch("render_engine_cli.cli.get_site_content_paths") as mock_get_paths,
):
mock_site = Mock()
mock_site.render = Mock()
mock_site.output_path = "output"
mock_get_site.return_value = mock_site
mock_get_paths.return_value = [Path("content")]
mock_handler = Mock()
mock_handler.__enter__ = Mock(return_value=mock_handler)
mock_handler.__exit__ = Mock(return_value=None)
mock_handler_class.return_value = mock_handler
result = runner.invoke(app, ["serve", "--module-site", module_site, "--reload"])
assert result.exit_code == 0, result.output
mock_get_paths.assert_called_once_with(mock_site)