Files
core/config/merge_test.go
T
terminar 13801bec11 Add platform-independent path/config resolution library
Implements core/paths (Config/Data/State/Cache/Runtime/Log/Bin
directory resolution with canonical-vs-search-dir separation, XDG on
Unix, native APPDATA/LOCALAPPDATA/PROGRAMDATA layering on Windows,
~/Library on macOS Desktop, injectable environment/platform for
deterministic cross-platform tests) and core/config (file search,
first-match resolution, layered YAML merging, safe ${VAR} expansion,
Explain/doctor introspection) on top of the shared Application/
Kind/Scope model in the core package.
2026-09-19 16:26:01 +02:00

98 lines
2.9 KiB
Go

package config
import (
"path/filepath"
"testing"
"yetano.dev/yetano/core/paths"
)
func TestMergeScalarOverride(t *testing.T) {
base := map[string]any{"port": 8080}
over := map[string]any{"port": 9090}
got := mergeMaps(base, over)
if got["port"] != 9090 {
t.Errorf("port = %v, want 9090", got["port"])
}
}
func TestMergeNestedMap(t *testing.T) {
base := map[string]any{
"http": map[string]any{"host": "0.0.0.0", "port": 8080, "tls": false},
}
over := map[string]any{
"http": map[string]any{"port": 9090},
}
got := mergeMaps(base, over)
httpMap := got["http"].(map[string]any)
if httpMap["host"] != "0.0.0.0" {
t.Errorf("host = %v, want 0.0.0.0 (should survive from base)", httpMap["host"])
}
if httpMap["port"] != 9090 {
t.Errorf("port = %v, want 9090", httpMap["port"])
}
if httpMap["tls"] != false {
t.Errorf("tls = %v, want false (should survive from base)", httpMap["tls"])
}
}
func TestMergeListReplacesWhollyNoConcatenation(t *testing.T) {
base := map[string]any{"registries": []any{"a", "b"}}
over := map[string]any{"registries": []any{"c"}}
got := mergeMaps(base, over)
list := got["registries"].([]any)
if len(list) != 1 || list[0] != "c" {
t.Errorf("registries = %v, want [c] (full replace, no concatenation)", list)
}
}
func TestMergeNullRemovesKey(t *testing.T) {
base := map[string]any{"ca_cert": "/etc/ssl/ca.pem", "port": 8080}
over := map[string]any{"ca_cert": nil}
got := mergeMaps(base, over)
if _, exists := got["ca_cert"]; exists {
t.Errorf("ca_cert = %v, want key removed entirely", got["ca_cert"])
}
if got["port"] != 8080 {
t.Errorf("port = %v, want 8080 (untouched)", got["port"])
}
}
func TestLoadMergedEndToEnd(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "etc", "yet", "yet.yaml"), "http:\n host: 0.0.0.0\n port: 8080\n tls: false\nregistries:\n - a\n - b\n")
writeFile(t, filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"), "http:\n port: 9090\nregistries:\n - c\n")
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkingDirectory(false),
)
type httpConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
TLS bool `yaml:"tls"`
}
var target struct {
HTTP httpConfig `yaml:"http"`
Registries []string `yaml:"registries"`
}
if err := r.LoadMerged(&target); err != nil {
t.Fatalf("LoadMerged: %v", err)
}
if target.HTTP.Host != "0.0.0.0" {
t.Errorf("HTTP.Host = %q, want 0.0.0.0", target.HTTP.Host)
}
if target.HTTP.Port != 9090 {
t.Errorf("HTTP.Port = %d, want 9090", target.HTTP.Port)
}
if target.HTTP.TLS != false {
t.Errorf("HTTP.TLS = %v, want false", target.HTTP.TLS)
}
if len(target.Registries) != 1 || target.Registries[0] != "c" {
t.Errorf("Registries = %v, want [c]", target.Registries)
}
}