53 lines
2.3 KiB
Go
53 lines
2.3 KiB
Go
package ckconfig_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"go.maximumdirect.net/ckconfig"
|
|
ck "go.maximumdirect.net/commandkit"
|
|
"go.maximumdirect.net/configkit"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestExplainHelpExemptsInvocationFeatures(t *testing.T) {
|
|
starts, inputs, loads := 0, 0, 0
|
|
feature := ck.NewInvocationFeature(ck.InvocationFeatureSpec[struct{}]{
|
|
NewInput: func() struct{} { inputs++; return struct{}{} },
|
|
Flags: []ck.Flag[struct{}]{ck.BoolFlag(ck.FlagMeta{Name: "observe", Required: true}, false, func(*struct{}, ck.Supplied[bool]) {})},
|
|
Start: func(ck.Invocation, struct{}) (context.Context, func() ck.Report) {
|
|
starts++
|
|
return nil, func() ck.Report { return ck.Report{Value: "summary"} }
|
|
},
|
|
})
|
|
config := ckconfig.AppCommand(func(context.Context, ckconfig.Selection) (*configkit.Result[struct{}], error) {
|
|
loads++
|
|
return nil, errors.New("load failed")
|
|
})
|
|
app, err := ck.New(ck.Identity{Name: "test"}, config, ck.WithInvocationFeature(ck.Group("feature", ""), feature), ck.Alias("why", "config explain"), ck.NewCommand(ck.CommandSpec[struct{}]{Name: "run", Run: func(ck.Invocation, struct{}) ck.Outcome { return ck.Success() }}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, args := range [][]string{{"config", "explain"}, {"config", "explain", "--help"}, {"why", "--observe"}} {
|
|
var out, diag bytes.Buffer
|
|
result := app.Execute(context.Background(), args, ck.Streams{Out: &out, Err: &diag})
|
|
if result.Status != 0 || !strings.Contains(out.String(), "Usage:") || diag.Len() != 0 || starts != 0 || inputs != 0 || loads != 0 {
|
|
t.Fatalf("%v: %#v %q %q starts=%d inputs=%d loads=%d", args, result, out.String(), diag.String(), starts, inputs, loads)
|
|
}
|
|
}
|
|
for _, args := range [][]string{{"config", "explain", "--path=/"}, {"run"}, {"config", "explain", "--observe=bad"}} {
|
|
if r := app.Execute(context.Background(), args, ck.Streams{}); r.Kind != ck.OutcomeUsage {
|
|
t.Fatalf("%v: %#v", args, r)
|
|
}
|
|
}
|
|
var diag bytes.Buffer
|
|
if r := app.Execute(context.Background(), []string{"run", "--observe"}, ck.Streams{Err: &diag}); r.Status != 0 || starts != 1 || !strings.Contains(diag.String(), "summary") {
|
|
t.Fatalf("%#v %q", r, diag.String())
|
|
}
|
|
app.Execute(context.Background(), []string{"config", "explain", "--path=/", "--observe"}, ck.Streams{})
|
|
if starts != 2 || loads != 1 {
|
|
t.Fatalf("starts=%d loads=%d", starts, loads)
|
|
}
|
|
}
|