Compose application and prompt configuration together
This commit is contained in:
@@ -107,37 +107,20 @@ func defaults() Config {
|
||||
}
|
||||
}
|
||||
|
||||
const glossaryImports = `config:
|
||||
imports:
|
||||
- path: glossary.yml
|
||||
required: false
|
||||
destination: [glossary_collections]
|
||||
- dir: glossary
|
||||
required: false
|
||||
recursive: true
|
||||
pattern: ["*.yml", "*.yaml"]
|
||||
destination: [glossary_collections]
|
||||
`
|
||||
|
||||
// Load resolves the selected main source and preserves native source evidence.
|
||||
func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) {
|
||||
input.Imports = true
|
||||
return ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
||||
Overlay: overlay, Overrides: overrides,
|
||||
Underlays: []ck.Underlay{{Inputs: []ck.Input{{Source: ck.Bytes("Audita glossary imports", []byte(glossaryImports)), Imports: true, MainRelative: ck.MainRelative()}}}},
|
||||
Definition: ck.Definition[Config]{
|
||||
Defaults: defaults,
|
||||
SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")},
|
||||
Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error {
|
||||
if version.Present && (version.Null || version.Text != "3") {
|
||||
v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md")
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
Resolve: resolveSettings,
|
||||
Validate: validate,
|
||||
},
|
||||
})
|
||||
// Definition describes the application settings independently of their sources.
|
||||
func Definition() ck.Definition[Config] {
|
||||
return ck.Definition[Config]{
|
||||
Defaults: defaults,
|
||||
SequenceIdentities: []ck.SequenceIdentity{ck.SequenceIdentityMatching(ck.SequenceSelector{ck.SequenceLiteralKey("glossary_collections"), ck.SequenceAnyMappingKey(), ck.SequenceLiteralKey("entries")}, "id")},
|
||||
Version: &ck.VersionRule{Path: ck.Keys("version"), Check: func(version ck.Version, v *ck.Validation) error {
|
||||
if version.Present && (version.Null || version.Text != "3") {
|
||||
v.Error(ck.Keys("version"), "use application configuration version 3; see docs/migrations/v3.md")
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
Resolve: resolveSettings,
|
||||
Validate: validate,
|
||||
}
|
||||
}
|
||||
|
||||
func validate(c *Config, v *ck.Validation) {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -40,109 +38,6 @@ func TestDefaultsAndVersion(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestMainContextImportsAndOverlay(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, contents := range map[string]string{
|
||||
"main.yml": "version: 3\nconfig:\n imports:\n - path: fragment.yml\noutput:\n schema: bare-segments\n",
|
||||
"fragment.yml": "context:\n description: imported\n",
|
||||
"overlays/selected.yml": "context:\n description: overlay\n",
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "selected", ck.Override{Path: ck.Keys("context", "description"), Value: "override", Flag: "--description"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info, ok := result.Metadata().Lookup(ck.Keys("context", "description")); !ok || info.Origin.Layer != ck.LayerCLI {
|
||||
t.Fatalf("override origin = %+v", info)
|
||||
}
|
||||
if result.Config.Context.Description != "override" {
|
||||
t.Fatalf("description = %q", result.Config.Context.Description)
|
||||
}
|
||||
found := false
|
||||
for _, source := range result.Sources() {
|
||||
if source.Layer == ck.LayerMain && source.ImportedFrom == nil {
|
||||
context, ok := result.SourceContext(source.ID)
|
||||
if !ok {
|
||||
t.Fatal("main context missing")
|
||||
}
|
||||
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "fragment.yml"))
|
||||
if err != nil || len(data) == 0 {
|
||||
t.Fatalf("main context cannot access companion: %v", err)
|
||||
}
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("main source missing")
|
||||
}
|
||||
if _, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, "missing"); err == nil {
|
||||
t.Fatal("missing overlay accepted")
|
||||
}
|
||||
if _, err := Load(ck.Input{Source: ck.NoSource()}, "selected"); err == nil {
|
||||
t.Fatal("overlay without context accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryAndSelectedFailure(t *testing.T) {
|
||||
first, second := t.TempDir(), t.TempDir()
|
||||
search := ck.Searches(ck.SearchDirectory(first), ck.SearchDirectory(second))
|
||||
load := func() (*ck.Result[Config], error) {
|
||||
return Load(ck.Input{Source: ck.SearchConfig("audita", search)}, "")
|
||||
}
|
||||
result, err := load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, source := range result.Sources() {
|
||||
if source.Layer == ck.LayerMain {
|
||||
t.Fatalf("unexpected main source: %+v", source)
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(second, "config.yml"), []byte("context:\n description: selected\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err = load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Config.Context.Description != "selected" {
|
||||
t.Fatalf("description = %q", result.Config.Context.Description)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(first, "config.yml"), []byte("version: 2\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := load(); err == nil {
|
||||
t.Fatal("invalid selected file fell through")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverlayUsesMainDirectoryWithinWiderRoot(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
dir := filepath.Join(root, "project")
|
||||
if err := os.MkdirAll(filepath.Join(dir, "overlays"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "config.yml"), []byte("version: 3\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "overlays", "local.yml"), []byte("context:\n description: project\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml")), Filesystem: &ck.FilesystemContext{Root: root}}, "local")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Config.Context.Description != "project" {
|
||||
t.Fatalf("description = %q", result.Config.Context.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaConstraintsAndExplicitValues(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
for _, tc := range []struct {
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestGlossarySourcesComposeAndRetainExtensions(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
write := func(name, data string) {
|
||||
t.Helper()
|
||||
file := filepath.Join(dir, name)
|
||||
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(file, []byte(data), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
write("config.yml", `version: 3
|
||||
config:
|
||||
imports: []
|
||||
glossary_collections:
|
||||
party:
|
||||
entries:
|
||||
- id: hero
|
||||
name: Revised
|
||||
category: character
|
||||
level: 5
|
||||
`)
|
||||
write("glossary.yml", `config:
|
||||
imports:
|
||||
- path: shared/extra.yml
|
||||
required: true
|
||||
party:
|
||||
entries:
|
||||
- id: hero
|
||||
name: Original
|
||||
category: character
|
||||
aliases: [Old]
|
||||
- id: other
|
||||
name: Other
|
||||
category: place
|
||||
`)
|
||||
write("shared/extra.yml", "lore:\n entries:\n - id: hero\n name: Ancient\n category: lore\n")
|
||||
write("glossary/nested/spells.yaml", "spells:\n entries:\n - id: bolt\n name: Bolt\n category: spell\n aliases: [Spark]\n plural: Bolts\n extra: {rank: 2}\n")
|
||||
result, err := Load(ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "")
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
|
||||
}
|
||||
party := result.Config.GlossaryCollections["party"].Entries
|
||||
if len(party) != 2 || party[0].Name != "Revised" || len(party[0].Aliases) != 0 || party[1].ID != "other" {
|
||||
t.Fatalf("party=%+v", party)
|
||||
}
|
||||
if party[0].Extra["level"] == nil {
|
||||
t.Fatalf("extension missing: %+v", party[0])
|
||||
}
|
||||
if got := result.Config.GlossaryCollections["lore"].Entries; len(got) != 1 || got[0].ID != "hero" {
|
||||
t.Fatalf("lore=%+v", got)
|
||||
}
|
||||
discovered, err := Load(ck.Input{Source: ck.SearchConfig("audita", ck.SearchDirectory(dir))}, "")
|
||||
if err != nil || len(discovered.Config.GlossaryCollections["party"].Entries) != 2 {
|
||||
t.Fatalf("discovered glossary: %+v %v", discovered, err)
|
||||
}
|
||||
spells := result.Config.GlossaryCollections["spells"].Entries
|
||||
if len(spells) != 1 || spells[0].Plural == nil || *spells[0].Plural != "Bolts" || spells[0].Extra["extra"] == nil {
|
||||
t.Fatalf("spells=%+v", spells)
|
||||
}
|
||||
var paths []string
|
||||
for _, source := range result.Sources() {
|
||||
if source.Kind == ck.SourceKindFile {
|
||||
paths = append(paths, source.Path)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"glossary.yml", "shared/extra.yml", "glossary/nested/spells.yaml"} {
|
||||
if !slices.Contains(paths, filepath.Join(dir, name)) {
|
||||
t.Errorf("missing source %s: %v", name, paths)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlossaryValidationAndPeerConflicts(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
write := func(path, data string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, data := range []string{
|
||||
"glossary_collections:\n empty: {entries: []}\n",
|
||||
"glossary_collections:\n x:\n entries:\n - {id: 'id with spaces', name: N, category: C}\n",
|
||||
} {
|
||||
write(main, data)
|
||||
if _, err := Load(ck.Input{Source: ck.File(main)}, ""); err != nil {
|
||||
t.Fatalf("valid %q: %v", data, err)
|
||||
}
|
||||
}
|
||||
for _, data := range []string{
|
||||
"glossary_collections:\n x:\n entries:\n - {id: '', name: N, category: C}\n",
|
||||
"glossary_collections:\n x:\n entries:\n - {id: x, name: '', category: C}\n",
|
||||
"glossary_collections:\n x:\n entries:\n - {id: x, name: N, category: C, aliases: ['']}\n",
|
||||
"glossary_collections:\n x:\n entries:\n - {id: x, name: N, category: C, plural: ''}\n",
|
||||
} {
|
||||
write(main, data)
|
||||
if _, err := Load(ck.Input{Source: ck.File(main)}, ""); err == nil {
|
||||
t.Fatalf("accepted %q", data)
|
||||
}
|
||||
}
|
||||
write(main, "version: 3\n")
|
||||
write(filepath.Join(dir, "glossary.yml"), "x:\n entries:\n - {id: same, name: One, category: C}\n")
|
||||
if err := os.Mkdir(filepath.Join(dir, "glossary"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
write(filepath.Join(dir, "glossary", "other.yml"), "x:\n entries:\n - {id: same, name: Two, category: C}\n")
|
||||
if _, err := Load(ck.Input{Source: ck.File(main)}, ""); err == nil {
|
||||
t.Fatal("peer conflict accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlossaryUnderlaySkipsAbsentMain(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "glossary.yml"), []byte("leak:\n entries:\n - {id: a, name: A, category: C}\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Chdir(dir)
|
||||
result, err := Load(ck.Input{Source: ck.NoSource()}, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Config.GlossaryCollections) != 0 {
|
||||
t.Fatalf("unanchored glossary loaded: %+v", result.Config.GlossaryCollections)
|
||||
}
|
||||
}
|
||||
12
internal/core/config/loader_test.go
Normal file
12
internal/core/config/loader_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package config
|
||||
|
||||
import ck "go.maximumdirect.net/configkit"
|
||||
|
||||
func Load(input ck.Input, overlay string, overrides ...ck.Override) (*ck.Result[Config], error) {
|
||||
input.Imports = true
|
||||
return ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
||||
Definition: Definition(),
|
||||
Overlay: overlay,
|
||||
Overrides: overrides,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user