Compose runtime glossary files with native configuration
This commit is contained in:
136
internal/appconfig/runtime_glossary_test.go
Normal file
136
internal/appconfig/runtime_glossary_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package appconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
ck "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestRuntimeGlossaryComposition(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
files := map[string]string{
|
||||
"main.yml": `version: 3
|
||||
glossary_collections:
|
||||
party:
|
||||
instruction: Keep the configured guidance
|
||||
entries: [{id: configured, name: Configured, category: person}]
|
||||
reference:
|
||||
entries: [{id: reference, name: Reference, category: lore}]
|
||||
`,
|
||||
"overlays/selected.yml": `glossary_collections:
|
||||
party:
|
||||
entries: [{id: overlay, name: Overlay, category: person}]
|
||||
`,
|
||||
"first.yml": `config:
|
||||
imports: [{path: imported.yml, required: true}]
|
||||
party:
|
||||
entries:
|
||||
- {id: shared, name: Old, category: person, aliases: [Former]}
|
||||
- {id: retained, name: Retained, category: person}
|
||||
`,
|
||||
"imported.yml": `other:
|
||||
entries: [{id: imported, name: Imported, category: lore}]
|
||||
`,
|
||||
"last.yml": `party:
|
||||
entries:
|
||||
- {id: shared, name: New, category: person, note: kept}
|
||||
`,
|
||||
}
|
||||
for name, contents := range files {
|
||||
path := filepath.Join(dir, name)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(contents), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
paths := []string{filepath.Join(dir, "first.yml"), filepath.Join(dir, "last.yml")}
|
||||
acquired, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "main.yml"))}, Options{Overlay: "selected", GlossaryFiles: paths})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
collections := acquired.Config.Application.GlossaryCollections
|
||||
party := collections["party"]
|
||||
if party.Instruction != "Keep the configured guidance" || len(party.Entries) != 2 {
|
||||
t.Fatalf("party = %+v", party)
|
||||
}
|
||||
if party.Entries[0].ID != "shared" || party.Entries[0].Name != "New" || len(party.Entries[0].Aliases) != 0 || party.Entries[0].Extra["note"] != "kept" || party.Entries[1].ID != "retained" {
|
||||
t.Fatalf("entries = %+v", party.Entries)
|
||||
}
|
||||
if len(collections["reference"].Entries) != 1 || len(collections["other"].Entries) != 1 {
|
||||
t.Fatalf("collections = %+v", collections)
|
||||
}
|
||||
for _, name := range []string{"first.yml", "last.yml", "imported.yml"} {
|
||||
path := filepath.Join(dir, name)
|
||||
if !slices.Contains(acquired.GlossarySources, path) {
|
||||
t.Errorf("missing source %s: %v", path, acquired.GlossarySources)
|
||||
}
|
||||
for _, source := range acquired.Sources() {
|
||||
if source.Path == path {
|
||||
t.Errorf("preliminary source leaked into final result: %s", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
info, ok := acquired.Metadata().Lookup(ck.Keys("glossary_collections", "party", "entries"))
|
||||
if !ok || info.Origin.Layer != ck.LayerCLI {
|
||||
t.Fatalf("origin = %+v", info)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeGlossaryLoadErrors(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "main.yml")
|
||||
if err := os.WriteFile(main, []byte("version: 3\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
malformed := filepath.Join(dir, "bad.yml")
|
||||
if err := os.WriteFile(malformed, []byte("party: [\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range []string{malformed, filepath.Join(dir, "missing.yml")} {
|
||||
_, report, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{GlossaryFiles: []string{path}})
|
||||
var inputErr *GlossaryInputError
|
||||
var loadErr *ck.LoadError
|
||||
if !errors.As(err, &inputErr) || !errors.As(err, &loadErr) || report == nil {
|
||||
t.Fatalf("path %s: report=%v error=%v", path, report, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeGlossaryPresence(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, body string
|
||||
wantInstruction string
|
||||
wantEntries int
|
||||
}{
|
||||
{"omitted", "party: {}\n", "original", 1},
|
||||
{"empty", "party: {instruction: '', entries: []}\n", "", 0},
|
||||
{"null entries", "party: {entries: null}\n", "original", 0},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "main.yml")
|
||||
incoming := filepath.Join(dir, "input.yml")
|
||||
if err := os.WriteFile(main, []byte("glossary_collections:\n party:\n instruction: original\n entries: [{id: a, name: A, category: C}]\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(incoming, []byte(tc.body), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _, err := Load(context.Background(), ck.Input{Source: ck.File(main)}, Options{GlossaryFiles: []string{incoming}})
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %+v", err, err.(*ck.LoadError).Diagnostics())
|
||||
}
|
||||
party := got.Config.Application.GlossaryCollections["party"]
|
||||
if party.Instruction != tc.wantInstruction || len(party.Entries) != tc.wantEntries {
|
||||
t.Fatalf("party = %+v", party)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user