test(01-02): add failing test for layered compass config
- Cover six-level precedence, env mapping, .env, Set/Persist/Reload - Require HasConfig merge before plugin Register - Assert hello command observes overlay and plugin defaults
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package party
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"git.golem15.com/golem15/summercms/backpack"
|
||||
"git.golem15.com/golem15/summercms/compass"
|
||||
@@ -27,6 +31,51 @@ func (p *tracePlugin) Boot(*backpack.App) error {
|
||||
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}
|
||||
|
||||
Reference in New Issue
Block a user