code.pablomurad.com

patchwork

Publishes the revisions of your pages as a feed, with notes and readable diffs, written in Go

internal/config/config_test.go raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package config

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

func write(t *testing.T, body string) string {
	t.Helper()
	p := filepath.Join(t.TempDir(), "patchwork.yaml")
	if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
	return p
}

func TestExampleLoads(t *testing.T) {
	c, err := Load(write(t, string(Example)))
	if err != nil {
		t.Fatal(err)
	}
	if c.Publish.FoldWindow.Duration != 20*time.Minute || c.Source.Slug != "path" || c.Feeds.ExcerptBlocks != 6 {
		t.Fatalf("got %+v", c)
	}
	if !filepath.IsAbs(c.Source.Path) || filepath.Base(c.Source.Path) != "content" {
		t.Fatalf("source path not resolved: %s", c.Source.Path)
	}
}

func TestEmptyFileIsDefaults(t *testing.T) {
	c, err := Load(write(t, ""))
	if err != nil {
		t.Fatal(err)
	}
	if c.Frontmatter.NoteKey != "revision" || c.Watch.Poll.Duration != time.Minute {
		t.Fatalf("got %+v", c)
	}
}

func TestEnvOverrides(t *testing.T) {
	t.Setenv("PATCHWORK_DB", "/tmp/override.db")
	t.Setenv("PATCHWORK_BASE_URL", "https://moonflowersociety.org")
	c, err := Load(write(t, "site:\n  base_url: https://example.com\n"))
	if err != nil {
		t.Fatal(err)
	}
	if filepath.ToSlash(c.DB) != "/tmp/override.db" && !strings.HasSuffix(filepath.ToSlash(c.DB), "/tmp/override.db") {
		t.Fatalf("db %s", c.DB)
	}
	if c.Site.BaseURL != "https://moonflowersociety.org" {
		t.Fatalf("base url %s", c.Site.BaseURL)
	}
}

func TestValidation(t *testing.T) {
	cases := map[string]string{
		"unknown key":  "sorce:\n  path: x\n",
		"bad slug":     "source:\n  slug: weird\n",
		"bad timezone": "site:\n  timezone: Mars/Olympus\n",
		"bad duration": "publish:\n  fold_window: soon\n",
		"git mode":     "source:\n  mode: git\n",
		"bad base url": "site:\n  base_url: example.com\n",
	}
	for name, body := range cases {
		if _, err := Load(write(t, body)); err == nil {
			t.Errorf("%s: want error", name)
		}
	}
}

func TestTimezoneWithoutSystemZoneinfo(t *testing.T) {
	c, err := Load(write(t, "site:\n  timezone: America/Sao_Paulo\n"))
	if err != nil {
		t.Fatal(err)
	}
	if c.Location().String() != "America/Sao_Paulo" {
		t.Fatal(c.Location())
	}
}