82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package config
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"yetano.dev/yetano/core/paths"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestConfigResolverAccessors(t *testing.T) {
|
||
|
|
r := mustResolver(t, yetApp(), WithFilename("custom.yaml"))
|
||
|
|
if r.Application().Name != "yet" {
|
||
|
|
t.Errorf("Application().Name = %q, want yet", r.Application().Name)
|
||
|
|
}
|
||
|
|
if r.Filename() != "custom.yaml" {
|
||
|
|
t.Errorf("Filename() = %q, want custom.yaml", r.Filename())
|
||
|
|
}
|
||
|
|
|
||
|
|
r2, err := For(yetApp())
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("For: %v", err)
|
||
|
|
}
|
||
|
|
if r2.Filename() != "yet.yaml" {
|
||
|
|
t.Errorf("For(...).Filename() = %q, want yet.yaml", r2.Filename())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWithEnvironmentProviderAndExpansionToggle(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
writeFile(t, filepath.Join(root, "etc", "yet", "yet.yaml"), "path: ${HOME}/data\n")
|
||
|
|
|
||
|
|
env := paths.MapEnvironment{"HOME": "/home/test"}
|
||
|
|
r := mustResolver(t, yetApp(),
|
||
|
|
WithEnvironmentProvider(env),
|
||
|
|
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||
|
|
WithWorkingDirectory(false),
|
||
|
|
)
|
||
|
|
var target struct {
|
||
|
|
Path string `yaml:"path"`
|
||
|
|
}
|
||
|
|
if err := r.LoadMerged(&target); err != nil {
|
||
|
|
t.Fatalf("LoadMerged: %v", err)
|
||
|
|
}
|
||
|
|
if want := "/home/test/data"; target.Path != want {
|
||
|
|
t.Errorf("Path = %q, want %q", target.Path, want)
|
||
|
|
}
|
||
|
|
|
||
|
|
r2 := mustResolver(t, yetApp(),
|
||
|
|
WithEnvironmentProvider(env),
|
||
|
|
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||
|
|
WithWorkingDirectory(false),
|
||
|
|
WithEnvExpansion(false),
|
||
|
|
)
|
||
|
|
var target2 struct {
|
||
|
|
Path string `yaml:"path"`
|
||
|
|
}
|
||
|
|
if err := r2.LoadMerged(&target2); err != nil {
|
||
|
|
t.Fatalf("LoadMerged: %v", err)
|
||
|
|
}
|
||
|
|
if want := "${HOME}/data"; target2.Path != want {
|
||
|
|
t.Errorf("Path = %q, want %q (expansion disabled)", target2.Path, want)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReadLayerMissingFileIsNotAnError(t *testing.T) {
|
||
|
|
m, err := readLayer("/does/not/exist/yet.yaml")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("readLayer() error = %v, want nil", err)
|
||
|
|
}
|
||
|
|
if len(m) != 0 {
|
||
|
|
t.Errorf("readLayer() = %v, want empty map", m)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReadLayerInvalidYAML(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
path := filepath.Join(root, "bad.yaml")
|
||
|
|
writeFile(t, path, "not: valid: yaml: [")
|
||
|
|
if _, err := readLayer(path); err == nil {
|
||
|
|
t.Fatal("readLayer() error = nil, want a parse error")
|
||
|
|
}
|
||
|
|
}
|