// SPDX-License-Identifier: GPL-3.0-or-later package build import ( "os" "path/filepath" "strings" "testing" ) // htmlFiles returns the relative paths of every HTML file of the output. func htmlFiles(t *testing.T, out string) []string { t.Helper() var files []string err := filepath.WalkDir(out, func(p string, d os.DirEntry, err error) error { if err == nil && !d.IsDir() && strings.HasSuffix(p, ".html") { rel, _ := filepath.Rel(out, p) files = append(files, filepath.ToSlash(rel)) } return err }) if err != nil { t.Fatal(err) } return files } // TestBuildWritesPagesAndPaths checks that every page lands at its address. func TestBuildWritesPagesAndPaths(t *testing.T) { out, err := runBuild(t, baseFiles()) if err != nil { t.Fatalf("Build() error = %v", err) } for _, rel := range []string{ "index.html", "it/index.html", "manifesto.html", "it/manifesto.html", "es/manifesto.html", "manifesto-history.html", "it/cronologia-manifesto.html", "people.html", "it/persone.html", "services.html", "it/servizi.html", "news/index.html", } { if _, err := os.Stat(filepath.Join(out, rel)); err != nil { t.Errorf("missing output %s: %v", rel, err) } } mustContain(t, "services.html", readOut(t, out, "services.html"), "Services", ``, "squid (at) kmalloc.org", ``) } // TestBuildNewsPathsDated checks the address and the byline of a post. func TestBuildNewsPathsDated(t *testing.T) { out, err := runBuild(t, baseFiles()) if err != nil { t.Fatal(err) } post := readOut(t, out, "news/2026/09/gnu-linux-day-2026.html") mustContain(t, "post", post, "

GNU/Linux Day 2026

", ``, "Matteo Bini", `Linux Day`, `FLUG`, "kmalloc.org: GNU/Linux Day 2026", `people`) readOut(t, out, "news/2026/11/shop.html") readOut(t, out, "news/2026/11/status.html") } // TestBuildCategoryPages checks the page of each category and its posts. func TestBuildCategoryPages(t *testing.T) { out, err := runBuild(t, baseFiles()) if err != nil { t.Fatal(err) } flug := readOut(t, out, "news/category/flug.html") mustContain(t, "flug", flug, "

Category: FLUG

", "GNU/Linux Day 2026", "A shop") if strings.Contains(flug, "Status page") { t.Error("category FLUG lists a post that is not in it") } day := readOut(t, out, "news/category/linux-day.html") mustContain(t, "linux-day", day, "GNU/Linux Day 2026") if strings.Contains(day, "A shop") { t.Error("category Linux Day lists a post that is not in it") } index := readOut(t, out, "news/index.html") mustContain(t, "news index", index, `FLUG (2)`, `Linux Day (1)`) } // TestBuildRobotsAllowsEverything checks robots.txt and that no page asks to be // left out of the index. func TestBuildRobotsAllowsEverything(t *testing.T) { out, err := runBuild(t, baseFiles()) if err != nil { t.Fatal(err) } robots := readOut(t, out, "robots.txt") mustContain(t, "robots.txt", robots, "User-agent: *", "Allow: /$", "Allow: /?lang=en$", "Allow: /it/servizi.html$", "Allow: /news/$", "Allow: /news/2026/11/shop.html$", "Allow: /news/category/flug.html$", "Allow: /es/manifesto.html$") if !strings.HasSuffix(robots, "Disallow: /\n") { t.Errorf("robots.txt does not end with Disallow: /\n%s", robots) } for _, rel := range htmlFiles(t, out) { if strings.Contains(readOut(t, out, rel), `name="robots"`) { t.Errorf("%s has a robots meta", rel) } } } // TestBuildHreflangAndLangLinks checks the links between languages. func TestBuildHreflangAndLangLinks(t *testing.T) { out, err := runBuild(t, baseFiles()) if err != nil { t.Fatal(err) } en := readOut(t, out, "services.html") mustContain(t, "en services", en, ``, ``, `Italiano`) it := readOut(t, out, "it/servizi.html") mustContain(t, "it services", it, `English`) // A page with no translation points to the home of another language. post := readOut(t, out, "news/2026/11/shop.html") mustContain(t, "post", post, `Italiano`) if strings.Contains(post, `rel="alternate" hreflang`) { t.Error("a page without translation has alternate links") } // The Spanish manifesto has the short header and links to its siblings. es := readOut(t, out, "es/manifesto.html") mustContain(t, "es manifesto", es, "Traduccion automatica.", "Traduccion de la version 1.0.", `English`) } // TestBuildMenuMarksTheCurrentPage checks aria-current on the menu. func TestBuildMenuMarksTheCurrentPage(t *testing.T) { out, err := runBuild(t, baseFiles()) if err != nil { t.Fatal(err) } mustContain(t, "manifesto", readOut(t, out, "manifesto.html"), `Manifesto`) mustContain(t, "post", readOut(t, out, "news/2026/11/shop.html"), `News`) if strings.Contains(readOut(t, out, "people.html"), `aria-current="page">News`) { t.Error("the News entry is marked on the People page") } } // TestBuildBlockPlaceholders checks the generated lists inside pages. func TestBuildBlockPlaceholders(t *testing.T) { out, err := runBuild(t, baseFiles()) if err != nil { t.Fatal(err) } mustContain(t, "news index", readOut(t, out, "news/index.html"), `
  • A shop: About the shop.
  • `) home := readOut(t, out, "index.html") mustContain(t, "home", home, `

    News

    `, `A shop`) it := readOut(t, out, "it/index.html") mustContain(t, "it home", it, `A shop`) if strings.Contains(home, "{{") || strings.Contains(home, "