All checks were successful
check / check (pull_request) Successful in 29s
A build never emptied the output folder, so the pages of a list that got shorter stayed behind. The build now returns the files it did not write and the command prints a warning that names them. -clean empties the folder first, only when it is new, empty or holds the .motorino-out file that an earlier -clean left, and never the folder of the site or one that holds it. The default is unchanged: no extra file in a folder built without it, so the deploy (a new folder published with rsync --delete) is not affected. Refs #9
162 lines
5.5 KiB
Go
162 lines
5.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// put writes a file of a test folder.
|
|
func put(t *testing.T, dir, rel, text string) {
|
|
t.Helper()
|
|
path := filepath.Join(dir, filepath.FromSlash(rel))
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(text), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// extraBuilders are the other outputs the tests of the output folder run against, by name: each takes the
|
|
// folder of a site and returns a function that builds it into a folder.
|
|
var extraBuilders = map[string]func(site string) func(out string, clean bool) ([]string, error){}
|
|
|
|
// builders are the outputs, each as a function that builds the site made of files into a folder and returns
|
|
// the files of the folder that the build did not write.
|
|
func builders(t *testing.T, files map[string]string) map[string]func(out string, clean bool) ([]string, error) {
|
|
t.Helper()
|
|
site := writeFiles(t, files)
|
|
all := map[string]func(string, bool) ([]string, error){
|
|
"html": func(out string, clean bool) ([]string, error) {
|
|
r, err := Build(Options{SiteDir: site, OutDir: out, Version: "1.2.3", Now: fixedNow, Clean: clean})
|
|
return r.Stale, err
|
|
},
|
|
}
|
|
for name, newBuilder := range extraBuilders {
|
|
all[name] = newBuilder(site)
|
|
}
|
|
return all
|
|
}
|
|
|
|
// TestStaleFilesAreReported checks that a build into a folder with files it did not write reports them and leaves them.
|
|
func TestStaleFilesAreReported(t *testing.T) {
|
|
for name, run := range builders(t, withFiles(nil)) {
|
|
t.Run(name, func(t *testing.T) {
|
|
out := t.TempDir()
|
|
put(t, out, "news/page/2/index.html", "old")
|
|
put(t, out, "old.txt", "old")
|
|
stale, err := run(out, false)
|
|
if err != nil {
|
|
t.Fatalf("build error = %v", err)
|
|
}
|
|
if strings.Join(stale, ",") != "news/page/2/index.html,old.txt" {
|
|
t.Errorf("stale = %v, want the two old files", stale)
|
|
}
|
|
if !exists(t, out, "old.txt") {
|
|
t.Error("a build without -clean removed a file")
|
|
}
|
|
if exists(t, out, motorinoMarker) {
|
|
t.Error("a build without -clean wrote the marker into the folder")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestFreshFolderHasNothingStale checks that a build into a new folder reports nothing and writes only the site.
|
|
func TestFreshFolderHasNothingStale(t *testing.T) {
|
|
for name, run := range builders(t, withFiles(nil)) {
|
|
t.Run(name, func(t *testing.T) {
|
|
stale, err := run(filepath.Join(t.TempDir(), "new"), false)
|
|
if err != nil || len(stale) != 0 {
|
|
t.Errorf("stale = %v, error = %v", stale, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCleanEmptiesAFolderItMarked checks that -clean removes the old files of a folder that has the marker,
|
|
// the pages of a list that got shorter included, and writes the marker again.
|
|
func TestCleanEmptiesAFolderItMarked(t *testing.T) {
|
|
for name, run := range builders(t, withFiles(nil)) {
|
|
t.Run(name, func(t *testing.T) {
|
|
out := t.TempDir()
|
|
put(t, out, motorinoMarker, "x")
|
|
put(t, out, "news/page/2/index.html", "old")
|
|
put(t, out, "old/deep/file.txt", "old")
|
|
stale, err := run(out, true)
|
|
if err != nil {
|
|
t.Fatalf("build error = %v", err)
|
|
}
|
|
if len(stale) != 0 {
|
|
t.Errorf("stale = %v after a clean build", stale)
|
|
}
|
|
for _, gone := range []string{"news/page/2/index.html", "old/deep/file.txt"} {
|
|
if exists(t, out, gone) {
|
|
t.Errorf("%s is still there after -clean", gone)
|
|
}
|
|
}
|
|
if !exists(t, out, motorinoMarker) {
|
|
t.Error("the marker is gone after -clean")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCleanTakesAnEmptyOrNewFolder checks that -clean needs no marker for a folder with nothing in it, and marks it.
|
|
func TestCleanTakesAnEmptyOrNewFolder(t *testing.T) {
|
|
for name, run := range builders(t, withFiles(nil)) {
|
|
t.Run(name, func(t *testing.T) {
|
|
for _, out := range []string{t.TempDir(), filepath.Join(t.TempDir(), "new", "deeper")} {
|
|
if _, err := run(out, true); err != nil {
|
|
t.Fatalf("build into %s error = %v", out, err)
|
|
}
|
|
if !exists(t, out, motorinoMarker) {
|
|
t.Errorf("no marker in %s", out)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCleanRefusesAFolderThatIsNotMotorinos checks that -clean leaves a folder with files and no marker alone.
|
|
func TestCleanRefusesAFolderThatIsNotMotorinos(t *testing.T) {
|
|
for name, run := range builders(t, withFiles(nil)) {
|
|
t.Run(name, func(t *testing.T) {
|
|
out := t.TempDir()
|
|
put(t, out, "precious.txt", "keep me")
|
|
_, err := run(out, true)
|
|
if err == nil || !strings.Contains(err.Error(), motorinoMarker) {
|
|
t.Fatalf("error = %v, want one that names %s", err, motorinoMarker)
|
|
}
|
|
if got := readOut(t, out, "precious.txt"); got != "keep me" {
|
|
t.Errorf("precious.txt = %q", got)
|
|
}
|
|
if exists(t, out, "index.html") || exists(t, out, "index.gmi") || exists(t, out, "en/index.gmi") {
|
|
t.Error("the build wrote into a folder it refused to empty")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCleanRefusesAFolderThatHoldsTheSite checks that -clean never empties the folder of the site or one above it,
|
|
// even when it has the marker.
|
|
func TestCleanRefusesAFolderThatHoldsTheSite(t *testing.T) {
|
|
parent := t.TempDir()
|
|
site := filepath.Join(parent, "site")
|
|
for rel, text := range withFiles(nil) {
|
|
put(t, site, rel, text)
|
|
}
|
|
put(t, parent, motorinoMarker, "x")
|
|
for _, out := range []string{site, parent} {
|
|
if _, err := Build(Options{SiteDir: site, OutDir: out, Now: fixedNow, Clean: true}); err == nil || !strings.Contains(err.Error(), "site") {
|
|
t.Errorf("Build into %s: error = %v, want one about the site", out, err)
|
|
}
|
|
}
|
|
if !exists(t, site, "site.json") {
|
|
t.Error("the site is gone")
|
|
}
|
|
}
|