53 lines
2.1 KiB
Go
53 lines
2.1 KiB
Go
package ckconfig_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
|
"gitea.maximumdirect.net/eric/CommandKit/integrations/ckconfig"
|
|
)
|
|
|
|
func TestParentHelpDiscoversMetadataOnlyGroups(t *testing.T) {
|
|
leaf := commandkit.Group("notes", "Read notes metadata.")
|
|
parent := commandkit.Group("topics", "Browse topic metadata.", leaf)
|
|
app, err := commandkit.New(commandkit.Identity{Name: "example"}, parent)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loads := 0
|
|
if err := ckconfig.RegisterConfig(app, ckconfig.ConfigSuite[struct{}]{Scopes: []ckconfig.ConfigScope[struct{}]{{Name: "app", Operations: []ckconfig.ConfigOperation{ckconfig.ConfigValidate}, Acquire: func(context.Context, ckconfig.ConfigRequest, struct{}) (commandkit.Acquired[ckconfig.ConfigView], error) {
|
|
loads++
|
|
return commandkit.Acquired[ckconfig.ConfigView]{}, nil
|
|
}}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
args []string
|
|
content string
|
|
}{
|
|
{nil, "Browse topic metadata."},
|
|
{[]string{"topics"}, "Read notes metadata."},
|
|
{[]string{"topics", "--help"}, "Read notes metadata."},
|
|
{[]string{"help", "topics"}, "Read notes metadata."},
|
|
{[]string{"topics", "notes"}, "Read notes metadata."},
|
|
{[]string{"help", "topics", "notes"}, "Read notes metadata."},
|
|
} {
|
|
var out bytes.Buffer
|
|
result := app.Execute(context.Background(), tc.args, commandkit.Streams{Out: &out})
|
|
if result.Kind != commandkit.OutcomeSuccess || !strings.Contains(out.String(), tc.content) || loads != 0 {
|
|
t.Fatalf("%v: result=%#v output=%q loads=%d", tc.args, result, out.String(), loads)
|
|
}
|
|
if len(tc.args) == 1 && (!strings.Contains(out.String(), "Available Commands:") || !strings.Contains(out.String(), "[command]")) {
|
|
t.Fatalf("missing child inventory/usage: %q", out.String())
|
|
}
|
|
}
|
|
var diagnostic bytes.Buffer
|
|
result := app.Execute(context.Background(), []string{"topics", "unknown"}, commandkit.Streams{Err: &diagnostic})
|
|
if result.Kind != commandkit.OutcomeUsage || !strings.Contains(diagnostic.String(), "[command]") {
|
|
t.Fatalf("result=%#v diagnostic=%q", result, diagnostic.String())
|
|
}
|
|
}
|