Prepare subtree filter prefixes once per discovery
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -16,7 +17,7 @@ func TestExplainDiscoveryModesAndMetadata(t *testing.T) {
|
||||
loads, inputs, states := 0, 0, 0
|
||||
scope := FromConfigKit("app", "", func(_ context.Context, selection Selection, _, _ struct{}) (*configkit.Result[map[string]any], error) {
|
||||
loads++
|
||||
return configkit.Load(configkit.Mapping("fixture", map[string]any{"a/b": map[string]any{"x": 1}, "ab": 2, "abacus": 3, "gone": map[string]any{"x": 1}}), configkit.Options[map[string]any]{Overrides: selection.Overrides})
|
||||
return configkit.Load(configkit.Mapping("fixture", map[string]any{"a/b": map[string]any{"x": 1}, "a~b": 4, "ab": 2, "abacus": 3, "gone": map[string]any{"x": 1}}), configkit.Options[map[string]any]{Overrides: selection.Overrides})
|
||||
})
|
||||
scope.Features.Overrides = true
|
||||
app := newConfigTestApp(t, Command(Suite[struct{}, struct{}]{NewInput: func() struct{} { inputs++; return struct{}{} }, NewState: func() struct{} { states++; return struct{}{} }, Scopes: []Scope[struct{}, struct{}]{scope}}))
|
||||
@@ -37,7 +38,11 @@ func TestExplainDiscoveryModesAndMetadata(t *testing.T) {
|
||||
}
|
||||
result, out = run("--list", "--under=/a~1b", "--format=json")
|
||||
var report pathDiscoveryReport
|
||||
if result.Kind != commandkit.OutcomeSuccess || json.Unmarshal([]byte(out), &report) != nil || len(report.Paths) != 2 {
|
||||
decode := func(output string) error {
|
||||
report = pathDiscoveryReport{}
|
||||
return json.Unmarshal([]byte(output), &report)
|
||||
}
|
||||
if result.Kind != commandkit.OutcomeSuccess || decode(out) != nil || len(report.Paths) != 2 {
|
||||
t.Fatalf("escaped subtree: %#v %q", result, out)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
@@ -48,11 +53,46 @@ func TestExplainDiscoveryModesAndMetadata(t *testing.T) {
|
||||
t.Fatalf("escaped paths: %#v", report.Paths)
|
||||
}
|
||||
result, out = run("--list", "--under=/ab", "--format=json")
|
||||
if result.Kind != commandkit.OutcomeSuccess || json.Unmarshal([]byte(out), &report) != nil || len(report.Paths) != 1 || report.Paths[0].Path != "/ab" {
|
||||
if result.Kind != commandkit.OutcomeSuccess || decode(out) != nil || len(report.Paths) != 1 || report.Paths[0].Path != "/ab" {
|
||||
t.Fatalf("token boundary: %#v %q", result, out)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
prefix string
|
||||
paths []string
|
||||
}{
|
||||
{"/a~1b/x", []string{"/a~1b/x"}},
|
||||
{"/a~0b", []string{"/a~0b"}},
|
||||
{"/a", nil},
|
||||
{"/a~1b/x/unknown", nil},
|
||||
{"/ab/" + strings.Repeat("too-deep/", 20) + "unknown", nil},
|
||||
} {
|
||||
result, out = run("--list", "--under="+tc.prefix, "--format=json")
|
||||
if result.Status != 0 || decode(out) != nil {
|
||||
t.Fatalf("prefix %q: %#v %s", tc.prefix, result, out)
|
||||
}
|
||||
var paths []string
|
||||
for _, entry := range report.Paths {
|
||||
paths = append(paths, entry.Path)
|
||||
}
|
||||
if !reflect.DeepEqual(paths, tc.paths) {
|
||||
t.Fatalf("prefix %q: paths=%v want=%v", tc.prefix, paths, tc.paths)
|
||||
}
|
||||
}
|
||||
result, baseline := run("--list", "--format=json")
|
||||
if result.Status != 0 {
|
||||
t.Fatalf("unfiltered discovery=%#v", result)
|
||||
}
|
||||
var all pathDiscoveryReport
|
||||
if err := json.Unmarshal([]byte(baseline), &all); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, out = run("--list", "--under=", "--format=json")
|
||||
if result.Status != 0 || decode(out) != nil || !reflect.DeepEqual(report, all) {
|
||||
t.Fatalf("root subtree changed discovery: %#v %s", result, out)
|
||||
}
|
||||
result, out = run("--list", "--historical", "--set=/gone=null", "--format=json")
|
||||
if result.Kind != commandkit.OutcomeSuccess || json.Unmarshal([]byte(out), &report) != nil {
|
||||
if result.Kind != commandkit.OutcomeSuccess || decode(out) != nil {
|
||||
t.Fatalf("historical: %#v %q", result, out)
|
||||
}
|
||||
removed := false
|
||||
@@ -64,8 +104,8 @@ func TestExplainDiscoveryModesAndMetadata(t *testing.T) {
|
||||
if !removed {
|
||||
t.Fatalf("missing historical entry: %q", out)
|
||||
}
|
||||
result, out = run("--list", "--historical", "--explicit=false", "--set=/gone=null", "--format=json")
|
||||
if result.Kind != commandkit.OutcomeSuccess || json.Unmarshal([]byte(out), &report) != nil {
|
||||
result, out = run("--list", "--under=/gone", "--historical", "--explicit=false", "--set=/gone=null", "--format=json")
|
||||
if result.Kind != commandkit.OutcomeSuccess || decode(out) != nil {
|
||||
t.Fatalf("explicit filter: %#v %q", result, out)
|
||||
}
|
||||
for _, entry := range report.Paths {
|
||||
|
||||
Reference in New Issue
Block a user