-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandler_test.go
More file actions
92 lines (82 loc) · 2.45 KB
/
filehandler_test.go
File metadata and controls
92 lines (82 loc) · 2.45 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
package main
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsSubdir(t *testing.T) {
tmp := t.TempDir()
os.MkdirAll(tmp+"/root/subdir", 0o755) // nolint:errcheck
os.MkdirAll(tmp+"/other", 0o755) // nolint:errcheck
tests := []struct {
name string
root string
sub string
want bool
}{
{"self", tmp + "/root", tmp + "/root", true},
{"direct subdir", tmp + "/root", tmp + "/root/subdir", true},
{"not subdir", tmp + "/root", tmp + "/other", false},
{"parent", tmp + "/root/subdir", tmp + "/root", false},
{"parent of root", tmp + "/root", tmp, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := isSubdir(tt.root, tt.sub)
assert.NoError(t, err)
assert.Equal(t, tt.want, got, "isSubdir(%q, %q)", tt.root, tt.sub)
})
}
}
func TestToRclonePath(t *testing.T) {
tmp := t.TempDir()
os.MkdirAll(tmp+"/google/drive1/folder", 0o755) // nolint:errcheck
f, _ := os.Create(tmp + "/google/drive1/folder/file.txt")
f.Close() // nolint:errcheck
f2, _ := os.Create(tmp + "/google/drive1/file.txt")
f2.Close() // nolint:errcheck
tests := []struct {
name string
root string
abs string
wantRem string
wantPath string
wantErr bool
}{
{"file in drive root", tmp + "/google", tmp + "/google/drive1/file.txt", "drive1", "file.txt", false},
{"file in subdir", tmp + "/google", tmp + "/google/drive1/folder/file.txt", "drive1", "folder/file.txt", false},
{"drive root itself", tmp + "/google", tmp + "/google/drive1", "drive1", "", false},
{"not under root", tmp + "/google", tmp, "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rem, sub, err := toRclonePath(tt.root, tt.abs)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.wantRem, rem)
assert.Equal(t, tt.wantPath, sub)
}
})
}
}
func TestPatchDestPath(t *testing.T) {
tests := []struct {
name string
src string
dest string
want string
}{
{"dest with trailing slash", "/foo/bar.txt", "/dest/", "/dest/bar.txt"},
{"dest without trailing slash", "/foo/bar.txt", "/dest", "/dest/bar.txt"},
{"src is dir", "/foo/dir", "/dest", "/dest/dir"},
{"src is file, dest is root", "/foo/file.txt", "/", "/file.txt"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := patchDestPath(tt.src, tt.dest)
assert.Equal(t, tt.want, got, "patchDestPath(%q, %q)", tt.src, tt.dest)
})
}
}