- Cover six-level precedence, env mapping, .env, Set/Persist/Reload - Require HasConfig merge before plugin Register - Assert hello command observes overlay and plugin defaults
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
package party
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"git.golem15.com/golem15/summercms/backpack"
|
|
"git.golem15.com/golem15/summercms/compass"
|
|
)
|
|
|
|
type tracePlugin struct {
|
|
id string
|
|
req []string
|
|
log *[]string
|
|
}
|
|
|
|
func (p *tracePlugin) ID() string { return p.id }
|
|
func (p *tracePlugin) Requires() []string { return p.req }
|
|
|
|
func (p *tracePlugin) Register(*backpack.App) error {
|
|
*p.log = append(*p.log, p.id+":register")
|
|
return nil
|
|
}
|
|
|
|
func (p *tracePlugin) Boot(*backpack.App) error {
|
|
*p.log = append(*p.log, p.id+":boot")
|
|
return nil
|
|
}
|
|
|
|
type configPlugin struct {
|
|
id string
|
|
fsys fs.FS
|
|
got *int
|
|
}
|
|
|
|
func (p *configPlugin) ID() string { return p.id }
|
|
func (p *configPlugin) Requires() []string { return nil }
|
|
func (p *configPlugin) ConfigFS() fs.FS { return p.fsys }
|
|
|
|
func (p *configPlugin) Register(app *backpack.App) error {
|
|
if app != nil && app.Config != nil {
|
|
*p.got = app.Config.Int("golem15.hello.posts_per_page")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *configPlugin) Boot(*backpack.App) error { return nil }
|
|
|
|
func TestActivateMergesHasConfigBeforeRegister(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "app.yaml"), []byte("name: app\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := compass.Load(dir)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
var got int
|
|
p := &configPlugin{
|
|
id: "golem15.hello",
|
|
got: &got,
|
|
fsys: fstest.MapFS{
|
|
"config/config.yaml": {Data: []byte("posts_per_page: 10\n")},
|
|
},
|
|
}
|
|
app := backpack.New(cfg)
|
|
if _, err := activate([]Plugin{p}, app, []string{"golem15.hello"}); err != nil {
|
|
t.Fatalf("activate: %v", err)
|
|
}
|
|
if got != 10 {
|
|
t.Fatalf("posts_per_page during Register = %d, want 10 (HasConfig must merge before Register)", got)
|
|
}
|
|
}
|
|
|
|
func TestActivateRunsAllRegisterBeforeAnyBoot(t *testing.T) {
|
|
var log []string
|
|
hello := &tracePlugin{id: "golem15.hello", log: &log}
|
|
greeter := &tracePlugin{id: "golem15.greeter", req: []string{"golem15.hello"}, log: &log}
|
|
app := backpack.New(&compass.Config{})
|
|
|
|
got, err := activate([]Plugin{greeter, hello}, app, []string{"golem15.greeter", "golem15.hello"})
|
|
if err != nil {
|
|
t.Fatalf("activate: %v", err)
|
|
}
|
|
if len(got) != 2 {
|
|
t.Fatalf("got %d plugins, want 2", len(got))
|
|
}
|
|
if got[0].ID() != "golem15.hello" || got[1].ID() != "golem15.greeter" {
|
|
t.Fatalf("order = %s, %s", got[0].ID(), got[1].ID())
|
|
}
|
|
|
|
want := []string{
|
|
"golem15.hello:register",
|
|
"golem15.greeter:register",
|
|
"golem15.hello:boot",
|
|
"golem15.greeter:boot",
|
|
}
|
|
if strings.Join(log, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("lifecycle = %v, want %v", log, want)
|
|
}
|
|
}
|