Use native path readers for discovery
This commit is contained in:
@@ -154,3 +154,68 @@ func TestExplainBareHelpRecordsDeliveryFailure(t *testing.T) {
|
||||
t.Fatalf("help delivery = %#v, input/state/acquire=%d/%d/%d", result, inputs, states, calls)
|
||||
}
|
||||
}
|
||||
|
||||
type discoveryPaths []configkit.Path
|
||||
|
||||
func (paths discoveryPaths) HistoricalPaths() []configkit.Path { return paths }
|
||||
|
||||
func TestExplainDiscoveryUsesSuppliedPaths(t *testing.T) {
|
||||
scope := FromConfigKit("app", "", func(_ context.Context, selection Selection, _, _ struct{}) (*configkit.Result[map[string]any], error) {
|
||||
return configkit.Load(configkit.Mapping("fixture", map[string]any{
|
||||
"focus": map[string]any{"keep": 1, "gone": map[string]any{"child": 2}},
|
||||
"sibling": 3,
|
||||
}), configkit.Options[map[string]any]{Overrides: selection.Overrides})
|
||||
})
|
||||
scope.Features.Overrides = true
|
||||
acquire := scope.Acquire
|
||||
var reader PathReader
|
||||
scope.Acquire = func(ctx context.Context, request Request, input, state struct{}) (commandkit.Acquired[View], error) {
|
||||
acquired, err := acquire(ctx, request, input, state)
|
||||
acquired.Value.Paths = reader
|
||||
return acquired, err
|
||||
}
|
||||
app := newConfigTestApp(t, Command(Suite[struct{}, struct{}]{Scopes: []Scope[struct{}, struct{}]{scope}}))
|
||||
run := func(args ...string) pathDiscoveryReport {
|
||||
t.Helper()
|
||||
var out bytes.Buffer
|
||||
arguments := append([]string{"config", "explain", "--list", "--format=json", "--set=/focus/gone=null"}, args...)
|
||||
outcome := app.Execute(context.Background(), arguments, commandkit.Streams{Out: &out, Err: io.Discard})
|
||||
var report pathDiscoveryReport
|
||||
if outcome.Kind != commandkit.OutcomeSuccess {
|
||||
t.Fatalf("discovery %v: %#v", args, outcome)
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return report
|
||||
}
|
||||
reader = discoveryPaths{configkit.Keys("focus", "keep"), configkit.Keys("focus", "gone"), configkit.Keys("focus", "gone", "child")}
|
||||
check := func(report pathDiscoveryReport, want ...string) {
|
||||
t.Helper()
|
||||
var got []string
|
||||
for _, entry := range report.Paths {
|
||||
got = append(got, entry.Path)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("paths = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
check(run(), "/focus/keep", "/focus/gone")
|
||||
check(run("--historical"), "/focus/keep", "/focus/gone", "/focus/gone/child")
|
||||
check(run("--under=/focus/gone", "--historical"), "/focus/gone", "/focus/gone/child")
|
||||
check(run("--historical", "--explicit=true"), "/focus/keep", "/focus/gone")
|
||||
check(run("--historical", "--explicit=false"))
|
||||
reader = discoveryPaths(nil)
|
||||
check(run("--historical"))
|
||||
reader = discoveryPaths{}
|
||||
check(run("--historical"))
|
||||
reader = nil
|
||||
fallback := run()
|
||||
foundSibling := false
|
||||
for _, entry := range fallback.Paths {
|
||||
foundSibling = foundSibling || entry.Path == "/sibling"
|
||||
}
|
||||
if !foundSibling {
|
||||
t.Fatalf("metadata fallback omitted sibling: %#v", fallback.Paths)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user