fix: templates are not copied automatically to projects

This commit is contained in:
2026-04-25 07:25:56 +02:00
parent 2296ff0e99
commit 6d86d0ce3f
11 changed files with 160 additions and 107 deletions

View File

@@ -254,6 +254,60 @@ defmodule BDS.GenerationTest do
assert post_html =~ "Language"
end
test "generation falls back to bundled default templates when the project has no template files or template rows",
%{project: project, temp_dir: temp_dir} do
File.rm_rf!(Path.join(temp_dir, "templates"))
Repo.delete_all(
from template in BDS.Templates.Template,
where: template.project_id == ^project.id
)
assert {:ok, _menu} =
BDS.Menu.update_menu(project.id, [
%{kind: :page, label: "Notes", slug: "notes"}
])
assert {:ok, _metadata} =
Metadata.update_project_metadata(project.id, %{
public_url: "https://example.com/blog",
main_language: "en",
blog_languages: ["en", "de"]
})
assert {:ok, post} =
Posts.create_post(%{
project_id: project.id,
title: "Bundled Rendered Post",
content: "**Rendered** body",
language: "en",
categories: ["notes"],
tags: ["Elixir"]
})
assert {:ok, published_post} = Posts.publish_post(post.id)
assert {:ok, _tags} = BDS.Tags.sync_tags_from_posts(project.id)
assert {:ok, result} = BDS.Generation.generate_site(project.id, [:core, :single])
post_path = BDS.Generation.post_output_path(published_post)
relative_paths = Enum.map(result.generated_files, & &1.relative_path)
assert "index.html" in relative_paths
assert post_path in relative_paths
index_html = File.read!(Path.join([temp_dir, "html", "index.html"]))
assert index_html =~ ~s(<nav class="blog-menu">)
assert index_html =~ ~s(/assets/pico.min.css)
assert index_html =~ "Bundled Rendered Post"
post_html = File.read!(Path.join([temp_dir, "html", post_path]))
assert post_html =~ ~s(data-template="single-post")
assert post_html =~ ~s(<strong>Rendered</strong> body)
assert post_html =~ "Taxonomy"
assert post_html =~ "Language"
end
test "generation expands starter-template markdown macros, rewrites canonical post links, media links, and emits not-found page",
%{project: project, temp_dir: temp_dir} do
assert {:ok, _metadata} =

View File

@@ -123,7 +123,7 @@ defmodule BDS.MaintenanceTest do
assert length(scripts) == 1
assert {:ok, templates} = BDS.Maintenance.rebuild_from_filesystem(project.id, "template")
assert length(templates) == 4
assert length(templates) == 1
assert Repo.get(BDS.Posts.Post, "dispatch-post") != nil
assert Repo.get(BDS.Media.Media, "dispatch-media") != nil

View File

@@ -43,7 +43,7 @@ defmodule BDS.ProjectsTest do
assert second.is_active == false
end
test "create_project installs starter templates into the project data directory", %{
test "create_project leaves the project templates directory empty by default", %{
temp_root: temp_root
} do
temp_dir = Path.join(temp_root, "starter")
@@ -52,12 +52,12 @@ defmodule BDS.ProjectsTest do
assert {:ok, project} =
BDS.Projects.create_project(%{name: "Starter Blog", data_path: temp_dir})
assert File.exists?(Path.join([temp_dir, "templates", "single-post.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "post-list.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "not-found.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "partials", "head.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "partials", "menu-items.liquid"]))
assert File.exists?(Path.join([temp_dir, "templates", "macros", "gallery.liquid"]))
refute File.exists?(Path.join([temp_dir, "templates", "single-post.liquid"]))
refute File.exists?(Path.join([temp_dir, "templates", "post-list.liquid"]))
refute File.exists?(Path.join([temp_dir, "templates", "not-found.liquid"]))
refute File.exists?(Path.join([temp_dir, "templates", "partials", "head.liquid"]))
refute File.exists?(Path.join([temp_dir, "templates", "partials", "menu-items.liquid"]))
refute File.exists?(Path.join([temp_dir, "templates", "macros", "gallery.liquid"]))
starter_slugs =
Repo.all(
@@ -66,34 +66,9 @@ defmodule BDS.ProjectsTest do
select: {template.slug, template.kind}
)
assert {"single-post", :post} in starter_slugs
assert {"post-list", :list} in starter_slugs
assert {"not-found", :not_found} in starter_slugs
end
test "starter template installation is idempotent for existing top-level templates", %{
temp_root: temp_root
} do
temp_dir = Path.join(temp_root, "idempotent-starter")
File.mkdir_p!(temp_dir)
assert {:ok, project} =
BDS.Projects.create_project(%{name: "Starter Blog", data_path: temp_dir})
template_path = Path.join([temp_dir, "templates", "single-post.liquid"])
original_contents = File.read!(template_path)
assert {:ok, %{fields: original_fields}} = BDS.Frontmatter.parse_document(original_contents)
assert is_binary(original_fields["id"])
assert :ok = BDS.StarterTemplates.install(project)
reinstalled_contents = File.read!(template_path)
assert reinstalled_contents == original_contents
assert {:ok, %{fields: reinstalled_fields}} =
BDS.Frontmatter.parse_document(reinstalled_contents)
assert reinstalled_fields["id"] == original_fields["id"]
refute {"single-post", :post} in starter_slugs
refute {"post-list", :list} in starter_slugs
refute {"not-found", :not_found} in starter_slugs
end
test "set_active_project clears the previous active project and activates the target", %{
@@ -161,7 +136,7 @@ defmodule BDS.ProjectsTest do
_ = File.rm_rf(internal_dir)
end)
assert File.exists?(Path.join(internal_dir, "templates/single-post.liquid"))
refute File.exists?(Path.join(internal_dir, "templates/single-post.liquid"))
assert {:ok, deleted_internal_project} = BDS.Projects.delete_project(internal_project.id)
assert deleted_internal_project.id == internal_project.id

View File

@@ -246,7 +246,7 @@ defmodule BDS.TemplatesTest do
)
assert {:ok, templates} = BDS.Templates.rebuild_templates_from_files(project.id)
assert length(templates) == 4
assert length(templates) == 1
template = Repo.get!(BDS.Templates.Template, "template-from-file")
assert template.id == "template-from-file"