Prepare ConfigKit v0.1.2
This commit is contained in:
@@ -2,9 +2,95 @@ package configkit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMetadataHistoricalPaths(t *testing.T) {
|
||||
t.Parallel()
|
||||
result, err := Load[map[string]any](Bytes("base", []byte("retired: {child: 1}\nshape: {'0': base}\n'a/b~': {'': 2}\n")), Options[map[string]any]{
|
||||
Overrides: []Override{
|
||||
{Path: Keys("retired"), Value: nil, Flag: "--retired"},
|
||||
{Path: Keys("shape"), Value: []any{"replacement"}, Flag: "--shape"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metadata := result.Metadata()
|
||||
paths := metadata.HistoricalPaths()
|
||||
var got []string
|
||||
for _, path := range paths {
|
||||
got = append(got, path.String())
|
||||
if len(metadata.History(path)) == 0 {
|
||||
t.Fatalf("enumerated path %s has no history", path)
|
||||
}
|
||||
}
|
||||
want := []string{"", "/a~1b~0", "/a~1b~0/", "/retired", "/retired/child", "/shape", "/shape/0"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("historical paths = %v, want %v", got, want)
|
||||
}
|
||||
if _, ok := metadata.Lookup(paths[4]); ok {
|
||||
t.Fatal("removed descendant remains effective")
|
||||
}
|
||||
if _, ok := metadata.Lookup(paths[6]); !ok {
|
||||
t.Fatal("enumerated numeric path cannot query the new sequence shape")
|
||||
}
|
||||
// Returned slices and nested paths must not alias retained metadata.
|
||||
paths[1].segments[0].key = "mutated"
|
||||
paths[2] = Keys("replacement")
|
||||
if again := metadata.HistoricalPaths(); again[1].String() != want[1] || again[2].String() != want[2] {
|
||||
t.Fatal("enumeration returned aliased paths")
|
||||
}
|
||||
if len((Metadata{}).HistoricalPaths()) != 0 {
|
||||
t.Fatal("zero metadata contains paths")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoricalPathsExposeHiddenBaseOwnershipWithoutResolvingBaseSecrets(t *testing.T) {
|
||||
t.Setenv("CONFIGKIT_HISTORY_UNUSED_PROD_KEY", "")
|
||||
t.Setenv("CONFIGKIT_HISTORY_TEST_KEY", "synthetic-test-key")
|
||||
type config struct {
|
||||
Credential *Secret `yaml:"credential"`
|
||||
Definitions map[string]map[string]string `yaml:"definitions"`
|
||||
}
|
||||
for _, credential := range []string{"CONFIGKIT_HISTORY_TEST_KEY", "null"} {
|
||||
t.Run(credential, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := writeImportFile(t, dir, "main.yml", "configkit:\n imports: [{path: first.yml}, {path: second.yml}]\ncredential: CONFIGKIT_HISTORY_UNUSED_PROD_KEY\n")
|
||||
writeImportFile(t, dir, "first.yml", "definitions: {recap: {prompt: recap}}\n")
|
||||
writeImportFile(t, dir, "second.yml", "definitions: {recap: {output: recap.md}}\n")
|
||||
writeImportFile(t, dir, "profiles/testing.yml", "definitions: null\ncredential: "+credential+"\n")
|
||||
result, err := Load[config](File(root), Options[config]{Imports: true, Profile: "testing"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metadata := result.Metadata()
|
||||
found := false
|
||||
for _, path := range metadata.HistoricalPaths() {
|
||||
if path.String() != "/definitions/recap" {
|
||||
continue
|
||||
}
|
||||
found = true
|
||||
owners := map[string]bool{}
|
||||
for _, change := range metadata.History(path) {
|
||||
if change.Origin.Layer == LayerBase {
|
||||
for _, location := range change.Origin.Locations {
|
||||
owners[location.SourceID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(owners) != 2 {
|
||||
t.Fatalf("hidden declaration owners = %v", owners)
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("hidden base declaration was not discoverable")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataQueriesReturnDetachedRecords(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user