-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathget_test.go
198 lines (175 loc) · 4.63 KB
/
get_test.go
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
package upx
import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func GetStartBetweenEndFiles(t *testing.T, src, dst, correct, start, end string) {
var err error
src = AbsPath(src)
if start != "" && start[0] != '/' {
start = path.Join(src, start)
}
if end != "" && end[0] != '/' {
end = path.Join(src, end)
}
if dst == "" {
_, err = Upx("get", src, "--start="+start, "--end="+end)
} else {
_, err = Upx("get", src, dst, "--start="+start, "--end="+end)
}
assert.NoError(t, err)
}
/*
测试目录 test1:start=123 end=999 test2:start=111 end=666
input: local: local:
|-- 111 ├── 333 ├── 111
|-- 333 ├── 666 ├── 333
|-- 777 │ ├── 111 └── 444
|-- 444 │ ├── 333 └── 666
! `-- 666 │ ├── 666
`-- 666 │ └── 777
|-- 111 └── 777
|-- 333
|-- 666
`-- 777
*/
func TestGetStartBetweenEndFiles(t *testing.T) {
nowpath, _ := os.Getwd()
root := strings.Join(strings.Split(ROOT, " "), "-")
base := root + "/get/"
pwd, err := ioutil.TempDir("", "test")
assert.NoError(t, err)
localBase := filepath.Join(pwd, "get")
func() {
SetUp()
err := os.MkdirAll(localBase, 0755)
assert.NoError(t, err)
}()
defer TearDown()
err = os.Chdir(localBase)
assert.NoError(t, err)
Upx("mkdir", base)
Upx("cd", base)
type uploadFiles []struct {
name string
file string
dst string
correct string
}
type uploadDirs []struct {
dir string
dst string
correct string
}
//构造测试目录
files := uploadFiles{
{name: "111", file: filepath.Join(localBase, "111"), dst: "", correct: path.Join(base, "111")},
{name: "333", file: filepath.Join(localBase, "333"), dst: "", correct: path.Join(base, "333")},
{name: "333", file: "333", dst: path.Join(base, "333"), correct: path.Join(base, "333")},
{name: "777", file: "777", dst: base, correct: path.Join(base, "777")},
{name: "666", file: "666", dst: base + "/444/", correct: path.Join(base, "444", "666")},
}
for _, file := range files {
CreateFile(file.name)
putFile(t, file.file, file.dst, file.correct)
}
log.Println(122)
dirs := uploadDirs{
{dir: localBase, dst: base + "/666/", correct: base + "/666/"},
}
for _, dir := range dirs {
putDir(t, dir.dir, dir.dst, dir.correct)
}
type list struct {
start string
end string
testDir string
}
type test struct {
input list
real []string
want []string
}
//构造测试
tests := []test{
{input: list{start: "123", end: "999", testDir: filepath.Join(nowpath, "test1")}, real: localFile("test1", base), want: upFile(t, base, "123", "999")},
{input: list{start: "111", end: "666", testDir: filepath.Join(nowpath, "test2")}, real: localFile("test2", base), want: upFile(t, base, "444", "666")},
}
for _, tc := range tests {
input := tc.input
err = os.MkdirAll(input.testDir, os.ModePerm)
if err != nil {
log.Println(err)
}
GetStartBetweenEndFiles(t, base, input.testDir, input.testDir, input.start, input.end)
sort.Strings(tc.real)
sort.Strings(tc.want)
assert.Equal(t, len(tc.real), len(tc.want))
for i := 0; i < len(tc.real); i++ {
log.Println("compare:", tc.real[i], " ", tc.want[i])
assert.Equal(t, tc.real[i], tc.want[i])
}
}
}
// 递归获取下载到本地的文件
func localFile(local, up string) []string {
var locals []string
localLen := len(local)
fInfos, _ := ioutil.ReadDir(local + "/")
for _, fInfo := range fInfos {
fp := filepath.Join(local, fInfo.Name())
//使用云存储目录作为前缀方便比较
locals = append(locals, up[:len(up)-1]+fp[localLen:])
if IsDir(fp) {
localFile(fp, up)
}
}
return locals
}
// 递归获取云存储目录文件
func upFile(t *testing.T, up, start, end string) []string {
b, err := Upx("ls", up)
assert.NoError(t, err)
var ups []string
output := strings.TrimRight(string(b), "\n")
for _, line := range strings.Split(output, "\n") {
items := strings.Split(line, " ")
fp := path.Join(up, items[len(items)-1])
ups = append(ups, fp)
if items[0][0] == 'd' {
upFile(t, fp, start, end)
}
}
var upfiles []string
for _, file := range ups {
if file >= start && file < end {
upfiles = append(upfiles, file)
}
}
return upfiles
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func AbsPath(upPath string) (ret string) {
if strings.HasPrefix(upPath, "/") {
ret = path.Join(upPath)
} else {
ret = path.Join("/", upPath)
}
if strings.HasSuffix(upPath, "/") && ret != "/" {
ret += "/"
}
return
}