All checks were successful
check / check (pull_request) Successful in 30s
motorino new -site DIR "A title" writes news/a-title.md with the header ready: the date of now, the author from site.json, the category News and Draft: true. motorino build -drafts publishes the drafts and the posts dated in the future, for a preview. The README describes the header keys, the news language, the blog placeholders and blocks, and the language strings they use. Refs #9
78 lines
2.5 KiB
Go
78 lines
2.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package content
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// newsNow is the moment the new post tests run at.
|
|
var newsNow = time.Date(2026, 11, 16, 10, 45, 12, 0, time.FixedZone("CET", 3600))
|
|
|
|
// TestNewPostWritesADraftThatLoads checks the file, its name and its header.
|
|
func TestNewPostWritesADraftThatLoads(t *testing.T) {
|
|
dir := t.TempDir()
|
|
rel, err := NewPost(dir, "Il manifesto è cambiato", "squid", newsNow)
|
|
if err != nil {
|
|
t.Fatalf("NewPost() error = %v", err)
|
|
}
|
|
if rel != "news/il-manifesto-e-cambiato.md" {
|
|
t.Errorf("NewPost() = %q, want news/il-manifesto-e-cambiato.md", rel)
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(rel)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []string{"Title: Il manifesto è cambiato\n", "Author: squid\n", "Date: 2026-11-16T10:45:12+0100\n", "Categories: News\n", "Draft: true\n", "\n-\n"} {
|
|
if !strings.Contains(string(data), want) {
|
|
t.Errorf("the file does not contain %q\n%s", want, data)
|
|
}
|
|
}
|
|
posts, err := LoadPosts(dir)
|
|
if err != nil || len(posts) != 1 {
|
|
t.Fatalf("LoadPosts() = %v, %v", posts, err)
|
|
}
|
|
if p := posts[0]; !p.Draft || p.Title != "Il manifesto è cambiato" || p.Author != "squid" || p.Visible(newsNow.Add(time.Hour)) {
|
|
t.Errorf("the new post is %+v, want a hidden draft with the title and the author", p)
|
|
}
|
|
}
|
|
|
|
// TestNewPostWithoutAuthor checks that no author line is written without one.
|
|
func TestNewPostWithoutAuthor(t *testing.T) {
|
|
dir := t.TempDir()
|
|
rel, err := NewPost(dir, "No author", "", newsNow)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, _ := os.ReadFile(filepath.Join(dir, filepath.FromSlash(rel)))
|
|
if strings.Contains(string(data), "Author:") {
|
|
t.Errorf("an Author line without an author:\n%s", data)
|
|
}
|
|
}
|
|
|
|
// TestNewPostRefuses checks the titles and the files it will not write over.
|
|
func TestNewPostRefuses(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if _, err := NewPost(dir, "Twice", "", newsNow); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tests := []struct{ name, title, want string }{
|
|
{"existing file", "Twice", "already exists"},
|
|
{"empty title", " ", "title"},
|
|
{"no letters", "!!!", "no letters"},
|
|
{"line break", "One\nTwo", "title"},
|
|
{"em dash", "One — two", "dash"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
rel, err := NewPost(dir, tt.title, "", newsNow)
|
|
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
|
t.Errorf("NewPost(%q) = %q, %v, want an error with %q", tt.title, rel, err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|