package sites import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "supermarket/internal/hugorepo" ) func TestNewSiteFiles(t *testing.T) { n := Defaults() n.Repo, n.URL, n.Title, n.Tagline, n.Email = " https://gitea.example/Us/acme.git ", "https://www.acme.example/", `Acme "Widgets" & Co`, "Wïdgets, for everyone", "hi@acme.example" if err := n.Check(); err != nil { t.Fatal(err) } if n.Repo != "Us/acme" || n.URL != "https://www.acme.example" { t.Errorf("tidied: %+v", n) } f := n.Files() if !strings.Contains(f["hugo.toml"], `title = "Acme \"Widgets\" & Co"`) { t.Errorf("hugo.toml:\n%s", f["hugo.toml"]) } // Hugo reads the settings and builds the site, the title as given. if _, err := exec.LookPath("hugo"); err == nil { dir := t.TempDir() for rel, body := range f { os.MkdirAll(filepath.Join(dir, filepath.Dir(rel)), 0o755) os.WriteFile(filepath.Join(dir, rel), []byte(body), 0o644) } out, err := hugorepo.BuildDir(context.Background(), dir) if err != nil { t.Fatal(err) } defer os.RemoveAll(out) home, _ := os.ReadFile(filepath.Join(out, "index.html")) if !strings.Contains(string(home), "Acme "Widgets" & Co — Wïdgets, for everyone") { t.Errorf("built home: %.600s", home) } } if !strings.Contains(f["content/_index.md"], `title: "Acme \"Widgets\" & Co"`) || f["themes/supermarket/VERSION"] != "1\n" { t.Errorf("home: %s", f["content/_index.md"]) } for _, bad := range []NewSite{{Title: "x\ny"}, {Dark: "red"}, {Font: "Comic"}, {Logo: "javascript:alert(1)"}, {URL: "https://x.example/path"}} { m := Defaults() m.Repo, m.URL, m.Title = "Us/acme", "https://acme.example", "Acme" if bad.Title != "" { m.Title = bad.Title } if bad.Dark != "" { m.Dark = bad.Dark } if bad.Font != "" { m.Font = bad.Font } if bad.Logo != "" { m.Logo = bad.Logo } if bad.URL != "" { m.URL = bad.URL } if err := m.Check(); err == nil { t.Errorf("accepted %+v", bad) } } }