86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package party_test
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
"git.golem15.com/golem15/summercms/modules/party"
|
|
)
|
|
|
|
func ExamplePlugin() {
|
|
var p party.Plugin = &BlogPlugin{}
|
|
fmt.Println(p.ID())
|
|
// Output: acme.blog
|
|
}
|
|
|
|
// registerBlog is the blog plugin package's init function: a real package
|
|
// names it init, so importing the package registers the plugin.
|
|
func registerBlog() {
|
|
party.Register(&BlogPlugin{})
|
|
}
|
|
|
|
// userPlugin is the acme.user plugin BlogPlugin requires.
|
|
type userPlugin struct{}
|
|
|
|
func (*userPlugin) ID() string { return "acme.user" }
|
|
func (*userPlugin) Requires() []string { return nil }
|
|
func (*userPlugin) Register(*backpack.App) error { return nil }
|
|
func (*userPlugin) Boot(*backpack.App) error { return nil }
|
|
|
|
// startApp is the part of an application's generated main that loads the
|
|
// configuration in dir and activates the plugins.
|
|
func startApp(dir string) (*backpack.App, []party.Plugin, error) {
|
|
// docs:start activate
|
|
cfg, err := compass.Load(dir)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
app := backpack.New(cfg)
|
|
info := []party.Info{
|
|
{ID: "acme.user", Source: party.SourceLocal},
|
|
{ID: "acme.blog", Version: "v0.2.5", Source: party.SourceModule},
|
|
}
|
|
if err := party.Describe(app, info, "v0.6.0"); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
plugins, err := party.Activate(app, []string{"acme.user", "acme.blog"})
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
// docs:end activate
|
|
return app, plugins, nil
|
|
}
|
|
|
|
// TestDocsActivate registers both plugins, starts an application on an
|
|
// empty config directory and checks the activation order, the published
|
|
// metadata and the plugin methods the README shows.
|
|
func TestDocsActivate(t *testing.T) {
|
|
party.DocsRegistry(t)
|
|
registerBlog()
|
|
party.Register(&userPlugin{})
|
|
|
|
app, plugins, err := startApp(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var ids []string
|
|
for _, p := range plugins {
|
|
ids = append(ids, p.ID())
|
|
}
|
|
if want := []string{"acme.user", "acme.blog"}; !slices.Equal(ids, want) {
|
|
t.Fatalf("activated %v, want %v", ids, want)
|
|
}
|
|
installed, ok := app.Lookup[party.Installed]()
|
|
if !ok || installed.Framework != "v0.6.0" {
|
|
t.Fatalf("Installed = %+v, %v", installed, ok)
|
|
}
|
|
|
|
p := plugins[1]
|
|
if !slices.Equal(p.Requires(), []string{"acme.user"}) || p.Register(app) != nil || p.Boot(app) != nil {
|
|
t.Fatalf("BlogPlugin methods: %v", p.Requires())
|
|
}
|
|
}
|