motorino/cmd/motorino/main_test.go
2026-11-15 09:51:52 +01:00

110 lines
4.1 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
// miniSite writes the smallest site that builds: every required page in one
// language and the templates the frame needs. It returns the folder.
func miniSite(t *testing.T) string {
t.Helper()
files := map[string]string{
"site.json": `{"base_url": "https://x.org", "languages": ["en"], "mastodon_url": "m", "motorino_url": "u"}`,
"lang/en.json": `{"lang_name": "English", "home_link": "/", "back": "b", "news_all": "a", "news_p": "p",
"news_empty": "e", "nojs": "n", "history_a": "h", "version": "v", "powered_by": "P", "category_h": "c",
"nav": {"news": "News", "manifesto": "M"}}`,
"static/style.css": "body{}\n",
"templates/base.html": "{{header}}{{content}}{{footer}}\n",
"templates/header.html": "<header>{{nav_news}}</header>\n",
"templates/footer.html": "<footer>{{lead}}{{badges}}</footer>\n",
"templates/lead-home.html": "h\n", "templates/lead-back.html": "b\n", "templates/lead-news.html": "n\n",
"templates/badges.html": "{{feed}}\n", "templates/feed.html": "f\n",
"templates/pages/page.html": "<h1>{{page_title}}</h1>{{body}}\n",
"templates/post.html": "{{post_body}}\n", "templates/category.html": "{{category_posts}}\n",
}
for _, id := range []string{"home", "manifesto", "people", "services", "news", "history"} {
name := id
if id == "home" {
name = "index"
}
files["content/en/"+name+".md"] = "Title: " + id + "\nID: " + id + "\nDescription: d\n-\nBody of " + id + ".\n"
}
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
}
// TestRunVersion checks that the version command prints the name and the version.
func TestRunVersion(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"version"}, &out, &errOut); code != 0 {
t.Fatalf("run(version) = %d, stderr %q", code, errOut.String())
}
if got := strings.TrimSpace(out.String()); got != "motorino dev" {
t.Errorf("output = %q, want %q", got, "motorino dev")
}
}
// TestRunBuildSuccess checks a build: exit 0, a summary line and the pages.
func TestRunBuildSuccess(t *testing.T) {
site := miniSite(t)
outDir := filepath.Join(t.TempDir(), "public")
var out, errOut bytes.Buffer
code := run([]string{"build", "-site", site, "-out", outDir}, &out, &errOut)
if code != 0 {
t.Fatalf("run(build) = %d, stderr %q", code, errOut.String())
}
if want := "built 6 pages into " + outDir; !strings.Contains(out.String(), want) {
t.Errorf("output %q does not contain %q", out.String(), want)
}
if _, err := os.Stat(filepath.Join(outDir, "index.html")); err != nil {
t.Errorf("no index.html in the output: %v", err)
}
}
// TestRunBuildErrorExit1 checks that a failed build exits 1 and says why.
func TestRunBuildErrorExit1(t *testing.T) {
var out, errOut bytes.Buffer
code := run([]string{"build", "-site", t.TempDir(), "-out", filepath.Join(t.TempDir(), "public")}, &out, &errOut)
if code != 1 {
t.Fatalf("run(build) = %d, want 1", code)
}
if !strings.HasPrefix(errOut.String(), "motorino: ") || !strings.Contains(errOut.String(), "site.json") {
t.Errorf("stderr = %q, want a line that starts with %q and names site.json", errOut.String(), "motorino: ")
}
}
// TestRunUnknownCommand checks the usage message and exit 2.
func TestRunUnknownCommand(t *testing.T) {
for _, args := range [][]string{{}, {"frobnicate"}} {
var out, errOut bytes.Buffer
if code := run(args, &out, &errOut); code != 2 {
t.Errorf("run(%v) = %d, want 2", args, code)
}
if !strings.Contains(errOut.String(), "usage:") {
t.Errorf("run(%v) stderr = %q, want a usage line", args, errOut.String())
}
}
}
// TestRunBuildBadFlag checks that an unknown flag is a usage error.
func TestRunBuildBadFlag(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"build", "-nope"}, &out, &errOut); code != 2 {
t.Errorf("run(build -nope) = %d, want 2", code)
}
}