github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/filesystem/atomic_test.go (about) 1 package filesystem 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func TestWriteFileAtomicNonExistentDirectory(t *testing.T) { 11 if WriteFileAtomic("/does/not/exist", []byte{}, 0600) == nil { 12 t.Error("atomic file write did not fail for non-existent path") 13 } 14 } 15 16 func TestWriteFileAtomic(t *testing.T) { 17 // Compute the target path. 18 target := filepath.Join(t.TempDir(), "file") 19 20 // Create contents. 21 contents := []byte{0, 1, 2, 3, 4, 5, 6} 22 23 // Attempt to write to a temporary file. 24 if err := WriteFileAtomic(target, contents, 0600); err != nil { 25 t.Fatal("atomic file write failed:", err) 26 } 27 28 // Read the contents back and ensure they match what's expected. 29 if data, err := os.ReadFile(target); err != nil { 30 t.Fatal("unable to read back file:", err) 31 } else if !bytes.Equal(data, contents) { 32 t.Error("file contents did not match expected") 33 } 34 }