diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-12 20:14:47 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-12 20:14:47 +0200 |
| commit | 3f8679be9373ee7508d512dfdfc1dda0839c7f90 (patch) | |
| tree | ec02eb075f6c4e90f21baa2fe674e86a2f7f6a62 /internal/apply/fs_test.go | |
| parent | 24a84671ace373ae331fa83a1ff484990f4dff0e (diff) | |
| download | krino-3f8679be9373ee7508d512dfdfc1dda0839c7f90.tar.gz krino-3f8679be9373ee7508d512dfdfc1dda0839c7f90.zip | |
krino: acting — trash, journal, apply, lock, review, undo
Diffstat (limited to 'internal/apply/fs_test.go')
| -rw-r--r-- | internal/apply/fs_test.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/internal/apply/fs_test.go b/internal/apply/fs_test.go new file mode 100644 index 0000000..87c5824 --- /dev/null +++ b/internal/apply/fs_test.go @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package apply + +import ( + "os" + "path/filepath" + "testing" + "time" +) + +func write(t *testing.T, path, content string, mode os.FileMode) string { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(content), mode); err != nil { + t.Fatal(err) + } + return path +} + +func TestCopyPreservesModeAndModTime(t *testing.T) { + dir := t.TempDir() + src := write(t, filepath.Join(dir, "a", "x.pdf"), "content", 0o640) + old := time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC) + if err := os.Chtimes(src, old, old); err != nil { + t.Fatal(err) + } + dst := filepath.Join(dir, "b", "x.pdf") + + if err := copyFile(src, dst); err != nil { + t.Fatal(err) + } + fi, err := os.Stat(dst) + if err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(dst); string(b) != "content" { + t.Errorf("content = %q", b) + } + if fi.Mode().Perm() != 0o640 { + t.Errorf("mode = %v, want 0640", fi.Mode().Perm()) + } + if !fi.ModTime().Equal(old) { + t.Errorf("mtime = %v, want %v", fi.ModTime(), old) + } + if si, _ := os.Stat(src); si == nil { + t.Error("copy removed its source") + } +} + +func TestCopyLeavesNoTempOnFailure(t *testing.T) { + dir := t.TempDir() + src := write(t, filepath.Join(dir, "x.pdf"), "content", 0o644) + // A destination directory that is really a file: the rename must fail. + blocked := write(t, filepath.Join(dir, "blocked"), "not a directory", 0o644) + if err := copyFile(src, filepath.Join(blocked, "x.pdf")); err == nil { + t.Fatal("copy into a non-directory succeeded") + } + entries, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + for _, e := range entries { + if len(e.Name()) > 6 && e.Name()[:7] == ".krino-" { + t.Errorf("a temporary file was left behind: %s", e.Name()) + } + } +} + +// TestMoveAcrossFilesystems exercises the EXDEV fallback. /dev/shm is a +// second filesystem on Linux; the test skips where there is none. +func TestMoveAcrossFilesystems(t *testing.T) { + other, err := os.MkdirTemp("/dev/shm", "krino-apply-") + if err != nil { + t.Skip("no second filesystem available:", err) + } + defer os.RemoveAll(other) + src := write(t, filepath.Join(other, "x.pdf"), "across", 0o644) + dst := filepath.Join(t.TempDir(), "x.pdf") + + if err := moveFile(src, dst); err != nil { + t.Fatal(err) + } + if b, err := os.ReadFile(dst); err != nil || string(b) != "across" { + t.Errorf("destination = %q, %v", b, err) + } + if _, err := os.Stat(src); !os.IsNotExist(err) { + t.Error("the source survived a cross-filesystem move") + } +} |
