motorino/cmd/motorino/main_test.go
tgiachi faff6476ab
All checks were successful
check / check (pull_request) Successful in 30s
Merge branch 'feat/blog-news' into feat/gemini-output
2026-11-16 12:50:17 +01:00

313 lines
13 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", "projects", "services", "news", "history"} {
name := id
switch id {
case "home":
name = "index"
case "news":
name = "news/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 7 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())
}
}
}
// TestRunBuildRejectsExtraArguments checks that arguments that are not flags are not ignored.
func TestRunBuildRejectsExtraArguments(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"build", "somesite", "someout"}, &out, &errOut); code != 2 {
t.Errorf("run(build somesite someout) = %d, want 2", code)
}
if !strings.Contains(errOut.String(), "unexpected") {
t.Errorf("stderr = %q, want it to say what was unexpected", 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)
}
}
// TestRunNewCreatesADraft checks the new command: it prints the file, writes a draft and
// takes the author from site.json.
func TestRunNewCreatesADraft(t *testing.T) {
dir := miniSite(t)
site := filepath.Join(dir, "site.json")
if err := os.WriteFile(site, []byte(`{"base_url": "https://x.org", "languages": ["en"], "mastodon_url": "m", "motorino_url": "u", "author": "squid"}`), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
if code := run([]string{"new", "-site", dir, "My first post"}, &out, &errOut); code != 0 {
t.Fatalf("run(new) = %d, stderr %q", code, errOut.String())
}
if got := strings.TrimSpace(out.String()); got != "news/my-first-post.md" {
t.Errorf("stdout = %q, want news/my-first-post.md", got)
}
data, err := os.ReadFile(filepath.Join(dir, "news", "my-first-post.md"))
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"Title: My first post\n", "Author: squid\n", "Draft: true\n"} {
if !strings.Contains(string(data), want) {
t.Errorf("the file does not contain %q\n%s", want, data)
}
}
if code := run([]string{"new", "-site", dir, "My first post"}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), "already exists") {
t.Errorf("the same title again: code %d, stderr %q, want 1 and an error", code, errOut.String())
}
}
// TestRunNewNeedsATitle checks the usage mistakes of the new command.
func TestRunNewNeedsATitle(t *testing.T) {
for _, args := range [][]string{{"new"}, {"new", "-site", t.TempDir()}, {"new", "one", "two"}} {
var out, errOut bytes.Buffer
if code := run(args, &out, &errOut); code != 2 || !strings.Contains(errOut.String(), "usage") {
t.Errorf("run(%v) = %d, stderr %q, want 2 and the usage", args, code, errOut.String())
}
}
}
// TestRunBuildDraftsFlag checks that a draft is only published with -drafts.
func TestRunBuildDraftsFlag(t *testing.T) {
dir := miniSite(t)
if err := os.MkdirAll(filepath.Join(dir, "news"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "news", "wip.md"), []byte("Title: Work in progress\nDate: 2026-01-01T12:00:00+0100\nDraft: true\n-\nSoon.\n"), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
plain := filepath.Join(t.TempDir(), "plain")
if code := run([]string{"build", "-site", dir, "-out", plain}, &out, &errOut); code != 0 {
t.Fatalf("build = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(plain, "news", "2026", "01", "wip.html")); err == nil {
t.Error("a draft is published without -drafts")
}
preview := filepath.Join(t.TempDir(), "preview")
if code := run([]string{"build", "-drafts", "-site", dir, "-out", preview}, &out, &errOut); code != 0 {
t.Fatalf("build -drafts = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(preview, "news", "2026", "01", "wip.html")); err != nil {
t.Errorf("a draft is missing from the build with -drafts: %v", err)
}
}
// TestRunGemini checks the gemini command: the capsule, the language flag and the drafts flag.
func TestRunGemini(t *testing.T) {
dir := miniSite(t)
if err := os.MkdirAll(filepath.Join(dir, "news"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "news", "wip.md"), []byte("Title: Work in progress\nDate: 2026-01-01T12:00:00+0100\nDraft: true\n-\nSoon.\n"), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
capsule := filepath.Join(t.TempDir(), "capsule")
if code := run([]string{"gemini", "-site", dir, "-out", capsule, "-lang", "en"}, &out, &errOut); code != 0 {
t.Fatalf("run(gemini) = %d, stderr %q", code, errOut.String())
}
if want := "gemtext files into " + capsule; !strings.Contains(out.String(), want) {
t.Errorf("output %q does not contain %q", out.String(), want)
}
for _, rel := range []string{"index.gmi", "manifesto.gmi", "people.gmi", "services.gmi", "news/index.gmi", "news/feed.gmi", ".meta"} {
if _, err := os.Stat(filepath.Join(capsule, filepath.FromSlash(rel))); err != nil {
t.Errorf("no %s in the capsule: %v", rel, err)
}
}
if _, err := os.Stat(filepath.Join(capsule, "news", "2026", "01", "wip.gmi")); err == nil {
t.Error("a draft is published without -drafts")
}
preview := filepath.Join(t.TempDir(), "preview")
if code := run([]string{"gemini", "-drafts", "-site", dir, "-out", preview}, &out, &errOut); code != 0 {
t.Fatalf("run(gemini -drafts) = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(preview, "news", "2026", "01", "wip.gmi")); err != nil {
t.Errorf("a draft is missing from the capsule with -drafts: %v", err)
}
}
// TestRunGeminiErrors checks the exit codes: 1 for a failed build, 2 for a mistake in the command line.
func TestRunGeminiErrors(t *testing.T) {
var out, errOut bytes.Buffer
if code := run([]string{"gemini", "-site", miniSite(t), "-out", filepath.Join(t.TempDir(), "c"), "-lang", "xx"}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), `"xx"`) {
t.Errorf("an unknown language: code %d, stderr %q, want 1 and a message that names it", code, errOut.String())
}
errOut.Reset()
if code := run([]string{"gemini", "extra"}, &out, &errOut); code != 2 || !strings.Contains(errOut.String(), "unexpected arguments") {
t.Errorf("an extra argument: code %d, stderr %q, want 2", code, errOut.String())
}
}
// TestRunBuildWarnsAboutOldFiles checks that old files in the output folder are named in a warning and left alone.
func TestRunBuildWarnsAboutOldFiles(t *testing.T) {
site := miniSite(t)
outDir := t.TempDir()
if err := os.WriteFile(filepath.Join(outDir, "old.html"), []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
var out, errOut bytes.Buffer
if code := run([]string{"build", "-site", site, "-out", outDir}, &out, &errOut); code != 0 {
t.Fatalf("run(build) = %d, stderr %q", code, errOut.String())
}
for _, want := range []string{"warning", "1 file", "old.html", "-clean"} {
if !strings.Contains(errOut.String(), want) {
t.Errorf("stderr %q does not contain %q", errOut.String(), want)
}
}
if _, err := os.Stat(filepath.Join(outDir, "old.html")); err != nil {
t.Error("a build without -clean removed the old file")
}
errOut.Reset()
if code := run([]string{"build", "-site", site, "-out", filepath.Join(t.TempDir(), "new")}, &out, &errOut); code != 0 || errOut.Len() != 0 {
t.Errorf("a build into a new folder: code %d, stderr %q, want 0 and nothing", code, errOut.String())
}
}
// TestRunBuildClean checks -clean on a folder that is marked and on one that is not.
func TestRunBuildClean(t *testing.T) {
site := miniSite(t)
var out, errOut bytes.Buffer
outDir := filepath.Join(t.TempDir(), "public")
if code := run([]string{"build", "-clean", "-site", site, "-out", outDir}, &out, &errOut); code != 0 {
t.Fatalf("run(build -clean) into a new folder = %d, stderr %q", code, errOut.String())
}
if err := os.WriteFile(filepath.Join(outDir, "old.html"), []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if code := run([]string{"build", "-clean", "-site", site, "-out", outDir}, &out, &errOut); code != 0 || errOut.Len() != 0 {
t.Fatalf("run(build -clean) again = %d, stderr %q", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(outDir, "old.html")); err == nil {
t.Error("-clean left the old file")
}
other := t.TempDir()
if err := os.WriteFile(filepath.Join(other, "mine.txt"), []byte("mine"), 0o644); err != nil {
t.Fatal(err)
}
if code := run([]string{"build", "-clean", "-site", site, "-out", other}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), ".motorino-out") {
t.Errorf("run(build -clean) into a folder that is not motorino's = %d, stderr %q, want 1 and the marker named", code, errOut.String())
}
if _, err := os.Stat(filepath.Join(other, "mine.txt")); err != nil {
t.Error("-clean removed a file of a folder that is not motorino's")
}
}
// TestRunGeminiOldFilesAndClean checks the warning about old files and -clean for the gemini command.
func TestRunGeminiOldFilesAndClean(t *testing.T) {
site := miniSite(t)
var out, errOut bytes.Buffer
capsule := t.TempDir()
if err := os.WriteFile(filepath.Join(capsule, "old.gmi"), []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if code := run([]string{"gemini", "-site", site, "-out", capsule}, &out, &errOut); code != 0 || !strings.Contains(errOut.String(), "old.gmi") {
t.Fatalf("run(gemini) = %d, stderr %q, want 0 and a warning that names old.gmi", code, errOut.String())
}
errOut.Reset()
if code := run([]string{"gemini", "-clean", "-site", site, "-out", capsule}, &out, &errOut); code != 1 || !strings.Contains(errOut.String(), ".motorino-out") {
t.Fatalf("run(gemini -clean) into a folder that is not motorino's = %d, stderr %q", code, errOut.String())
}
fresh := filepath.Join(t.TempDir(), "capsule")
for i := 0; i < 2; i++ {
errOut.Reset()
if code := run([]string{"gemini", "-clean", "-site", site, "-out", fresh}, &out, &errOut); code != 0 || errOut.Len() != 0 {
t.Fatalf("run(gemini -clean) %d = %d, stderr %q", i+1, code, errOut.String())
}
}
}