49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
// Package testutil provides isolated runtime fixtures for tests that write saves.
|
|||
|
|
package testutil
|
||
|
|
|
||
|
|
import (
|
||
|
|
"io/fs"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// CopyRuntime copies data, never links writable saves to the source checkout.
|
||
|
|
// Local operator configuration and binaries are intentionally excluded.
|
||
|
|
func CopyRuntime(root, destination string) error {
|
||
|
|
entries, err := os.ReadDir(root)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
for _, entry := range entries {
|
||
|
|
name := entry.Name()
|
||
|
|
if entry.IsDir() || strings.HasSuffix(name, ".local.json") || !(strings.HasSuffix(name, ".json") || name == "persistent" || name == "go.mod") {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(filepath.Join(root, name))
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if err = os.WriteFile(filepath.Join(destination, name), data, 0600); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return filepath.WalkDir(filepath.Join(root, "resources"), func(path string, d fs.DirEntry, err error) error {
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
relative, err := filepath.Rel(root, path)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
target := filepath.Join(destination, relative)
|
||
|
|
if d.IsDir() {
|
||
|
|
return os.MkdirAll(target, 0700)
|
||
|
|
}
|
||
|
|
data, err := os.ReadFile(path)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return os.WriteFile(target, data, 0600)
|
||
|
|
})
|
||
|
|
}
|