- Draft: true and a date in the future keep a post out of the pages, the lists, the categories and the feed. motorino build -drafts (next commit) shows them. - news_language in site.json, and an optional Language key in a post, choose the language of the news pages and of the interface strings they use. The news still exist once, not once per language. - New header keys Mastodon (an https address), Draft and Language; a post knows its reading time and whether its description was written. Refs #9
129 lines
4.5 KiB
Go
129 lines
4.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fixedNow is the moment the blog tests build at, so that drafts and future
|
|
// dates do not depend on the clock.
|
|
var fixedNow = time.Date(2026, 11, 15, 12, 0, 0, 0, time.FixedZone("CET", 3600))
|
|
|
|
// withFiles returns the base site with files added or replaced; an empty value removes the file.
|
|
func withFiles(extra map[string]string) map[string]string {
|
|
files := baseFiles()
|
|
for name, content := range extra {
|
|
if content == "" {
|
|
delete(files, name)
|
|
continue
|
|
}
|
|
files[name] = content
|
|
}
|
|
return files
|
|
}
|
|
|
|
// buildWith builds the site made of files with the options changed by tune.
|
|
func buildWith(t *testing.T, files map[string]string, tune func(*Options)) (string, error) {
|
|
t.Helper()
|
|
out := filepath.Join(t.TempDir(), "public")
|
|
opts := Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3", Now: fixedNow}
|
|
if tune != nil {
|
|
tune(&opts)
|
|
}
|
|
_, err := Build(opts)
|
|
return out, err
|
|
}
|
|
|
|
// exists reports whether the output has the file rel.
|
|
func exists(t *testing.T, out, rel string) bool {
|
|
t.Helper()
|
|
_, err := readOutMaybe(out, rel)
|
|
return err == nil
|
|
}
|
|
|
|
// postNews returns the news files used by the draft and future tests.
|
|
func postNews() map[string]string {
|
|
return map[string]string{
|
|
"news/a-draft.md": "Title: A draft\nDate: 2026-11-10T12:00:00+0100\nCategories: Secret\nDraft: true\n-\nNot yet.\n",
|
|
"news/in-future.md": "Title: In the future\nDate: 2026-12-24T12:00:00+0100\nCategories: Later\n-\nSoon.\n",
|
|
}
|
|
}
|
|
|
|
// TestDraftsAndFutureStayHidden checks that a draft and a post dated in the
|
|
// future are not published anywhere, and that no category exists only for them.
|
|
func TestDraftsAndFutureStayHidden(t *testing.T) {
|
|
out, err := buildWith(t, withFiles(postNews()), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
for _, rel := range []string{"news/2026/11/a-draft.html", "news/2026/12/in-future.html", "news/category/secret.html", "news/category/later.html"} {
|
|
if exists(t, out, rel) {
|
|
t.Errorf("%s is published, but the post is hidden", rel)
|
|
}
|
|
}
|
|
for _, rel := range []string{"news/index.html", "news/feed.xml", "index.html"} {
|
|
got := readOut(t, out, rel)
|
|
for _, hidden := range []string{"A draft", "In the future", "Secret", "Later"} {
|
|
if strings.Contains(got, hidden) {
|
|
t.Errorf("%s mentions %q, but the post is hidden", rel, hidden)
|
|
}
|
|
}
|
|
}
|
|
mustContain(t, "news index", readOut(t, out, "news/index.html"), "Status page")
|
|
}
|
|
|
|
// TestDraftsFlagShowsHiddenPosts checks the preview build.
|
|
func TestDraftsFlagShowsHiddenPosts(t *testing.T) {
|
|
out, err := buildWith(t, withFiles(postNews()), func(o *Options) { o.Drafts = true })
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
for _, rel := range []string{"news/2026/11/a-draft.html", "news/2026/12/in-future.html", "news/category/secret.html"} {
|
|
if !exists(t, out, rel) {
|
|
t.Errorf("%s is missing from a build with drafts", rel)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestFuturePostAppearsWhenItsDateComes checks the moment of publication.
|
|
func TestFuturePostAppearsWhenItsDateComes(t *testing.T) {
|
|
later := time.Date(2026, 12, 24, 12, 0, 0, 0, time.FixedZone("CET", 3600))
|
|
out, err := buildWith(t, withFiles(postNews()), func(o *Options) { o.Now = later })
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if !exists(t, out, "news/2026/12/in-future.html") {
|
|
t.Error("a post is published from the moment of its date")
|
|
}
|
|
if exists(t, out, "news/2026/11/a-draft.html") {
|
|
t.Error("a draft stays hidden whatever the date")
|
|
}
|
|
}
|
|
|
|
// TestNewsLanguage checks the language of the news and its errors.
|
|
func TestNewsLanguage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
extra map[string]string
|
|
want string
|
|
}{
|
|
{"unknown language in site.json", map[string]string{
|
|
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"news_language": "xx", "languages"`, 1)}, `news_language "xx"`},
|
|
{"no news page in that language", map[string]string{
|
|
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"news_language": "it", "languages"`, 1)}, `content/it`},
|
|
{"post in a language the site does not have", map[string]string{
|
|
"news/tedesco.md": "Title: Deutsch\nDate: 2026-11-01T12:00:00+0100\nLanguage: de\n-\nText.\n"}, `Language "de"`},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := buildWith(t, withFiles(tt.extra), nil)
|
|
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
|
t.Errorf("error = %v, want one that contains %q", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|