- new module kiosk: Manifest, Marshal, Parse, Derive, Generate, Write, Check, StaleError - plugin:manifest [dir] with --check, --pretend, --name, --description - docs: kiosk README, root table row, plugins/marketplace.md, scaffolding row
127 lines
3.6 KiB
Go
127 lines
3.6 KiB
Go
package kiosk
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// copyTestdata copies testdata/<names...> into a fresh temp dir, keeping
|
|
// the relative layout, and returns the temp dir.
|
|
func copyTestdata(t *testing.T, names ...string) string {
|
|
t.Helper()
|
|
dst := t.TempDir()
|
|
for _, name := range names {
|
|
src := filepath.Join("testdata", name)
|
|
err := filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rel, err := filepath.Rel("testdata", path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
target := filepath.Join(dst, rel)
|
|
if d.IsDir() {
|
|
return os.MkdirAll(target, 0o755)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(target, data, 0o644)
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("copy %s: %v", src, err)
|
|
}
|
|
}
|
|
return dst
|
|
}
|
|
|
|
// TestManifestSmoke drives plugin.yaml generation end to end on the
|
|
// fixture plugins in testdata.
|
|
func TestManifestSmoke(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
t.Run("literal", func(t *testing.T) {
|
|
dir := filepath.Join(copyTestdata(t, "literal"), "literal")
|
|
path, warnings, err := Write(ctx, dir, DeriveOptions{})
|
|
if err != nil {
|
|
t.Fatalf("Write: %v", err)
|
|
}
|
|
if len(warnings) != 0 {
|
|
t.Errorf("warnings = %v", warnings)
|
|
}
|
|
if path != filepath.Join(dir, FileName) {
|
|
t.Errorf("path = %s", path)
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(string(raw), Header+"\n") {
|
|
t.Errorf("plugin.yaml does not start with the header:\n%s", raw)
|
|
}
|
|
m, err := Parse(raw)
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
want := Manifest{
|
|
Schema: SchemaVersion,
|
|
ID: "acme.blog",
|
|
Module: "example.test/sm-acme-plugin",
|
|
Package: "example.test/sm-acme-plugin",
|
|
Name: "Blog",
|
|
Description: "Posts and categories for an acme application, with a feed.",
|
|
Framework: "v0.5.3",
|
|
}
|
|
if m.Schema != want.Schema || m.ID != want.ID || m.Module != want.Module || m.Package != want.Package ||
|
|
m.Name != want.Name || m.Description != want.Description || m.Framework != want.Framework ||
|
|
len(m.Requires) != 0 || m.Overrides != nil {
|
|
t.Errorf("manifest = %+v, want %+v", m, want)
|
|
}
|
|
wantOrder := []string{"schema:", "id:", "module:", "package:", "name:", "description:", "framework:"}
|
|
lines := strings.Split(strings.TrimSpace(string(raw)), "\n")[1:]
|
|
if len(lines) != len(wantOrder) {
|
|
t.Fatalf("lines = %q", lines)
|
|
}
|
|
for i, prefix := range wantOrder {
|
|
if !strings.HasPrefix(lines[i], prefix) {
|
|
t.Errorf("line %d = %q, want prefix %q", i+2, lines[i], prefix)
|
|
}
|
|
}
|
|
|
|
if err := Check(ctx, dir); err != nil {
|
|
t.Fatalf("Check on a fresh plugin.yaml: %v", err)
|
|
}
|
|
|
|
readme := filepath.Join(dir, "README.md")
|
|
src, _ := os.ReadFile(readme)
|
|
edited := strings.Replace(string(src), "with a feed.", "with an Atom feed.", 1)
|
|
if err := os.WriteFile(readme, []byte(edited), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var stale *StaleError
|
|
if err := Check(ctx, dir); !errors.As(err, &stale) || stale.Missing {
|
|
t.Fatalf("Check after a README edit = %v, want a stale *StaleError", err)
|
|
}
|
|
if !strings.Contains(stale.Error(), path) || !strings.Contains(stale.Error(), "line ") {
|
|
t.Errorf("StaleError text = %q", stale.Error())
|
|
}
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Check(ctx, dir); !errors.As(err, &stale) || !stale.Missing {
|
|
t.Fatalf("Check without plugin.yaml = %v, want Missing", err)
|
|
}
|
|
if len(stale.Want) == 0 {
|
|
t.Error("a missing StaleError carries no Want bytes")
|
|
}
|
|
})
|
|
}
|