Bind companion inputs to the main context
This commit is contained in:
70
facade_main_relative_test.go
Normal file
70
facade_main_relative_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package configkit_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestMainRelativeCompanion(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "main.yml"), []byte("count: 3\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "extra.yml"), []byte("settings: {companion: host}\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
binding := configkit.MainRelative()
|
||||
companion := configkit.Input{Source: configkit.Bytes("companions", []byte("config: {imports: [{path: extra.yml}]}\n")), Imports: true, MainRelative: binding}
|
||||
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.File(filepath.Join(dir, "main.yml"))}, configkit.LayeredOptions[layeredFacadeConfig]{Underlays: []configkit.Underlay{{Inputs: []configkit.Input{companion}}}})
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if result.Config.Count != 3 || result.Config.Settings["companion"] != "host" {
|
||||
t.Fatalf("configuration = %#v", result.Config)
|
||||
}
|
||||
native := configkit.NewFilesystem("bundle", fstest.MapFS{"dir/main.yml": &fstest.MapFile{Data: []byte("count: 4\n")}, "dir/extra.yml": &fstest.MapFile{Data: []byte("settings: {companion: native}\n")}})
|
||||
result, err = configkit.LoadLayered(configkit.Input{Source: native.File("dir/main.yml")}, configkit.LayeredOptions[layeredFacadeConfig]{Underlays: []configkit.Underlay{{Inputs: []configkit.Input{companion}}}})
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if result.Config.Count != 4 || result.Config.Settings["companion"] != "native" {
|
||||
t.Fatalf("configuration = %#v", result.Config)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainRelativeContextSelection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
base := filepath.Join(root, "main")
|
||||
if err := os.Mkdir(base, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for path, value := range map[string]string{"main/main.yml": "count: 1\n", "main/extra.yml": "settings: {value: derived}\n", "extra.yml": "settings: {value: fallback}\n"} {
|
||||
if err := os.WriteFile(filepath.Join(root, path), []byte(value), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
link := filepath.Join(root, "link.yml")
|
||||
if err := os.Symlink(filepath.Join(base, "main.yml"), link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
companion := configkit.Input{Source: configkit.Bytes("companion", []byte("config: {imports: [{path: extra.yml}]}\n")), Imports: true, MainRelative: &configkit.MainRelativeContext{Fallback: &configkit.FilesystemContext{Root: root, BaseDir: root}}}
|
||||
opts := configkit.LayeredOptions[layeredFacadeConfig]{Underlays: []configkit.Underlay{{Inputs: []configkit.Input{companion}}}}
|
||||
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.File(link), Filesystem: &configkit.FilesystemContext{Root: root}}, opts)
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if got := result.Config.Settings["value"]; got != "derived" {
|
||||
t.Fatalf("derived value = %q", got)
|
||||
}
|
||||
result, err = configkit.LoadLayered(configkit.Input{Source: configkit.NoSource()}, opts)
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if got := result.Config.Settings["value"]; got != "fallback" {
|
||||
t.Fatalf("fallback value = %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user