Files
configkit/internal/engine/filesystem_context_test.go

65 lines
2.6 KiB
Go

package engine
import (
"path/filepath"
"testing"
)
func TestHostFilesystemContextFacts(t *testing.T) {
t.Parallel()
base := canonicalTestPath(t, t.TempDir())
other := canonicalTestPath(t, t.TempDir())
mainPath := writeImportFile(t, base, "main.yml", "value: main\n")
for _, entry := range []struct {
name string
source Source
context *FilesystemContext
main parsedSource
root, lookup, bound string
code Code
}{
{name: "selected default", source: File(mainPath), main: parsedSource{source: &SourceInfo{Path: mainPath}}, root: base, lookup: base},
{name: "selected bounded", source: File(mainPath), context: &FilesystemContext{Root: base}, main: parsedSource{source: &SourceInfo{Path: mainPath}}, root: base, lookup: base, bound: base},
{name: "synthetic base", source: Bytes("main", nil), context: &FilesystemContext{BaseDir: base}, main: parsedSource{source: &SourceInfo{}}, root: base, lookup: base},
{name: "absent base", source: NoSource(), context: &FilesystemContext{BaseDir: base}, root: base, lookup: base},
{name: "absent root", source: NoSource(), context: &FilesystemContext{Root: base}, root: base, lookup: base, bound: base},
{name: "outside base", source: NoSource(), context: &FilesystemContext{BaseDir: other, Root: base}, code: CodeImportOutsideRoot},
} {
t.Run(entry.name, func(t *testing.T) {
if entry.context != nil {
if diagnostics := filesystemOptionDiagnostics(entry.source, entry.context); len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
}
got, diagnostic := validateFilesystemContext(entry.source, entry.context, entry.main, nil)
if entry.code != "" {
if diagnostic == nil || diagnostic.Code != entry.code {
t.Fatalf("diagnostic = %v", diagnostic)
}
return
}
if diagnostic != nil {
t.Fatal(diagnostic)
}
if got.rootPath != entry.root || got.basePath != entry.lookup || got.importRoot != entry.bound {
t.Fatalf("context = %#v", got)
}
})
}
}
func TestSearchBaseOnlyContextDependsOnSelection(t *testing.T) {
t.Parallel()
base := t.TempDir()
main := writeImportFile(t, base, "main.yml", "value: main\n")
options := Options[discoveryLoadConfig]{Filesystem: &FilesystemContext{BaseDir: base}}
if _, err := testLoad(SearchConfig("", Paths(filepath.Join(base, "missing.yml"))), options); err != nil {
t.Fatal(err)
}
// Once a file is selected, its directory owns the base. Selection is terminal.
if loaded, err := testLoad(SearchConfig("", Paths(main)), options); err == nil || loaded != nil {
t.Fatalf("selected = %v, %v", loaded, err)
}
}