1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
package atomicwriter
import (
"bytes"
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
)
// testMode returns the file-mode to use in tests, accounting for Windows
// not supporting full Linux file mode.
func testMode() os.FileMode {
if runtime.GOOS == "windows" {
return 0o666
}
return 0o640
}
// assertFile asserts the given fileName to exist, and to have the expected
// content and mode.
func assertFile(t *testing.T, fileName string, fileContent []byte, expectedMode os.FileMode) {
t.Helper()
actual, err := os.ReadFile(fileName)
if err != nil {
t.Fatalf("Error reading from file: %v", err)
}
if !bytes.Equal(actual, fileContent) {
t.Errorf("Data mismatch, expected %q, got %q", fileContent, actual)
}
st, err := os.Stat(fileName)
if err != nil {
t.Fatalf("Error statting file: %v", err)
}
if st.Mode() != expectedMode {
t.Errorf("Mode mismatched, expected %o, got %o", expectedMode, st.Mode())
}
}
// assertFileCount asserts the given directory has the expected number
// of files, and returns the list of files found.
func assertFileCount(t *testing.T, directory string, expected int) []os.DirEntry {
t.Helper()
files, err := os.ReadDir(directory)
if err != nil {
t.Fatalf("Error reading dir: %v", err)
}
if len(files) != expected {
t.Errorf("Expected %d files, got %d: %v", expected, len(files), files)
}
return files
}
func TestNew(t *testing.T) {
for _, tc := range []string{"normal", "symlinked"} {
tmpDir := t.TempDir()
parentDir := tmpDir
actualParentDir := parentDir
if tc == "symlinked" {
actualParentDir = filepath.Join(tmpDir, "parent-dir")
if err := os.Mkdir(actualParentDir, 0o700); err != nil {
t.Fatal(err)
}
parentDir = filepath.Join(tmpDir, "parent-dir-symlink")
if err := os.Symlink(actualParentDir, parentDir); err != nil {
t.Fatal(err)
}
}
t.Run(tc, func(t *testing.T) {
for _, tc := range []string{"new-file", "existing-file"} {
t.Run(tc, func(t *testing.T) {
fileName := filepath.Join(parentDir, "test.txt")
var origFileCount int
if tc == "existing-file" {
if err := os.WriteFile(fileName, []byte("original content"), testMode()); err != nil {
t.Fatalf("Error writing file: %v", err)
}
origFileCount = 1
}
writer, err := New(fileName, testMode())
if writer == nil {
t.Errorf("Writer is nil")
}
if err != nil {
t.Fatalf("Error creating new atomicwriter: %v", err)
}
files := assertFileCount(t, actualParentDir, origFileCount+1)
if tmpFileName := files[0].Name(); !strings.HasPrefix(tmpFileName, ".tmp-test.txt") {
t.Errorf("Unexpected file name for temp-file: %s", tmpFileName)
}
// Closing the writer without writing should clean up the temp-file,
// and should not replace the destination file.
if err = writer.Close(); err != nil {
t.Errorf("Error closing writer: %v", err)
}
assertFileCount(t, actualParentDir, origFileCount)
if tc == "existing-file" {
assertFile(t, fileName, []byte("original content"), testMode())
}
})
}
})
}
}
func TestNewInvalid(t *testing.T) {
t.Run("missing target dir", func(t *testing.T) {
tmpDir := t.TempDir()
fileName := filepath.Join(tmpDir, "missing-dir", "test.txt")
writer, err := New(fileName, testMode())
if writer != nil {
t.Errorf("Should not have created writer")
}
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("Should produce a 'not found' error, but got %[1]T (%[1]v)", err)
}
})
t.Run("target dir is not a directory", func(t *testing.T) {
tmpDir := t.TempDir()
parentPath := filepath.Join(tmpDir, "not-a-dir")
err := os.WriteFile(parentPath, nil, testMode())
if err != nil {
t.Fatalf("Error writing file: %v", err)
}
fileName := filepath.Join(parentPath, "new-file.txt")
writer, err := New(fileName, testMode())
if writer != nil {
t.Errorf("Should not have created writer")
}
// This should match the behavior of os.WriteFile, which returns a [os.PathError] with [syscall.ENOTDIR].
if !errors.Is(err, syscall.ENOTDIR) {
t.Errorf("Should produce a 'not a directory' error, but got %[1]T (%[1]v)", err)
}
})
t.Run("empty filename", func(t *testing.T) {
writer, err := New("", testMode())
if writer != nil {
t.Errorf("Should not have created writer")
}
if err == nil || err.Error() != "file name is empty" {
t.Errorf("Should produce a 'file name is empty' error, but got %[1]T (%[1]v)", err)
}
})
t.Run("directory", func(t *testing.T) {
tmpDir := t.TempDir()
writer, err := New(tmpDir, testMode())
if writer != nil {
t.Errorf("Should not have created writer")
}
if err == nil || err.Error() != "cannot write to a directory" {
t.Errorf("Should produce a 'cannot write to a directory' error, but got %[1]T (%[1]v)", err)
}
})
t.Run("symlinked file", func(t *testing.T) {
tmpDir := t.TempDir()
linkTarget := filepath.Join(tmpDir, "symlink-target")
if err := os.WriteFile(linkTarget, []byte("orig content"), testMode()); err != nil {
t.Fatal(err)
}
fileName := filepath.Join(tmpDir, "symlinked-file")
if err := os.Symlink(linkTarget, fileName); err != nil {
t.Fatal(err)
}
writer, err := New(fileName, testMode())
if writer != nil {
t.Errorf("Should not have created writer")
}
if err == nil || err.Error() != "cannot write to a symbolic link directly" {
t.Errorf("Should produce a 'cannot write to a symbolic link directly' error, but got %[1]T (%[1]v)", err)
}
})
}
func TestWriteFile(t *testing.T) {
t.Run("empty filename", func(t *testing.T) {
err := WriteFile("", nil, testMode())
if err == nil || err.Error() != "file name is empty" {
t.Errorf("Should produce a 'file name is empty' error, but got %[1]T (%[1]v)", err)
}
})
t.Run("write to directory", func(t *testing.T) {
err := WriteFile(t.TempDir(), nil, testMode())
if err == nil || err.Error() != "cannot write to a directory" {
t.Errorf("Should produce a 'cannot write to a directory' error, but got %[1]T (%[1]v)", err)
}
})
t.Run("write to file", func(t *testing.T) {
tmpDir := t.TempDir()
fileName := filepath.Join(tmpDir, "test.txt")
fileContent := []byte("file content")
fileMode := testMode()
if err := WriteFile(fileName, fileContent, fileMode); err != nil {
t.Fatalf("Error writing to file: %v", err)
}
assertFile(t, fileName, fileContent, fileMode)
assertFileCount(t, tmpDir, 1)
})
t.Run("missing parent directory", func(t *testing.T) {
tmpDir := t.TempDir()
fileName := filepath.Join(tmpDir, "missing-dir", "test.txt")
fileContent := []byte("file content")
fileMode := testMode()
if err := WriteFile(fileName, fileContent, fileMode); !errors.Is(err, os.ErrNotExist) {
t.Errorf("Should produce a 'not found' error, but got %[1]T (%[1]v)", err)
}
assertFileCount(t, tmpDir, 0)
})
t.Run("symlinked file", func(t *testing.T) {
tmpDir := t.TempDir()
linkTarget := filepath.Join(tmpDir, "symlink-target")
originalContent := []byte("original content")
fileMode := testMode()
if err := os.WriteFile(linkTarget, originalContent, fileMode); err != nil {
t.Fatal(err)
}
if err := os.Symlink(linkTarget, filepath.Join(tmpDir, "symlinked-file")); err != nil {
t.Fatal(err)
}
origFileCount := 2
assertFileCount(t, tmpDir, origFileCount)
fileName := filepath.Join(tmpDir, "symlinked-file")
err := WriteFile(fileName, []byte("new content"), testMode())
if err == nil || err.Error() != "cannot write to a symbolic link directly" {
t.Errorf("Should produce a 'cannot write to a symbolic link directly' error, but got %[1]T (%[1]v)", err)
}
assertFile(t, linkTarget, originalContent, fileMode)
assertFileCount(t, tmpDir, origFileCount)
})
t.Run("symlinked directory", func(t *testing.T) {
tmpDir := t.TempDir()
actualParentDir := filepath.Join(tmpDir, "parent-dir")
if err := os.Mkdir(actualParentDir, 0o700); err != nil {
t.Fatal(err)
}
actualTargetFile := filepath.Join(actualParentDir, "target-file")
if err := os.WriteFile(actualTargetFile, []byte("orig content"), testMode()); err != nil {
t.Fatal(err)
}
parentDir := filepath.Join(tmpDir, "parent-dir-symlink")
if err := os.Symlink(actualParentDir, parentDir); err != nil {
t.Fatal(err)
}
origFileCount := 1
assertFileCount(t, actualParentDir, origFileCount)
fileName := filepath.Join(parentDir, "target-file")
fileContent := []byte("new content")
fileMode := testMode()
if err := WriteFile(fileName, fileContent, fileMode); err != nil {
t.Fatalf("Error writing to file: %v", err)
}
assertFile(t, fileName, fileContent, fileMode)
assertFile(t, actualTargetFile, fileContent, fileMode)
assertFileCount(t, actualParentDir, origFileCount)
})
}
func TestWriteSetCommit(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0o700); err != nil {
t.Fatalf("Error creating tmp directory: %s", err)
}
targetDir := filepath.Join(tmpDir, "target")
ws, err := NewWriteSet(filepath.Join(tmpDir, "tmp"))
if err != nil {
t.Fatalf("Error creating atomic write set: %s", err)
}
fileContent := []byte("file content")
fileMode := testMode()
if err := ws.WriteFile("foo", fileContent, fileMode); err != nil {
t.Fatalf("Error writing to file: %v", err)
}
if _, err := os.ReadFile(filepath.Join(targetDir, "foo")); err == nil {
t.Fatalf("Expected error reading file where should not exist")
}
if err := ws.Commit(targetDir); err != nil {
t.Fatalf("Error committing file: %s", err)
}
assertFile(t, filepath.Join(targetDir, "foo"), fileContent, fileMode)
assertFileCount(t, targetDir, 1)
}
func TestWriteSetCancel(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0o700); err != nil {
t.Fatalf("Error creating tmp directory: %s", err)
}
ws, err := NewWriteSet(filepath.Join(tmpDir, "tmp"))
if err != nil {
t.Fatalf("Error creating atomic write set: %s", err)
}
fileContent := []byte("file content")
fileMode := testMode()
if err := ws.WriteFile("foo", fileContent, fileMode); err != nil {
t.Fatalf("Error writing to file: %v", err)
}
if err := ws.Cancel(); err != nil {
t.Fatalf("Error committing file: %s", err)
}
if _, err := os.ReadFile(filepath.Join(tmpDir, "target", "foo")); err == nil {
t.Fatalf("Expected error reading file where should not exist")
} else if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Unexpected error reading file: %s", err)
}
assertFileCount(t, filepath.Join(tmpDir, "tmp"), 0)
}
|