chultun/internal/budget/files_test.go

48 lines
1.3 KiB
Go
Raw Normal View History

package budget
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestWriteFiles(t *testing.T) {
p := homePaths(t.TempDir())
old := "old.scope.d/50-budget.conf"
put(t, filepath.Join(p.units, old), "old")
put(t, filepath.Join(p.units, "user.conf"), "keep")
put(t, filepath.Join(p.state, "files.json"), `["old.scope.d/50-budget.conf"]`)
files := map[string]string{"app-.scope.d/50-budget.conf": "[Scope]\nMemoryMax=8G\n", "search.slice": "[Slice]\n"}
if err := writeFiles(p, files); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(p.units, old)); !os.IsNotExist(err) {
t.Fatalf("stale owned file remains: %v", err)
}
data, err := os.ReadFile(filepath.Join(p.units, "user.conf"))
if err != nil || string(data) != "keep" {
t.Fatal("unmanaged configuration changed")
}
data, err = os.ReadFile(filepath.Join(p.state, "files.json"))
if err != nil {
t.Fatal(err)
}
var manifest []string
if err := json.Unmarshal(data, &manifest); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(manifest, keys(files)) {
t.Fatal(manifest)
}
for name, want := range files {
data, err := os.ReadFile(filepath.Join(p.units, name))
if err != nil || string(data) != want {
t.Fatalf("%s: %q, %v", name, data, err)
}
}
if err := writeFiles(p, files); err != nil {
t.Fatal("second apply failed:", err)
}
}