-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_test.go
More file actions
384 lines (357 loc) · 8.37 KB
/
path_test.go
File metadata and controls
384 lines (357 loc) · 8.37 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package goverhaul
import (
"path/filepath"
"runtime"
"testing"
"github.com/spf13/afero"
)
func TestNormalizePath(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Unix path",
input: "/usr/local/bin",
expected: "/usr/local/bin",
},
{
name: "Windows path",
input: "C:\\Program Files\\App",
expected: "C:/Program Files/App",
},
{
name: "Mixed separators",
input: "path/to\\file.txt",
expected: "path/to/file.txt",
},
{
name: "Empty path",
input: "",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := NormalizePath(tt.input)
if result != tt.expected {
t.Errorf("NormalizePath(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestJoinPaths(t *testing.T) {
tests := []struct {
name string
elements []string
expected string
}{
{
name: "Join Unix paths",
elements: []string{"usr", "local", "bin"},
expected: "usr/local/bin",
},
{
name: "Join with empty element",
elements: []string{"path", "", "file.txt"},
expected: "path/file.txt",
},
{
name: "Join with absolute path",
elements: []string{"/root", "dir", "file.txt"},
expected: "/root/dir/file.txt",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := JoinPaths(tt.elements...)
if result != tt.expected {
t.Errorf("JoinPaths(%v) = %q, want %q", tt.elements, result, tt.expected)
}
})
}
}
func TestIsSubPath(t *testing.T) {
tests := []struct {
name string
parentPath string
childPath string
expected bool
}{
{
name: "Direct child",
parentPath: "parent",
childPath: "parent/child",
expected: true,
},
{
name: "Nested child",
parentPath: "parent",
childPath: "parent/child/grandchild",
expected: true,
},
{
name: "Not a child",
parentPath: "parent",
childPath: "other/path",
expected: false,
},
{
name: "Empty parent",
parentPath: "",
childPath: "any/path",
expected: true,
},
{
name: "Same path",
parentPath: "path/to/dir",
childPath: "path/to/dir",
expected: true,
},
{
name: "Path with relative components",
parentPath: "path/to/dir",
childPath: "path/to/dir/../dir/file.txt",
expected: true,
},
{
name: "Path with relative components going up",
parentPath: "path/to/dir",
childPath: "path/to/dir/../../other",
expected: false,
},
{
name: "Current directory as parent",
parentPath: ".",
childPath: "any/path",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsSubPath(tt.parentPath, tt.childPath)
if result != tt.expected {
t.Errorf("IsSubPath(%q, %q) = %v, want %v", tt.parentPath, tt.childPath, result, tt.expected)
}
})
}
}
func TestAbsPath(t *testing.T) {
// Create a temporary directory for testing
tempDir := t.TempDir()
normalizedTempDir := NormalizePath(tempDir)
tests := []struct {
name string
input string
expected string
setup func() string
}{
{
name: "Relative path",
input: "file.txt",
setup: func() string {
// Get current working directory
wd, _ := filepath.Abs(".")
return NormalizePath(filepath.Join(wd, "file.txt"))
},
},
{
name: "Already absolute path",
input: normalizedTempDir,
expected: normalizedTempDir,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var expected string
if tt.setup != nil {
expected = tt.setup()
} else {
expected = tt.expected
}
result := AbsPath(tt.input)
if result != expected {
t.Errorf("AbsPath(%q) = %q, want %q", tt.input, result, expected)
}
})
}
}
func TestDirPath(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Unix path",
input: "/usr/local/bin/app",
expected: "/usr/local/bin",
},
{
name: "Relative path",
input: "dir/file.txt",
expected: "dir",
},
{
name: "File in root",
input: "/file.txt",
expected: "/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := DirPath(tt.input)
if result != tt.expected {
t.Errorf("DirPath(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestPathExists(t *testing.T) {
memFs := afero.NewMemMapFs()
tempDir := "/tmp/cache"
err := memFs.Mkdir(tempDir, 0o755)
if err != nil {
t.Fatalf("Failed to create test directory: %v", err)
}
// Create a test file
testFilePath := filepath.Join(tempDir, "test.txt")
err = afero.WriteFile(memFs, testFilePath, []byte("test content"), 0o644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
tests := []struct {
name string
path string
expected bool
}{
{
name: "Existing file",
path: testFilePath,
expected: true,
},
{
name: "Existing directory",
path: tempDir,
expected: true,
},
{
name: "Non-existent file",
path: filepath.Join(tempDir, "nonexistent.txt"),
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exists, err := afero.Exists(memFs, tt.path)
if err != nil {
t.Fatalf("Failed to check if path exists: %v", err)
}
if exists != tt.expected {
t.Errorf("PathExists(%q) = %v, want %v", tt.path, exists, tt.expected)
}
})
}
}
func TestIsAbsPath(t *testing.T) {
tests := []struct {
name string
path string
expected bool
}{
{
name: "Absolute path",
path: "/usr/local/bin",
expected: true,
},
{
name: "Relative path",
path: "dir/file.txt",
expected: false,
},
{
name: "Current directory",
path: ".",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsAbsPath(tt.path)
if result != tt.expected {
t.Errorf("IsAbsPath(%q) = %v, want %v", tt.path, result, tt.expected)
}
})
}
}
// TestPlatformSpecificBehavior tests platform-specific behavior
// to ensure our helpers work correctly on the current platform
func TestPlatformSpecificBehavior(t *testing.T) {
if runtime.GOOS == "windows" {
// Test Windows-specific behavior
t.Run("Windows path normalization", func(t *testing.T) {
path := "C:\\Users\\user\\Documents"
normalized := NormalizePath(path)
expected := "C:/Users/user/Documents"
if normalized != expected {
t.Errorf("NormalizePath(%q) = %q, want %q", path, normalized, expected)
}
})
t.Run("Windows DirPath", func(t *testing.T) {
path := "C:\\Users\\user\\Documents\\file.txt"
dir := DirPath(path)
expected := "C:/Users/user/Documents"
if dir != expected {
t.Errorf("DirPath(%q) = %q, want %q", path, dir, expected)
}
})
t.Run("Windows JoinPaths", func(t *testing.T) {
elements := []string{"C:\\Users", "user", "Documents"}
joined := JoinPaths(elements...)
expected := "C:/Users/user/Documents"
if joined != expected {
t.Errorf("JoinPaths(%v) = %q, want %q", elements, joined, expected)
}
})
t.Run("Windows IsSubPath", func(t *testing.T) {
parent := "C:\\Users\\user"
child := "C:\\Users\\user\\Documents"
if !IsSubPath(parent, child) {
t.Errorf("IsSubPath(%q, %q) = false, want true", parent, child)
}
})
} else {
// Test Unix-specific behavior
t.Run("Unix path normalization", func(t *testing.T) {
path := "/home/user/documents"
normalized := NormalizePath(path)
if normalized != path {
t.Errorf("NormalizePath(%q) = %q, want %q", path, normalized, path)
}
})
t.Run("Unix DirPath", func(t *testing.T) {
path := "/home/user/documents/file.txt"
dir := DirPath(path)
expected := "/home/user/documents"
if dir != expected {
t.Errorf("DirPath(%q) = %q, want %q", path, dir, expected)
}
})
t.Run("Unix JoinPaths", func(t *testing.T) {
elements := []string{"/home", "user", "documents"}
joined := JoinPaths(elements...)
expected := "/home/user/documents"
if joined != expected {
t.Errorf("JoinPaths(%v) = %q, want %q", elements, joined, expected)
}
})
t.Run("Unix IsSubPath", func(t *testing.T) {
parent := "/home/user"
child := "/home/user/documents"
if !IsSubPath(parent, child) {
t.Errorf("IsSubPath(%q, %q) = false, want true", parent, child)
}
})
}
}