Skip to content

Commit 14a86c9

Browse files
committed
Cache MkdirAll for basline outputs
1 parent 073008a commit 14a86c9

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

internal/testutil/baseline/baseline.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,25 @@ func RunAgainstSubmodule(t *testing.T, fileName string, actual string, opts Opti
146146
writeComparison(t, actual, local, reference, true)
147147
}
148148

149+
var mkdirAllCache sync.Map
150+
151+
func mkdirAllCached(t *testing.T, path string) error {
152+
cached, ok := mkdirAllCache.Load(path)
153+
if ok {
154+
return cached.(error)
155+
}
156+
157+
err := os.MkdirAll(path, 0o755)
158+
cached, _ = mkdirAllCache.LoadOrStore(path, err)
159+
return cached.(error)
160+
}
161+
149162
func writeComparison(t *testing.T, actualContent string, local, reference string, comparingAgainstSubmodule bool) {
150163
if actualContent == "" {
151164
panic("the generated content was \"\". Return 'baseline.NoContent' if no baselining is required.")
152165
}
153166

154-
if err := os.MkdirAll(filepath.Dir(local), 0o755); err != nil {
167+
if err := mkdirAllCached(t, filepath.Dir(local)); err != nil {
155168
t.Error(fmt.Errorf("failed to create directories for the local baseline file %s: %w", local, err))
156169
return
157170
}

0 commit comments

Comments
 (0)