|
| 1 | +package fsatomic |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestWriteFile_WritesContentAndPerm(t *testing.T) { |
| 11 | + dir := t.TempDir() |
| 12 | + path := filepath.Join(dir, "data.json") |
| 13 | + |
| 14 | + if err := WriteFile(path, []byte("hello"), 0600); err != nil { |
| 15 | + t.Fatalf("WriteFile: %v", err) |
| 16 | + } |
| 17 | + got, err := os.ReadFile(path) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("ReadFile: %v", err) |
| 20 | + } |
| 21 | + if string(got) != "hello" { |
| 22 | + t.Errorf("content = %q, want %q", got, "hello") |
| 23 | + } |
| 24 | + info, err := os.Stat(path) |
| 25 | + if err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + if info.Mode().Perm() != 0600 { |
| 29 | + t.Errorf("perm = %v, want 0600", info.Mode().Perm()) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestWriteFile_Overwrites(t *testing.T) { |
| 34 | + dir := t.TempDir() |
| 35 | + path := filepath.Join(dir, "data") |
| 36 | + if err := WriteFile(path, []byte("first"), 0644); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + if err := WriteFile(path, []byte("second"), 0644); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + got, _ := os.ReadFile(path) |
| 43 | + if string(got) != "second" { |
| 44 | + t.Errorf("content = %q, want %q", got, "second") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// TestWriteFile_LeavesNoTempOnSuccess verifies the unique temp file is renamed |
| 49 | +// away (no litter), which also confirms the temp naming doesn't collide with |
| 50 | +// the target. |
| 51 | +func TestWriteFile_LeavesNoTempOnSuccess(t *testing.T) { |
| 52 | + dir := t.TempDir() |
| 53 | + path := filepath.Join(dir, "data") |
| 54 | + if err := WriteFile(path, []byte("x"), 0644); err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + entries, _ := os.ReadDir(dir) |
| 58 | + if len(entries) != 1 || entries[0].Name() != "data" { |
| 59 | + var names []string |
| 60 | + for _, e := range entries { |
| 61 | + names = append(names, e.Name()) |
| 62 | + } |
| 63 | + t.Errorf("expected only [data], got %v", names) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// TestWriteFile_ConcurrentSameTarget verifies concurrent writers to the same |
| 68 | +// path don't clobber each other's temp file (the old fixed-".tmp" pattern |
| 69 | +// could) — the final content must be a complete one of the writes, never torn. |
| 70 | +func TestWriteFile_ConcurrentSameTarget(t *testing.T) { |
| 71 | + dir := t.TempDir() |
| 72 | + path := filepath.Join(dir, "data") |
| 73 | + payloads := []string{ |
| 74 | + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 75 | + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 76 | + "cccccccccccccccccccccccccccccc", |
| 77 | + } |
| 78 | + var wg sync.WaitGroup |
| 79 | + for _, p := range payloads { |
| 80 | + wg.Add(1) |
| 81 | + go func(data string) { |
| 82 | + defer wg.Done() |
| 83 | + for i := 0; i < 20; i++ { |
| 84 | + if err := WriteFile(path, []byte(data), 0644); err != nil { |
| 85 | + t.Errorf("WriteFile: %v", err) |
| 86 | + return |
| 87 | + } |
| 88 | + } |
| 89 | + }(p) |
| 90 | + } |
| 91 | + wg.Wait() |
| 92 | + |
| 93 | + got, err := os.ReadFile(path) |
| 94 | + if err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + ok := false |
| 98 | + for _, p := range payloads { |
| 99 | + if string(got) == p { |
| 100 | + ok = true |
| 101 | + } |
| 102 | + } |
| 103 | + if !ok { |
| 104 | + t.Errorf("final content %q is torn — not a complete write", got) |
| 105 | + } |
| 106 | + // No temp files left behind. |
| 107 | + entries, _ := os.ReadDir(dir) |
| 108 | + if len(entries) != 1 { |
| 109 | + t.Errorf("expected only the target file, got %d entries", len(entries)) |
| 110 | + } |
| 111 | +} |
0 commit comments