Skip to content

Commit 5514407

Browse files
committed
Fix tests.
1 parent e54d38e commit 5514407

4 files changed

Lines changed: 521 additions & 496 deletions

File tree

src/render_engine_cli/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def create_collection_entry(
134134
filepath: Path, editor: str | None, content: str | None, collection: Collection, **context
135135
) -> str:
136136
"""Creates a new entry for a collection"""
137-
return collection.content_manager.create_entry(filepath, editor, content, context)
137+
return collection.create_entry(filepath=filepath, editor=editor, content=content, metadata=context)
138138

139139

140140
def split_args(args: list[str] | None) -> dict[str, str]:

tests/test_cli.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,33 @@ def test_import_lib_gets_site():
5555
pass
5656

5757

58-
def test_collection_call():
58+
def test_collection_call(tmp_path_factory):
5959
"""Tests that you can get content from the parser using `new_entry`"""
6060
test_collection = Collection()
61-
content = create_collection_entry(content=None, collection=test_collection, foo="bar")
62-
post = frontmatter.loads(content)
61+
content_path1 = tmp_path_factory.getbasetemp()
62+
filepath = content_path1 / "test.md"
63+
content = create_collection_entry(
64+
collection=test_collection, content=None, foo="bar", filepath=filepath, editor=None
65+
)
66+
assert content.strip() == f"New entry created at {filepath} ."
6367

68+
post = frontmatter.loads(filepath.read_text())
69+
print(filepath.read_text())
6470
assert post["title"] == "Untitled Entry"
6571
assert post["foo"] == "bar"
6672

6773

68-
def test_collection_call_with_content():
74+
def test_collection_call_with_content(tmp_path_factory):
6975
"""Tests that you can get content from the parser using `new_entry`"""
7076
test_collection = Collection()
71-
content = create_collection_entry(content="This is a test", collection=test_collection, foo="bar")
72-
post = frontmatter.loads(content)
77+
content_path1 = tmp_path_factory.getbasetemp()
78+
filepath = content_path1 / "test.md"
79+
create_collection_entry(
80+
content="This is a test", collection=test_collection, foo="bar", filepath=filepath, editor=None
81+
)
82+
print(filepath.read_text())
83+
84+
post = frontmatter.loads(filepath.read_text())
7385

7486
assert post["title"] == "Untitled Entry"
7587
assert post["foo"] == "bar"
@@ -148,16 +160,21 @@ def test_config_loading_invalid_file(tmp_path, monkeypatch, capsys):
148160
CliConfig().load_config(str(config_file))
149161

150162

151-
def test_collection_entry_with_custom_attributes():
163+
def test_collection_entry_with_custom_attributes(tmp_path_factory):
152164
"""Tests that custom attributes are passed through to collection entry"""
153165
test_collection = Collection()
154-
content = create_collection_entry(
166+
content_path1 = tmp_path_factory.getbasetemp()
167+
filepath = content_path1 / "test.md"
168+
169+
create_collection_entry(
155170
content="Test content",
156171
collection=test_collection,
157172
author="Test Author",
158173
tags="test,example",
174+
filepath=filepath,
175+
editor=None,
159176
)
160-
post = frontmatter.loads(content)
177+
post = frontmatter.loads(filepath.read_text())
161178

162179
assert post["author"] == "Test Author"
163180
assert post["tags"] == "test,example"

tests/test_cli_commands.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ def test_new_entry_command_success(runner, test_site_module, monkeypatch):
162162
assert result.exit_code == 0, result.output
163163
mock_create_entry.assert_called_once()
164164

165-
# Check that the file was created
166-
created_file = content_dir / "test.md"
167-
assert created_file.exists()
168-
169165

170166
def test_new_entry_command_with_args(runner, test_site_module, monkeypatch):
171167
"""Tests new_entry command with --args parameter"""
@@ -227,7 +223,12 @@ def test_new_entry_command_missing_required_args(runner):
227223
"slug": "slug1",
228224
"content": "content",
229225
},
230-
{"date": datetime.datetime.fromisoformat("2025-05-23T00:00:00"), "slug": "slug1", "content": "content"},
226+
{
227+
"date": datetime.datetime.fromisoformat("2025-05-23T00:00:00"),
228+
"slug": "slug1",
229+
"content": "content",
230+
"title": "New Entry",
231+
},
231232
),
232233
(
233234
{
@@ -239,7 +240,12 @@ def test_new_entry_command_missing_required_args(runner):
239240
"content": "content",
240241
"include-date": True,
241242
},
242-
{"date": datetime.datetime.fromisoformat("2025-05-23T00:00:00"), "slug": "slug1", "content": "content"},
243+
{
244+
"date": datetime.datetime.fromisoformat("2025-05-23T00:00:00"),
245+
"slug": "slug1",
246+
"content": "content",
247+
"title": "New Entry",
248+
},
243249
),
244250
(
245251
{
@@ -249,7 +255,7 @@ def test_new_entry_command_missing_required_args(runner):
249255
"slug": "slug1",
250256
"content": "content",
251257
},
252-
{"slug": "slug1", "content": "content"},
258+
{"slug": "slug1", "content": "content", "title": "New Entry"},
253259
),
254260
],
255261
)
@@ -290,12 +296,15 @@ def mock_create_collection_entry(**kwargs):
290296
else:
291297
formatted_options.extend([f"--{key}", value])
292298

293-
print(formatted_options)
294-
result = runner.invoke(app, ["new-entry", "--module-site", module_site, *formatted_options])
295-
print(result.output)
299+
runner.invoke(app, ["new-entry", "--module-site", module_site, *formatted_options])
296300
# Pop the collection from the passed arguments since it's not relevant.
297301
passed_args.pop("collection", None)
298302

303+
# We can remove the editor because we're not actually testing with that.
304+
passed_args.pop("editor")
305+
306+
assert passed_args.pop("filepath") == content_dir / options["filename"]
307+
299308
# include_date needs some special handling.
300309
if "include_date" in options:
301310
if not any(arg.startswith("date=") or arg.startswith("date:") for arg in options.get("args", [])):

0 commit comments

Comments
 (0)