Cache source directories during configuration normalization

This commit is contained in:
2026-11-18 00:36:26 +00:00
parent 1c93aa0947
commit 76364a7b7e
4 changed files with 81 additions and 10 deletions

View File

@@ -8,6 +8,44 @@ import (
configkit "go.maximumdirect.net/configkit"
)
func TestSourceDirectoryLookupIsMemoizedPerLoad(t *testing.T) {
dir := t.TempDir()
state := newDocumentLoadState(dir)
calls := make(map[string]int)
lookup := func(id string) (configkit.SourceInfo, bool) {
calls[id]++
if id == "missing" {
return configkit.SourceInfo{}, false
}
if id == "memory" {
return configkit.SourceInfo{}, true
}
return configkit.SourceInfo{Path: filepath.Join(dir, id, "config.yml")}, true
}
for range 2 {
for _, id := range []string{"first", "second", "missing", "memory"} {
want := ""
if id == "first" || id == "second" {
want = filepath.Join(dir, id)
}
if got := state.sourceDirectory(id, lookup); got != want {
t.Fatalf("directory(%s) = %q, want %q", id, got, want)
}
}
}
for id, count := range calls {
if count != 1 {
t.Fatalf("source %s queried %d times", id, count)
}
}
other := newDocumentLoadState(dir)
if got := other.sourceDirectory("first", func(string) (configkit.SourceInfo, bool) {
return configkit.SourceInfo{Path: filepath.Join(dir, "changed", "config.yml")}, true
}); got != filepath.Join(dir, "changed") {
t.Fatalf("another load reused directory %q", got)
}
}
func TestDocumentPathsUseDeclaringFilesAcrossImports(t *testing.T) {
t.Parallel()