Sites → Sections and pages reads the website's Hugo repository: each section type with the README's description, the fields in use and the layout's, the pages using it and a real example; the pages and their sections; the latest commits. Working copies are now held by one job at a time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
4.6 KiB
Go
111 lines
4.6 KiB
Go
package sites
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Files is a small site built from sections, for tests.
|
|
var Files = map[string]string{
|
|
"README.md": "# Site\n\n| Type | Fields |\n|---|---|\n| `hero` | `title`, `subhead`, `ctas` |\n\n- `faq` — questions: `items` of `q` + `a`\n",
|
|
"layouts/partials/render-sections.html": `{{ range .Params.sections }}{{ if eq .type "hero" }}{{ partial "sections/hero.html" . }}{{ else }}{{ partial "sections/wrapper.html" . }}{{ end }}{{ end }}`,
|
|
"layouts/partials/sections/wrapper.html": `<section>{{ partial (printf "sections/%s.html" .type) . }}</section>`,
|
|
"layouts/partials/sections/head.html": `{{/* the heading: .ignored */}}{{ with .kicker }}<p>{{ . }}</p>{{ end }}{{ with .headline }}<h2>{{ . }}</h2>{{ end }}`,
|
|
"layouts/partials/sections/ctas.html": `{{ range . }}<a href="{{ .url }}">{{ .label }}</a>{{ end }}`,
|
|
"layouts/partials/sections/hero.html": `<h1>{{ .title }}</h1><p>{{ .subhead }}</p>{{ partial "sections/ctas.html" .ctas }}`,
|
|
"layouts/partials/sections/faq.html": "{{ $s := . }}{{ partial \"sections/head.html\" $s }}{{ range $s.items }}<h3>{{ .q }}</h3>{{ .a }}{{ end }}",
|
|
"layouts/partials/sections/stats.html": "{{ $s := . }}{{ range $s.items }}{{ .value }}{{ end }}",
|
|
"content/_index.md": "---\ntitle: Home\nsections:\n - type: hero\n title: Hello\n subhead: There\n ctas:\n - label: Start\n url: /start/\n - type: faq\n headline: Questions\n items:\n - q: Why?\n a: Because.\n---\n",
|
|
"content/pricing.md": "---\ntitle: Pricing\nsections:\n - type: hero\n title: Prices\n - type: widget\n size: 3\n---\n",
|
|
"content/blog/post.md": "+++\ntitle = \"A post\"\n+++\nHello.\n",
|
|
"content/notes.md": "---\ntitle: Notes\ndraft: true\n---\nJust text.\n",
|
|
}
|
|
|
|
func write(t *testing.T, files map[string]string) string {
|
|
dir := t.TempDir()
|
|
for name, body := range files {
|
|
p := filepath.Join(dir, name)
|
|
os.MkdirAll(filepath.Dir(p), 0o755)
|
|
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func path(rel string) string {
|
|
rel = strings.TrimSuffix(filepath.ToSlash(rel), ".md")
|
|
rel = strings.Trim(strings.TrimSuffix(rel, "_index"), "/")
|
|
if rel == "" {
|
|
return "/"
|
|
}
|
|
return "/" + rel + "/"
|
|
}
|
|
|
|
func TestRead(t *testing.T) {
|
|
c, err := Read(write(t, Files), path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var names []string
|
|
for _, ty := range c.Types {
|
|
names = append(names, ty.Name)
|
|
}
|
|
// Most used first; stats has a layout but no page uses it yet; the
|
|
// widget has no layout.
|
|
if want := []string{"hero", "faq", "widget", "stats"}; !slices.Equal(names, want) {
|
|
t.Errorf("types: %v, want %v", names, want)
|
|
}
|
|
if want := []string{"ctas", "head", "wrapper"}; !slices.Equal(c.Helpers, want) {
|
|
t.Errorf("helpers: %v", c.Helpers)
|
|
}
|
|
hero := c.Type("hero")
|
|
if hero.Uses != 2 || !slices.Equal(hero.Pages, []string{"/", "/pricing/"}) || hero.Doc != "`title`, `subhead`, `ctas`" {
|
|
t.Errorf("hero: %+v", hero)
|
|
}
|
|
if hero.Fields[0].Name != "title" || hero.Fields[0].Count != 2 {
|
|
t.Errorf("hero fields: %+v", hero.Fields)
|
|
}
|
|
for _, f := range hero.Fields {
|
|
if f.Name == "ctas" && !slices.Equal(f.Items, []string{"label", "url"}) {
|
|
t.Errorf("ctas items: %v", f.Items)
|
|
}
|
|
}
|
|
// ctas is handed .ctas, not the section, so its fields aren't the hero's.
|
|
if !slices.Equal(hero.Layout, []string{"ctas", "subhead", "title"}) {
|
|
t.Errorf("hero layout: %v", hero.Layout)
|
|
}
|
|
faq := c.Type("faq")
|
|
if faq.Doc != "questions: `items` of `q` + `a`" || !slices.Equal(faq.Layout, []string{"headline", "items", "kicker"}) {
|
|
t.Errorf("faq: %+v", faq)
|
|
}
|
|
if !strings.HasPrefix(faq.Example, "- type: faq\n headline: Questions") || !strings.Contains(faq.Example, "- q: Why?") {
|
|
t.Errorf("faq example: %s", faq.Example)
|
|
}
|
|
if w := c.Type("widget"); !w.Missing || c.Has("widget") || !c.Has("stats") || c.Type("stats").Uses != 0 {
|
|
t.Errorf("widget: %+v", w)
|
|
}
|
|
if len(c.Pages) != 4 || c.Pages[1].Path != "/blog/post/" || c.Pages[1].Title != "A post" {
|
|
t.Fatalf("pages: %+v", c.Pages)
|
|
}
|
|
home := c.Pages[0]
|
|
if home.Path != "/" || home.Title != "Home" || !home.Sections || !slices.Equal(home.Types, []string{"hero", "faq"}) || home.File != "content/_index.md" {
|
|
t.Errorf("home: %+v", home)
|
|
}
|
|
for _, p := range c.Pages {
|
|
if p.Path == "/notes/" && (!p.Draft || p.Sections) {
|
|
t.Errorf("notes: %+v", p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReadEmpty(t *testing.T) {
|
|
c, err := Read(t.TempDir(), path)
|
|
if err != nil || len(c.Types) != 0 || len(c.Pages) != 0 {
|
|
t.Errorf("%+v %v", c, err)
|
|
}
|
|
}
|