54 lines
2.2 KiB
Go
54 lines
2.2 KiB
Go
package ckprompt_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
commandkit "gitea.maximumdirect.net/eric/CommandKit"
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDefinitionInspectionCancellationBoundaries(t *testing.T) {
|
|
for _, resource := range []string{"prompt", "profile"} {
|
|
for _, before := range []bool{true, false} {
|
|
t.Run(fmt.Sprintf("%s/before=%t", resource, before), func(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
if before {
|
|
cancel()
|
|
}
|
|
calls, contexts := 0, 0
|
|
diagnostics := []commandkit.Diagnostic{commandkit.MessageDiagnostic(configkit.SeverityWarning, "acquired warning")}
|
|
suite := commandkit.PromptKitSuite[struct{}]{NewContext: func() struct{} { contexts++; return struct{}{} },
|
|
InspectPrompt: func(context.Context, commandkit.Selection, struct{}, string, string) (commandkit.Acquired[*promptkit.PromptInspection], error) {
|
|
calls++
|
|
cancel()
|
|
return commandkit.Acquired[*promptkit.PromptInspection]{Value: &promptkit.PromptInspection{}, Diagnostics: diagnostics}, nil
|
|
},
|
|
InspectProfile: func(context.Context, commandkit.Selection, struct{}, string) (commandkit.Acquired[*promptkit.ProfileInspection], error) {
|
|
calls++
|
|
cancel()
|
|
return commandkit.Acquired[*promptkit.ProfileInspection]{Value: &promptkit.ProfileInspection{}, Diagnostics: diagnostics}, nil
|
|
},
|
|
}
|
|
app, _ := commandkit.New(commandkit.Identity{Name: "example"})
|
|
if err := commandkit.RegisterPromptKit(app, suite); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out, errs bytes.Buffer
|
|
result := app.Execute(ctx, []string{"promptkit", "show", resource, "example"}, commandkit.Streams{Out: &out, Err: &errs})
|
|
want := 1
|
|
if before {
|
|
want = 0
|
|
}
|
|
if result.Kind != commandkit.OutcomeOperational || !errors.Is(result.Cause, context.Canceled) || calls != want || contexts != want || out.Len() != 0 || (!before && !strings.Contains(errs.String(), "acquired warning")) {
|
|
t.Fatalf("result=%#v calls=%d contexts=%d out=%q diagnostics=%q", result, calls, contexts, out.String(), errs.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|