Derive selected overlay context from the main input
This commit is contained in:
@@ -156,3 +156,65 @@ func TestMainRelativeWithoutBaseImportDefaults(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLayeredOverlayContextSelection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
base := filepath.Join(root, "base")
|
||||
for _, dir := range []string{filepath.Join(root, "overlays"), filepath.Join(base, "overlays")} {
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for path, content := range map[string]string{"base/main.yml": "count: 1\n", "base/overlays/test.yml": "count: 2\n", "overlays/test.yml": "count: 3\n"} {
|
||||
if err := os.WriteFile(filepath.Join(root, path), []byte(content), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
main := configkit.Input{Source: configkit.File(filepath.Join(base, "main.yml")), Filesystem: &configkit.FilesystemContext{Root: root}}
|
||||
result, err := configkit.LoadLayered(main, configkit.LayeredOptions[layeredFacadeConfig]{Overlay: "test"})
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if result.Config.Count != 2 {
|
||||
t.Fatalf("derived overlay = %d", result.Config.Count)
|
||||
}
|
||||
result, err = configkit.LoadLayered(main, configkit.LayeredOptions[layeredFacadeConfig]{Overlay: "test", OverlayFilesystem: &configkit.FilesystemContext{Root: root, BaseDir: base}})
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if result.Config.Count != 3 {
|
||||
t.Fatalf("explicit overlay = %d", result.Config.Count)
|
||||
}
|
||||
result, err = configkit.LoadLayered(configkit.Input{Source: configkit.NoSource()}, configkit.LayeredOptions[layeredFacadeConfig]{Overlay: "test", OverlayFallback: &configkit.FilesystemContext{Root: root, BaseDir: base}})
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if result.Config.Count != 2 {
|
||||
t.Fatalf("fallback overlay = %d", result.Config.Count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLayeredOverlayNativeContext(t *testing.T) {
|
||||
filesystem := configkit.NewFilesystem("bundle", fstest.MapFS{
|
||||
"dir/main.yml": &fstest.MapFile{Data: []byte("count: 1\n")},
|
||||
"dir/overlays/test.yml": &fstest.MapFile{Data: []byte("count: 9\n")},
|
||||
})
|
||||
main := configkit.Input{Source: filesystem.File("dir/main.yml"), Filesystem: &configkit.FilesystemContext{Root: ".", FS: &filesystem}}
|
||||
result, err := configkit.LoadLayered(main, configkit.LayeredOptions[layeredFacadeConfig]{Overlay: "test"})
|
||||
if err != nil {
|
||||
t.Fatalf("%v: %#v", err, err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
if result.Config.Count != 9 {
|
||||
t.Fatalf("config = %#v", result.Config)
|
||||
}
|
||||
_, err = configkit.LoadLayered(main, configkit.LayeredOptions[layeredFacadeConfig]{Overlay: "missing", OverlayFallback: &configkit.FilesystemContext{Root: ".", BaseDir: ".", FS: &filesystem}})
|
||||
if err == nil {
|
||||
t.Fatal("missing selected overlay succeeded")
|
||||
}
|
||||
for _, diagnostic := range err.(*configkit.LoadError).Diagnostics() {
|
||||
if diagnostic.Code == configkit.CodeOverlayMissing {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("diagnostics = %#v", err.(*configkit.LoadError).Diagnostics())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user