Load ordinary overlays without a main source
This commit is contained in:
@@ -2,11 +2,14 @@ package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
type overlayTestConfig struct {
|
||||
@@ -21,6 +24,81 @@ type overlayTestConfig struct {
|
||||
Schema map[string]string `yaml:"schema"`
|
||||
}
|
||||
|
||||
func TestNoSourceSelectedOverlayUsesRootedHostContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
type config struct {
|
||||
Resource string `yaml:"resource"`
|
||||
}
|
||||
|
||||
root := t.TempDir()
|
||||
overlayPath := writeImportFile(t, root, "overlays/testing.yml", "resource: asset.txt\n")
|
||||
writeImportFile(t, root, "overlays/asset.txt", "host asset\n")
|
||||
loaded, err := testLoad[config](NoSource(), Options[config]{
|
||||
Overlay: "testing", Filesystem: &FilesystemContext{Root: root},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sources := loaded.Sources()
|
||||
if len(sources) != 1 || sources[0].ID != "source-1" || sources[0].Path != canonicalTestPath(t, overlayPath) || sources[0].Layer != LayerOverlay || sources[0].Kind != SourceKindFile {
|
||||
t.Fatalf("NoSource overlay sources = %#v", sources)
|
||||
}
|
||||
context, ok := loaded.Inspection.SourceContext("source-1")
|
||||
if !ok || context.BaseDir != "overlays" {
|
||||
t.Fatalf("overlay resource context = %#v, %v", context, ok)
|
||||
}
|
||||
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, loaded.Config.Resource))
|
||||
if err != nil || string(data) != "host asset\n" {
|
||||
t.Fatalf("overlay resource = %q, %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoSourceSelectedOverlayRequiresContextAndSelection(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
requireLoadCode(t, NoSource(), Options[importTestConfig]{Overlay: "testing"}, CodeOptionsInvalid)
|
||||
|
||||
root := t.TempDir()
|
||||
requireLoadCode(t, NoSource(), Options[importTestConfig]{
|
||||
Overlay: "testing", Filesystem: &FilesystemContext{Root: root},
|
||||
}, CodeOverlayMissing)
|
||||
|
||||
outside := t.TempDir()
|
||||
requireLoadCode(t, NoSource(), Options[importTestConfig]{
|
||||
Overlay: "testing", Filesystem: &FilesystemContext{Root: root, BaseDir: outside},
|
||||
}, CodeImportOutsideRoot)
|
||||
}
|
||||
|
||||
func TestNoSourceSelectedOverlayUsesCompleteNativeContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
type config struct {
|
||||
Resource string `yaml:"resource"`
|
||||
}
|
||||
|
||||
filesystem := NewFilesystem("embedded", fstest.MapFS{
|
||||
"overlays/testing.yml": &fstest.MapFile{Data: []byte("resource: asset.txt\n")},
|
||||
"overlays/asset.txt": &fstest.MapFile{Data: []byte("native asset\n")},
|
||||
})
|
||||
loaded, err := testLoad[config](NoSource(), Options[config]{
|
||||
Overlay: "testing", Filesystem: &FilesystemContext{FS: &filesystem, BaseDir: ".", Root: "."},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sources := loaded.Sources()
|
||||
if len(sources) != 1 || sources[0].ID != "source-1" || sources[0].Path != "overlays/testing.yml" || sources[0].Layer != LayerOverlay || sources[0].Kind != SourceKindFilesystem {
|
||||
t.Fatalf("native NoSource overlay sources = %#v", sources)
|
||||
}
|
||||
context, ok := loaded.Inspection.SourceContext("source-1")
|
||||
if !ok || context.BaseDir != "overlays" {
|
||||
t.Fatalf("native overlay resource context = %#v, %v", context, ok)
|
||||
}
|
||||
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, loaded.Config.Resource))
|
||||
if err != nil || string(data) != "native asset\n" {
|
||||
t.Fatalf("native overlay resource = %q, %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadAppliesSelectedOverlayAfterBaseAndBeforeDefaults(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
mainPath := writeImportFile(t, directory, "main.yml", `configkit:
|
||||
|
||||
Reference in New Issue
Block a user