From 2c3975430b370c36182d20b70738eb590834dece Mon Sep 17 00:00:00 2001 From: tgiachi Date: Sun, 15 Nov 2026 09:51:52 +0100 Subject: [PATCH] feat: add the build and version commands --- cmd/motorino/main.go | 57 +++++++++++++++-- cmd/motorino/main_test.go | 110 +++++++++++++++++++++++++++++++++ internal/build/build.go | 23 ++++--- internal/build/fixture_test.go | 2 +- 4 files changed, 179 insertions(+), 13 deletions(-) create mode 100644 cmd/motorino/main_test.go diff --git a/cmd/motorino/main.go b/cmd/motorino/main.go index 7b4c8d5..ce40c01 100644 --- a/cmd/motorino/main.go +++ b/cmd/motorino/main.go @@ -2,21 +2,68 @@ // Command motorino builds the kmalloc.org site from Markdown, templates and // language files. +// +// Usage: +// +// motorino build [-site DIR] [-out DIR] +// motorino version package main import ( + "errors" + "flag" "fmt" + "io" "os" + "git.kmalloc.org/squid/motorino/internal/build" "git.kmalloc.org/squid/motorino/internal/version" ) +// usage is the text printed for a missing or unknown command. +const usage = "usage: motorino build [-site DIR] [-out DIR] | motorino version" + // main runs the command line and exits with its status code. func main() { - if len(os.Args) > 1 && os.Args[1] == "version" { - fmt.Println("motorino", version.String()) - return + os.Exit(run(os.Args[1:], os.Stdout, os.Stderr)) +} + +// run executes one command line and returns the exit code: 0 for success, 1 +// for a failed command and 2 for a mistake in the command line. +func run(args []string, stdout, stderr io.Writer) int { + if len(args) == 0 { + fmt.Fprintln(stderr, usage) + return 2 } - fmt.Fprintln(os.Stderr, "usage: motorino version") - os.Exit(2) + switch args[0] { + case "version": + fmt.Fprintln(stdout, "motorino", version.String()) + return 0 + case "build": + return runBuild(args[1:], stdout, stderr) + default: + fmt.Fprintf(stderr, "motorino: unknown command %q\n%s\n", args[0], usage) + return 2 + } +} + +// runBuild parses the flags of the build command and builds the site. +func runBuild(args []string, stdout, stderr io.Writer) int { + flags := flag.NewFlagSet("build", flag.ContinueOnError) + flags.SetOutput(stderr) + site := flags.String("site", ".", "the folder of the site") + out := flags.String("out", "public", "the folder the HTML is written to") + if err := flags.Parse(args); err != nil { + if errors.Is(err, flag.ErrHelp) { + return 0 + } + return 2 + } + result, err := build.Build(build.Options{SiteDir: *site, OutDir: *out, Version: version.String()}) + if err != nil { + fmt.Fprintf(stderr, "motorino: %v\n", err) + return 1 + } + fmt.Fprintf(stdout, "built %d pages into %s\n", result.Pages, *out) + return 0 } diff --git a/cmd/motorino/main_test.go b/cmd/motorino/main_test.go new file mode 100644 index 0000000..8e2ffd6 --- /dev/null +++ b/cmd/motorino/main_test.go @@ -0,0 +1,110 @@ +// 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": "
{{nav_news}}
\n", + "templates/footer.html": "\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": "

{{page_title}}

{{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) + } +} diff --git a/internal/build/build.go b/internal/build/build.go index cdabad4..153fabd 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -65,29 +65,38 @@ type categoryEntry struct { posts []content.Post } +// Result summarises a build. +type Result struct { + // Pages is the number of HTML pages written: pages, news and categories. + Pages int +} + // Build reads the site in opts.SiteDir and writes it to opts.OutDir. It renders // everything in memory first, so a failure leaves the output folder untouched. -func Build(opts Options) error { +func Build(opts Options) (Result, error) { b, err := load(opts) if err != nil { - return err + return Result{}, err } if err := b.renderPages(); err != nil { - return err + return Result{}, err } if err := b.renderPosts(); err != nil { - return err + return Result{}, err } if err := b.renderCategories(); err != nil { - return err + return Result{}, err } b.renderFeed() b.files["robots.txt"] = robots(b.indexable) assets, err := b.readAssets() if err != nil { - return err + return Result{}, err } - return b.write(assets) + if err := b.write(assets); err != nil { + return Result{}, err + } + return Result{Pages: len(b.indexable)}, nil } // load reads the configuration, the content, the templates and the style. diff --git a/internal/build/fixture_test.go b/internal/build/fixture_test.go index cbbd0e1..37a0f0c 100644 --- a/internal/build/fixture_test.go +++ b/internal/build/fixture_test.go @@ -31,7 +31,7 @@ func writeFiles(t *testing.T, files map[string]string) string { func runBuild(t *testing.T, files map[string]string) (string, error) { t.Helper() out := filepath.Join(t.TempDir(), "public") - err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"}) + _, err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"}) return out, err }