@@ -24,11 +24,12 @@ import (
24
24
)
25
25
26
26
func TestClient (t * testing.T ) {
27
- client , err := go_ftp . NewClient ( go_ftp.ClientConfig {
27
+ config := go_ftp.ClientConfig {
28
28
Hostname : "127.0.0.1:2121" ,
29
29
Username : "admin" ,
30
30
Password : "123456" ,
31
- })
31
+ }
32
+ client , err := go_ftp .NewClient (config )
32
33
require .NotNil (t , client )
33
34
require .NoError (t , err )
34
35
@@ -127,6 +128,55 @@ func TestClient(t *testing.T) {
127
128
require .ErrorContains (t , err , "retrieving new.txt failed: 551 File not available" )
128
129
})
129
130
131
+ t .Run ("mkdir and upload" , func (t * testing.T ) {
132
+ client , err := go_ftp .NewClient (go_ftp.ClientConfig {
133
+ Hostname : config .Hostname ,
134
+ Username : config .Username ,
135
+ Password : config .Password ,
136
+
137
+ CreateUploadDirectories : true ,
138
+ })
139
+ require .NotNil (t , client )
140
+ require .NoError (t , err )
141
+
142
+ // Make sure we've delted newdir from any previous test runs
143
+ require .NoError (t , os .RemoveAll (filepath .Join ("testdata" , "ftp-server" , "newdir" )))
144
+
145
+ // Upload files into new directories
146
+ err = client .UploadFile ("newdir/first.txt" , io .NopCloser (strings .NewReader ("first newdir data" )))
147
+ require .NoError (t , err )
148
+
149
+ err = client .UploadFile ("newdir/a/second.txt" , io .NopCloser (strings .NewReader ("second newdir data" )))
150
+ require .NoError (t , err )
151
+
152
+ err = client .UploadFile ("newdir/a/b/third.txt" , io .NopCloser (strings .NewReader ("third newdir data" )))
153
+ require .NoError (t , err )
154
+
155
+ // read all files
156
+ file , err := client .Open ("newdir/first.txt" )
157
+ require .NoError (t , err )
158
+
159
+ bs , err := io .ReadAll (file )
160
+ require .NoError (t , err )
161
+ require .Equal (t , "first newdir data" , string (bs ))
162
+
163
+ // second file
164
+ file , err = client .Open ("newdir/a/second.txt" )
165
+ require .NoError (t , err )
166
+
167
+ bs , err = io .ReadAll (file )
168
+ require .NoError (t , err )
169
+ require .Equal (t , "second newdir data" , string (bs ))
170
+
171
+ // third file
172
+ file , err = client .Open ("newdir/a/b/third.txt" )
173
+ require .NoError (t , err )
174
+
175
+ bs , err = io .ReadAll (file )
176
+ require .NoError (t , err )
177
+ require .Equal (t , "third newdir data" , string (bs ))
178
+ })
179
+
130
180
t .Run ("delete" , func (t * testing.T ) {
131
181
err := client .Delete ("/missing.txt" )
132
182
require .NoError (t , err )
@@ -217,6 +267,7 @@ func TestClient(t *testing.T) {
217
267
"archive" , "archive/old.txt" , "archive/empty2.txt" ,
218
268
"with-empty" , "with-empty/EMPTY1.txt" , "with-empty/empty_file2.txt" ,
219
269
"with-empty/data.txt" , "with-empty/data2.txt" ,
270
+ "newdir" , "newdir/first.txt" , "newdir/a" , "newdir/a/second.txt" , "newdir/a/b" , "newdir/a/b/third.txt" ,
220
271
})
221
272
})
222
273
@@ -248,6 +299,7 @@ func TestClient(t *testing.T) {
248
299
"bigdata" , "bigdata/large.txt" ,
249
300
"archive" , "archive/old.txt" , "archive/empty2.txt" ,
250
301
"Upper" , "Upper/names.txt" ,
302
+ "newdir" , "newdir/first.txt" , "newdir/a" , "newdir/a/second.txt" , "newdir/a/b" , "newdir/a/b/third.txt" ,
251
303
})
252
304
})
253
305
0 commit comments