forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_complete_test.go
264 lines (252 loc) · 6.68 KB
/
auto_complete_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
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
package e2e
import (
"testing"
"gotest.tools/v3/icmd"
)
func TestCompletionFlag(t *testing.T) {
flag := "--generate-bash-completion"
bucket := s3BucketFromTestName(t)
testcases := []struct {
name string
precedingArgs []string
arg string
remoteFiles []string
expected map[int]compareFunc
shell string
}{
{
name: "cp complete empty string",
precedingArgs: []string{"cp"},
arg: "",
// local file completions are prepared by the shell
expected: map[int]compareFunc{},
shell: "/bin/bash",
},
{
name: "cat complete empty string",
precedingArgs: []string{"cat"},
arg: "",
expected: map[int]compareFunc{
0: equals("s3://%s/", bucket),
},
shell: "/bin/pwsh",
},
{
name: "mb complete empty string",
precedingArgs: []string{"mb"},
arg: "",
expected: map[int]compareFunc{
0: equals("s3://"),
},
shell: "/bin/pwsh",
},
{
name: "mb complete bucket",
precedingArgs: []string{"mb"},
arg: "s3://bu",
expected: map[int]compareFunc{
0: equals("s3://bu"),
},
shell: "/bin/pwsh",
},
{
name: "rb complete empty string",
precedingArgs: []string{"rb"},
arg: "",
expected: map[int]compareFunc{
0: equals("s3://%s/", bucket),
},
shell: "/bin/pwsh",
},
{
name: "rb should not complete keys string",
precedingArgs: []string{"rb"},
arg: "s3://" + bucket + "/f",
expected: map[int]compareFunc{
0: equals("s3://%s/", bucket),
},
remoteFiles: []string{
"file.txt",
"fdir/child.txt",
},
shell: "/bin/pwsh",
},
{
name: "select complete empty string",
precedingArgs: []string{"select"},
arg: "",
expected: map[int]compareFunc{
0: equals("s3://%s/", bucket),
},
shell: "/bin/pwsh",
},
{
name: "cp complete bucket names in pwsh",
precedingArgs: []string{"cp"},
arg: "s3://",
expected: map[int]compareFunc{
0: equals("s3://%s/", bucket),
},
shell: "/bin/pwsh",
},
{
name: "cp complete bucket names in zsh",
precedingArgs: []string{"cp"},
arg: "s3://",
expected: map[int]compareFunc{
0: equals("s3\\://%s/", bucket),
},
shell: "/bin/zsh",
},
{
name: "cp complete bucket names in bash",
precedingArgs: []string{"cp"},
arg: "s3://",
expected: map[int]compareFunc{
0: equals("//%s/", bucket),
1: equals("s3://%s/", bucket),
},
shell: "/bin/bash",
},
{
name: "cp complete bucket keys pwsh",
precedingArgs: []string{"cp"},
arg: "s3://" + bucket + "/",
remoteFiles: []string{
"file0.txt",
"file1.txt",
"filedir/child.txt",
"dir/child.txt",
},
expected: map[int]compareFunc{
0: equals("s3://%s/dir/", bucket),
1: equals("s3://%s/file0.txt", bucket),
2: equals("s3://%s/file1.txt", bucket),
3: equals("s3://%s/filedir/", bucket),
},
shell: "/bin/pwsh",
},
{
name: "cp complete bucket keys bash",
precedingArgs: []string{"cp"},
arg: "s3://" + bucket + "/",
remoteFiles: []string{
"file0.txt",
"file1.txt",
"filedir/child.txt",
"dir/child.txt",
},
expected: map[int]compareFunc{
0: equals("//%s/dir/", bucket),
1: equals("//%s/file0.txt", bucket),
2: equals("//%s/file1.txt", bucket),
3: equals("//%s/filedir/", bucket),
4: equals("s3://%s/dir/", bucket),
5: equals("s3://%s/file0.txt", bucket),
6: equals("s3://%s/file1.txt", bucket),
7: equals("s3://%s/filedir/", bucket),
},
shell: "/bin/bash",
},
{
name: "cp complete bucket keys zsh",
precedingArgs: []string{"cp"},
arg: "s3://" + bucket + "/",
remoteFiles: []string{
"file0.txt",
"file1.txt",
"filedir/child.txt",
"dir/child.txt",
},
expected: map[int]compareFunc{
0: equals("s3\\://%s/dir/", bucket),
1: equals("s3\\://%s/file0.txt", bucket),
2: equals("s3\\://%s/file1.txt", bucket),
3: equals("s3\\://%s/filedir/", bucket),
},
shell: "/bin/zsh",
},
{
name: "cp complete keys with colon bash",
precedingArgs: []string{"cp"},
arg: "s3://" + bucket + "/co:lo",
remoteFiles: []string{
"co:lon:in:key",
"co:lonized",
},
expected: map[int]compareFunc{
0: equals("lon:in:key"),
1: equals("lonized"),
2: equals("s3://%s/co:lon:in:key", bucket),
3: equals("s3://%s/co:lonized", bucket),
},
shell: "/bin/bash",
},
{
name: "cp complete keys with colon zsh",
precedingArgs: []string{"cp"},
arg: "s3://" + bucket + "/co:lo",
remoteFiles: []string{
"co:lon:in:key",
"co:lonized",
},
expected: map[int]compareFunc{
0: equals("s3\\://%s/co\\:lon\\:in\\:key", bucket),
1: equals("s3\\://%s/co\\:lonized", bucket),
},
shell: "/bin/zsh",
},
{
name: "cp complete keys with backslash",
precedingArgs: []string{"cp"},
arg: "s3://" + bucket + "/back\\",
remoteFiles: []string{
`back\slash`,
`backback`,
},
expected: map[int]compareFunc{
0: equals("s3://%s/back\\slash", bucket),
},
shell: "/bin/pwsh",
},
{
name: "cp complete keys with asterisk",
precedingArgs: []string{"cp"},
arg: "s3://" + bucket + "/as*",
remoteFiles: []string{
"as*terisk",
"as*oburiks",
},
expected: map[int]compareFunc{
0: equals("s3://%s/as*oburiks", bucket),
1: equals("s3://%s/as*terisk", bucket),
},
shell: "/bin/pwsh",
},
/*
Question marks and asterisk are thought to be wildcard (special charactes)
by the s5cmd so when they're given s5cmd's behaviour changes.
When asterisk is given s5cmd also matches the keys with literal '*' as well as
all keys that match the URL'S regexp. So the completions with '*' accidentally include the
keys that contains '*' while the shell scripts filter out those that does not have '*'s.
On the other hand when the question mark is given then s5cmd do not list keys
if it is the last character. Because the ? represent a single character and
it is not expanded to complete remaining of the key.
*/
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
s3client, s5cmd := setup(t)
// prepare remote bucket content
createBucket(t, s3client, bucket)
for _, f := range tc.remoteFiles {
putFile(t, s3client, bucket, f, "content")
}
cmd := s5cmd(append(tc.precedingArgs, tc.arg, flag)...)
res := icmd.RunCmd(cmd, withEnv("SHELL", tc.shell))
assertLines(t, res.Stdout(), tc.expected, sortInput(true))
})
}
}