package site
import (
"os"
"path/filepath"
"strings"
"testing"
)
// writeFile writes content to a file under dir, creating parent directories.
func writeFile(t *testing.T, dir, rel, content string) {
t.Helper()
path := filepath.Join(dir, rel)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
// A page is wrapped in the base layout with shared partials, and static assets
// are copied through.
func TestBuild(t *testing.T) {
src := t.TempDir()
out := t.TempDir()
writeFile(t, src, "base.html",
`{{define "base"}}
{{template "title" .}}`+
`{{template "header" .}}{{template "content" .}}{{template "footer" .}}{{end}}`)
writeFile(t, src, "partials/header.html", `{{define "header"}}{{end}}`)
writeFile(t, src, "partials/footer.html", `{{define "footer"}}{{end}}`)
writeFile(t, src, "pages/index.html", `{{define "title"}}Home{{end}}{{define "content"}}Welcome
{{end}}`)
writeFile(t, src, "static/site.css", `body { color: red; }`)
if err := Build(src, out); err != nil {
t.Fatal(err)
}
page, err := os.ReadFile(filepath.Join(out, "index.html"))
if err != nil {
t.Fatal(err)
}
html := string(page)
// Page content and both shared partials are present in the rendered page.
if !strings.Contains(html, "Home") {
t.Errorf("missing page title in output:\n%s", html)
}
if !strings.Contains(html, "Welcome
") {
t.Errorf("missing page content in output:\n%s", html)
}
if !strings.Contains(html, "") {
t.Errorf("missing shared header in output:\n%s", html)
}
if !strings.Contains(html, "