// SPDX-License-Identifier: GPL-3.0-or-later package build import ( "os" "path/filepath" "strings" "testing" ) // writeFiles writes the files (path relative to a new directory, then content) // and returns the directory. func writeFiles(t *testing.T, files map[string]string) string { t.Helper() dir := t.TempDir() for name, content := range files { path := filepath.Join(dir, filepath.FromSlash(name)) 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) } } return dir } // runBuild builds the site made of files into a fresh output folder and // returns the folder and the build error. func runBuild(t *testing.T, files map[string]string) (string, error) { t.Helper() out := filepath.Join(t.TempDir(), "public") _, err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"}) return out, err } // readOut returns the content of a file of the output, failing the test when it // is missing. func readOut(t *testing.T, out, rel string) string { t.Helper() data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(rel))) if err != nil { t.Fatalf("output file %s: %v", rel, err) } return string(data) } // readOutMaybe returns the content of a file of the output, or the error when it is missing. func readOutMaybe(out, rel string) (string, error) { data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(rel))) return string(data), err } // mustContain fails the test for each want that is not in got. func mustContain(t *testing.T, name, got string, wants ...string) { t.Helper() for _, want := range wants { if !strings.Contains(got, want) { t.Errorf("%s does not contain %q\n%s", name, want, got) } } } // page returns a page file with the given ID, title, extra header lines and body. func page(id, title, extra, body string) string { return "Title: " + title + "\nID: " + id + "\nDescription: About " + title + ".\n" + extra + "-\n" + body } // baseFiles is a small complete site: English and Italian with every page, a // Spanish manifesto, three news with categories, templates and one asset. func baseFiles() map[string]string { return map[string]string{ "site.json": `{ "base_url": "https://kmalloc.org", "languages": ["en", "it", "es"], "contact": "squid@kmalloc.org", "mastodon_url": "https://social.kmalloc.org/@squid", "motorino_url": "https://git.kmalloc.org/squid/motorino", "manifesto_version": "1.2", "manifesto_date": "2026-11-14", "issues_url": "https://git.kmalloc.org/squid/kmalloc.org/issues", "assets": {"logo.svg": "static/logo.svg"} }`, "lang/en.json": `{"lang_name": "English", "sections": "Sections", "home_link": "/", "nojs": "No JavaScript.", "powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Contents", "history_a": "Manifesto history", "news_p": "What is new.", "news_all": "All news", "news_empty": "No news yet.", "category_h": "Category", "nav": {"manifesto": "Manifesto", "people": "People", "services": "Services", "news": "News"}}`, "lang/it.json": `{"lang_name": "Italiano", "sections": "Sezioni", "home_link": "/it/", "nojs": "Niente JavaScript.", "powered_by": "Powered by", "back": "kmalloc.org", "version": "Versione", "toc": "Indice", "history_a": "Cronologia", "news_p": "Le novita.", "news_all": "Tutte le news", "news_empty": "Nessuna news.", "category_h": "Categoria", "nav": {"manifesto": "Manifesto", "people": "Persone", "services": "Servizi", "news": "News"}}`, "lang/es.json": `{"lang_name": "Espanol", "header": "header-min", "sections": "Secciones", "home_link": "/?lang=en", "nojs": "Sin JavaScript.", "powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Indice", "history_a": "Historial", "translation_note": "Traduccion automatica.", "translation_note_a": "avisar", "news_p": "x", "news_all": "x", "news_empty": "x", "category_h": "x", "nav": {"manifesto": "Manifiesto", "news": "News"}}`, "content/en/index.md": page("home", "kmalloc.org", "Layout: bare\n", "

Technology.

\n\n{{latest_news}}\n"), "content/en/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascist\n\nText one.\n\n## Our commitments\n\nText two.\n"), "content/en/manifesto-history.md": page("history", "Manifesto history", "", "Versions.\n"), "content/en/people.md": page("people", "People", "", "## squid\n\nI run it.\n"), "content/en/projects.md": page("projects", "Projects", "", "## motorino\n\nThis.\n"), "content/en/services.md": page("services", "Services", "Switch: it=/it/servizi.html\n", "Write to **{{contact_shown}}**.\n"), "content/en/news/index.md": page("news", "News", "", "{{news_list}}\n\n{{categories_list}}\n"), "content/it/index.md": page("home", "kmalloc.org", "Layout: bare\n", "{{latest_news}}\n"), "content/it/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascisti\n\nTesto.\n"), "content/it/cronologia-manifesto.md": page("history", "Cronologia", "", "Versioni.\n"), "content/it/persone.md": page("people", "Persone", "", "Io.\n"), "content/it/progetti.md": page("projects", "Progetti", "", "## motorino\n\nQuesto.\n"), "content/it/servizi.md": page("services", "Servizi", "", "Scrivi a {{contact_shown}}.\n"), "content/es/manifesto.md": page("manifesto", "Manifiesto", "Layout: manifesto\nNote: Traduccion de la version 1.0.\nManifestoVersion: 1.0\nManifestoDate: 2026-11-12\n", "## 01 — Antifascistas\n\nTexto.\n"), "news/gnu-linux-day-2026.md": "Title: GNU/Linux Day 2026\nAuthor: Matteo Bini\nDate: 2026-09-18T17:00:45+0200\nCategories: Linux Day, FLUG\n-\nThe programme is out. See [people](/people.html).\n", "news/shop.md": "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: FLUG\nDescription: About the shop.\n-\nThere is a shop.\n", "news/status.md": "Title: Status page\nDate: 2026-11-12T12:00:00+0100\n-\nThe status page.\n", "static/style.css": "body { margin: 0 }\n", "static/logo.svg": "\n", "templates/base.html": ` {{title_html}} {{alt_links}} {{header}} {{content}} {{footer}} `, "templates/header.html": `
home {{nav_manifesto}} {{nav_news}} {{lang_links}}
`, "templates/header-min.html": `
{{nav_manifesto}} {{lang_links}}

{{translation_note}} {{translation_note_a}}

`, "templates/footer.html": ` `, "templates/lead-home.html": "

lead home

\n", "templates/lead-back.html": "

{{back}}

\n", "templates/lead-news.html": "

{{news_all}} {{back}}

\n", "templates/badges.html": "

{{feed}} {{doc_q}}

\n", "templates/feed.html": "RSS\n", "templates/latest-news.html": `

{{nav_news}}

`, "templates/pages/page.html": "

{{page_title}}

\n\n{{body}}\n", "templates/pages/bare.html": "{{body}}\n", "templates/pages/manifesto.html": `

{{page_title}}

{{version}} {{manifesto_version}},

{{note_block}}

{{toc}}

{{body}} `, "templates/post.html": `

{{post_title}}

{{post_byline}}

{{post_body}} `, "templates/category.html": `

{{category_h}}: {{category_name}}

{{category_posts}} `, } }