Fifteen call sites each had their own temp-file-and-rename code. Some used a fixed temporary name, so two writers could mix their content, and most did not flush before the rename. They now share atomicfile.Write. An architecture test keeps new os.Rename calls out of other packages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package atomicfile
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func entries(t *testing.T, dir string) []string {
|
|
t.Helper()
|
|
list, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var names []string
|
|
for _, e := range list {
|
|
names = append(names, e.Name())
|
|
}
|
|
return names
|
|
}
|
|
|
|
func TestWriteReplacesAndLeavesNoTemporaryFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "state.json")
|
|
for _, want := range []string{"first, longer content", "second"} {
|
|
if err := Write(path, []byte(want), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := os.ReadFile(path); err != nil || string(got) != want {
|
|
t.Fatalf("content = %q, %v; want %q", got, err, want)
|
|
}
|
|
}
|
|
if got := entries(t, dir); len(got) != 1 {
|
|
t.Fatalf("directory holds %v", got)
|
|
}
|
|
}
|
|
|
|
func TestWriteFailureKeepsTargetAndCleansUp(t *testing.T) {
|
|
dir := t.TempDir()
|
|
target := filepath.Join(dir, "taken")
|
|
if err := os.MkdirAll(filepath.Join(target, "child"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Write(target, []byte("x"), 0o600); err == nil {
|
|
t.Fatal("replacing a directory succeeded")
|
|
}
|
|
if got := entries(t, dir); len(got) != 1 {
|
|
t.Fatalf("directory holds %v", got)
|
|
}
|
|
if err := Write(filepath.Join(dir, "missing", "f"), nil, 0o600); err == nil {
|
|
t.Fatal("writing into a missing directory succeeded")
|
|
}
|
|
}
|
|
|
|
func TestConcurrentWritersNeverMixContent(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "state")
|
|
var wg sync.WaitGroup
|
|
for i := range 8 {
|
|
wg.Go(func() {
|
|
// Windows may refuse a rename that races with another one; the file
|
|
// must still hold one writer's whole content.
|
|
if err := Write(path, []byte(strings.Repeat(fmt.Sprint(i), 4096)), 0o600); err != nil {
|
|
t.Logf("writer %d: %v", i, err)
|
|
}
|
|
})
|
|
}
|
|
wg.Wait()
|
|
got, err := os.ReadFile(path)
|
|
if err != nil || len(got) != 4096 || strings.Trim(string(got), string(got[0])) != "" {
|
|
t.Fatalf("mixed content of %d bytes, %v", len(got), err)
|
|
}
|
|
if left := entries(t, dir); len(left) != 1 {
|
|
t.Fatalf("directory holds %v", left)
|
|
}
|
|
}
|
|
|
|
func TestWriteJSONIndentsAndEndsWithNewline(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "v.json")
|
|
if err := WriteJSON(path, map[string]int{"a": 1}, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(path); string(got) != "{\n \"a\": 1\n}\n" {
|
|
t.Fatalf("content = %q", got)
|
|
}
|
|
if err := WriteJSON(path, func() {}, 0o644); err == nil {
|
|
t.Fatal("encoded a function")
|
|
}
|
|
}
|