// Package hugotest makes a small Hugo site in a Git repository, for tests.
package hugotest
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
// Site makes a small Hugo site in a bare repository and returns its
// clone URL. Its titles are "Page title · Site". Extra files are added to
// (or replace) the site's own.
func Site(t testing.TB, extra ...map[string]string) string {
t.Helper()
dir := t.TempDir()
src := filepath.Join(dir, "src")
files := map[string]string{
"hugo.toml": "baseURL = 'https://site.example/'\ntitle = 'Site'\ndisableKinds = ['taxonomy', 'term', 'RSS', 'sitemap']\n",
"layouts/_default/baseof.html": `
{{ if .IsHome }}{{ site.Title }}{{ else }}{{ .Title }} · {{ site.Title }}{{ end }}{{ block "main" . }}{{ end }}`,
"layouts/_default/single.html": `{{ define "main" }}{{ .Title }}
{{ .Content }}{{ end }}`,
"layouts/_default/list.html": `{{ define "main" }}{{ .Title }}
{{ .Content }}{{ end }}`,
"content/_index.md": "---\ntitle: Home\n---\nWelcome.\n",
"content/pricing.md": "---\ntitle: Pricing\ndescription: |\n Old description\n on two lines\ndraft: false\n---\nOur prices.\n",
"content/blog/first-post/index.md": "+++\ntitle = \"First post\"\n+++\nHello.\n",
"content/about.md": "---\ntitle: About us\nurl: /company/\n---\nWho we are.\n",
}
for _, e := range extra {
for name, body := range e {
files[name] = body
}
}
for name, body := range files {
p := filepath.Join(src, name)
os.MkdirAll(filepath.Dir(p), 0o755)
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
bare := filepath.Join(dir, "site.git")
for _, args := range [][]string{
{"init", "-q", "-b", "main", src},
{"-C", src, "add", "."},
{"-C", src, "-c", "user.name=T", "-c", "user.email=t@example.com", "commit", "-qm", "init"},
{"clone", "-q", "--bare", src, bare},
} {
if out, err := exec.Command("git", args...).CombinedOutput(); err != nil {
t.Fatalf("git %v: %s", args, out)
}
}
return "file://" + bare
}