// 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", "
{{translation_note}} {{translation_note_a}}
lead home
\n", "templates/lead-back.html": "\n", "templates/lead-news.html": "\n", "templates/badges.html": "{{feed}} {{doc_q}}
\n", "templates/feed.html": "RSS\n", "templates/latest-news.html": `{{version}} {{manifesto_version}},
{{note_block}}{{post_byline}}
{{post_body}} `, "templates/category.html": `