-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathreflink_test.go
More file actions
168 lines (140 loc) · 4.1 KB
/
reflink_test.go
File metadata and controls
168 lines (140 loc) · 4.1 KB
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
// Copyright (c) 2025-2026, s0up and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package reflinktree
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/autobrr/qui/pkg/hardlinktree"
)
func TestSupportsReflink(t *testing.T) {
tmpDir := t.TempDir()
supported, reason := SupportsReflink(tmpDir)
// On unsupported platforms, should return false.
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" && runtime.GOOS != "windows" {
if supported {
t.Errorf("SupportsReflink should return false on %s", runtime.GOOS)
}
if reason != "reflink is not supported on this operating system" {
t.Errorf("unexpected reason on unsupported OS: %s", reason)
}
return
}
// On supported platforms, the result depends on filesystem.
// We just verify the function doesn't panic and returns sensible values.
t.Logf("SupportsReflink(%s): supported=%v, reason=%s", tmpDir, supported, reason)
if !supported {
t.Skipf("reflink not supported on this filesystem: %s", reason)
}
}
func TestCreate_NilPlan(t *testing.T) {
err := Create(nil)
if err == nil {
t.Error("expected error for nil plan")
}
}
func TestCreate_EmptyRootDir(t *testing.T) {
plan := &hardlinktree.TreePlan{
RootDir: "",
Files: []hardlinktree.FilePlan{{SourcePath: "/src", TargetPath: "/dst"}},
}
err := Create(plan)
if err == nil {
t.Error("expected error for empty root dir")
}
}
func TestCreate_EmptyFiles(t *testing.T) {
plan := &hardlinktree.TreePlan{
RootDir: "/tmp",
Files: []hardlinktree.FilePlan{},
}
err := Create(plan)
if err == nil {
t.Error("expected error for empty files")
}
}
func TestCreate_Success(t *testing.T) {
tmpDir := t.TempDir()
// Check if reflink is supported
supported, reason := SupportsReflink(tmpDir)
if !supported {
t.Skipf("reflink not supported: %s", reason)
}
// Create source file
srcDir := filepath.Join(tmpDir, "src")
if err := os.MkdirAll(srcDir, 0o755); err != nil {
t.Fatalf("failed to create src dir: %v", err)
}
srcFile := filepath.Join(srcDir, "test.txt")
testContent := "test content for reflink"
if err := os.WriteFile(srcFile, []byte(testContent), 0o644); err != nil {
t.Fatalf("failed to write source file: %v", err)
}
// Create reflink tree
dstDir := filepath.Join(tmpDir, "dst")
plan := &hardlinktree.TreePlan{
RootDir: dstDir,
Files: []hardlinktree.FilePlan{
{
SourcePath: srcFile,
TargetPath: filepath.Join(dstDir, "test.txt"),
},
},
}
err := Create(plan)
if err != nil {
t.Fatalf("Create failed: %v", err)
}
// Verify file exists and has correct content
dstContent, err := os.ReadFile(filepath.Join(dstDir, "test.txt"))
if err != nil {
t.Fatalf("failed to read destination file: %v", err)
}
if string(dstContent) != testContent {
t.Errorf("content mismatch: got %q, want %q", string(dstContent), testContent)
}
}
func TestRollback(t *testing.T) {
tmpDir := t.TempDir()
// Check if reflink is supported
supported, reason := SupportsReflink(tmpDir)
if !supported {
t.Skipf("reflink not supported: %s", reason)
}
// Create source file
srcDir := filepath.Join(tmpDir, "src")
if err := os.MkdirAll(srcDir, 0o755); err != nil {
t.Fatalf("failed to create src dir: %v", err)
}
srcFile := filepath.Join(srcDir, "test.txt")
if err := os.WriteFile(srcFile, []byte("test"), 0o644); err != nil {
t.Fatalf("failed to write source file: %v", err)
}
// Create reflink tree
dstDir := filepath.Join(tmpDir, "dst")
plan := &hardlinktree.TreePlan{
RootDir: dstDir,
Files: []hardlinktree.FilePlan{
{
SourcePath: srcFile,
TargetPath: filepath.Join(dstDir, "subdir", "test.txt"),
},
},
}
if err := Create(plan); err != nil {
t.Fatalf("Create failed: %v", err)
}
// Verify files exist
if _, err := os.Stat(filepath.Join(dstDir, "subdir", "test.txt")); err != nil {
t.Fatalf("file should exist before rollback: %v", err)
}
// Rollback
if err := Rollback(plan); err != nil {
t.Fatalf("Rollback failed: %v", err)
}
// Verify file was removed
if _, err := os.Stat(filepath.Join(dstDir, "subdir", "test.txt")); !os.IsNotExist(err) {
t.Error("file should not exist after rollback")
}
}