138 lines
5.5 KiB
Go
138 lines
5.5 KiB
Go
package commandkit_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
ck "go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
type observationKey struct{}
|
|
type observationInput struct{ enabled bool }
|
|
|
|
func observationFeature() ck.InvocationFeature {
|
|
return ck.NewInvocationFeature(ck.InvocationFeatureSpec[observationInput]{Flags: []ck.Flag[observationInput]{ck.BoolFlag(ck.FlagMeta{Name: "observe", Short: "o"}, false, func(i *observationInput, v ck.Supplied[bool]) { i.enabled = v.Value })}, Start: func(inv ck.Invocation, i observationInput) (context.Context, func() ck.Report) {
|
|
if !i.enabled {
|
|
return nil, nil
|
|
}
|
|
return context.WithValue(inv.Context, observationKey{}, true), func() ck.Report { return ck.Report{Value: "observed"} }
|
|
}})
|
|
}
|
|
|
|
func TestInvocationFeaturePublicAssembly(t *testing.T) {
|
|
feature := observationFeature()
|
|
var contexts []bool
|
|
cmd := ck.NewCommand(ck.CommandSpec[struct{}]{Name: "run", Run: func(inv ck.Invocation, _ struct{}) ck.Outcome {
|
|
contexts = append(contexts, inv.Context.Value(observationKey{}) == true)
|
|
_ = inv.Reporter().Report(ck.Report{Value: map[string]string{"result": "ok"}})
|
|
return ck.Success()
|
|
}})
|
|
owner := ck.WithInvocationFeature(ck.Group("feature", ""), feature)
|
|
for _, featureFirst := range []bool{true, false} {
|
|
var first, second ck.Declaration = owner, cmd
|
|
if !featureFirst {
|
|
first, second = second, first
|
|
}
|
|
app, err := ck.New(ck.Identity{Name: "test"}, first)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := app.Register(second, ck.Alias("execute", "run"), ck.FlagAlias("--watch", "--observe")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
args []string
|
|
summary bool
|
|
}{{[]string{"execute", "--watch", "--format=json"}, true}, {[]string{"run", "--observe=false"}, false}, {[]string{"run", "--observe", "--quiet"}, false}, {[]string{"run", "--quiet", "--observe"}, false}} {
|
|
var out, diag bytes.Buffer
|
|
result := app.Execute(context.Background(), tc.args, ck.Streams{Out: &out, Err: &diag})
|
|
if result.Status != 0 || strings.Contains(diag.String(), "observed") != tc.summary {
|
|
t.Fatalf("args %v: %#v %q", tc.args, result, diag.String())
|
|
}
|
|
if tc.summary && !strings.Contains(out.String(), `"result": "ok"`) {
|
|
t.Fatalf("main output %q", out.String())
|
|
}
|
|
}
|
|
conflict := ck.NewCommand(ck.CommandSpec[struct{}]{Name: "conflict", Flags: []ck.Flag[struct{}]{ck.BoolFlag(ck.FlagMeta{Name: "observe"}, false, func(*struct{}, ck.Supplied[bool]) {})}})
|
|
if app.Register(conflict) == nil {
|
|
t.Fatal("expected collision")
|
|
}
|
|
var diag bytes.Buffer
|
|
if app.Execute(context.Background(), []string{"execute", "--watch"}, ck.Streams{Err: &diag}).Status != 0 || !strings.Contains(diag.String(), "observed") {
|
|
t.Fatal("failed registration changed prior routing")
|
|
}
|
|
}
|
|
other, err := ck.New(ck.Identity{Name: "test"}, owner, cmd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var diag bytes.Buffer
|
|
other.Execute(context.Background(), []string{"run", "--observe"}, ck.Streams{Err: &diag})
|
|
if !strings.Contains(diag.String(), "observed") {
|
|
t.Fatal("feature unavailable in reused app")
|
|
}
|
|
plain, err := ck.New(ck.Identity{Name: "test"}, cmd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result := plain.Execute(context.Background(), []string{"run", "--observe"}, ck.Streams{}); result.Kind != ck.OutcomeUsage {
|
|
t.Fatalf("flag present without feature: %#v", result)
|
|
}
|
|
if len(contexts) != 11 || !contexts[0] || contexts[1] || !contexts[2] {
|
|
t.Fatalf("invocation isolation: %v", contexts)
|
|
}
|
|
}
|
|
|
|
func TestInvocationFeaturePublicFailureAndRollback(t *testing.T) {
|
|
cause := errors.New("canceled")
|
|
feature := observationFeature()
|
|
existing := ck.NewCommand(ck.CommandSpec[struct{}]{Name: "taken", Flags: []ck.Flag[struct{}]{ck.BoolFlag(ck.FlagMeta{Name: "observe"}, false, func(*struct{}, ck.Supplied[bool]) {})}, Run: func(ck.Invocation, struct{}) ck.Outcome { return ck.Success() }})
|
|
app, err := ck.New(ck.Identity{Name: "test"}, existing)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := app.Register(ck.WithInvocationFeature(ck.Group("feature", ""), feature)); err == nil {
|
|
t.Fatal("expected collision")
|
|
}
|
|
if app.Execute(context.Background(), []string{"taken", "--observe"}, ck.Streams{}).Status != 0 {
|
|
t.Fatal("failed registration changed existing command")
|
|
}
|
|
reject := false
|
|
run := ck.WithInvocationFeature(ck.NewCommand(ck.CommandSpec[struct{}]{Name: "run", CheckInput: func(struct{}) error {
|
|
if reject {
|
|
return errors.New("reject")
|
|
}
|
|
return nil
|
|
}, Run: func(inv ck.Invocation, _ struct{}) ck.Outcome {
|
|
_ = inv.Reporter().AtEnd().Warn("deferred")
|
|
return ck.Domain("canceled", 9).WithCause(cause).WithExplanation("stopped")
|
|
}}), feature)
|
|
app, err = ck.New(ck.Identity{Name: "test"}, run)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, mode := range []string{"", "--verbose", "--debug"} {
|
|
args := []string{"run", "--observe"}
|
|
if mode != "" {
|
|
args = append(args, mode)
|
|
}
|
|
var diag bytes.Buffer
|
|
out := app.Execute(context.Background(), args, ck.Streams{Err: &diag})
|
|
if out.Kind != ck.OutcomeDomain || out.Status != 9 || !errors.Is(out.Cause, cause) || !strings.Contains(diag.String(), "observed\nERROR: stopped") {
|
|
t.Fatalf("%v: %#v %q", args, out, diag.String())
|
|
}
|
|
if mode != "" && strings.Index(diag.String(), "deferred") > strings.Index(diag.String(), "observed") {
|
|
t.Fatalf("order %q", diag.String())
|
|
}
|
|
}
|
|
reject = true
|
|
var diag bytes.Buffer
|
|
app.Execute(context.Background(), []string{"run", "--observe"}, ck.Streams{Err: &diag})
|
|
if strings.Contains(diag.String(), "observed") {
|
|
t.Fatal("report after rejected input")
|
|
}
|
|
}
|