@@ -133,19 +133,23 @@ class SFTPTests: XCTestCase {
133
133
}
134
134
135
135
func testWrite( ) throws {
136
- let expectation = self . expectation ( description: " Buffer Written" )
136
+ let writeExpectation = self . expectation ( description: " File Written" )
137
137
138
138
//var connection: SSHClient?
139
- // var sftp: SFTPClient?
139
+ var root : SFTPTranslator ? = nil
140
140
var totalWritten = 0
141
141
142
142
let gen = RandomInputGenerator ( fast: true )
143
143
144
- let cancellable = SSHClient . dialWithTestConfig ( )
144
+ let cancelWrite = SSHClient . dialWithTestConfig ( )
145
145
. flatMap ( ) { $0. requestSFTP ( ) }
146
- . tryMap ( ) { try SFTPTranslator ( on: $0) }
146
+ . tryMap ( ) { t in
147
+ let r = try SFTPTranslator ( on: t)
148
+ root = r
149
+ return r
150
+ }
147
151
. flatMap ( ) { $0. walkTo ( " /tmp " ) }
148
- . flatMap ( ) { $0. create ( name: " newfile " , flags : O_WRONLY , mode: S_IRWXU) }
152
+ . flatMap ( ) { $0. create ( name: " newfile " , mode: S_IRWXU) }
149
153
. flatMap ( ) { file in
150
154
return gen. read ( max: 5 * 1024 * 1024 )
151
155
. flatMap ( ) { data in
@@ -154,7 +158,7 @@ class SFTPTests: XCTestCase {
154
158
} . sink ( receiveCompletion: { completion in
155
159
switch completion {
156
160
case . finished:
157
- expectation . fulfill ( )
161
+ writeExpectation . fulfill ( )
158
162
case . failure( let error) :
159
163
// Problem here is we can have both SFTP and SSHError
160
164
if let err = error as? SSH . FileError {
@@ -169,6 +173,51 @@ class SFTPTests: XCTestCase {
169
173
170
174
waitForExpectations ( timeout: 15 , handler: nil )
171
175
XCTAssert ( totalWritten == 5 * 1024 * 1024 , " Did not write all data " )
176
+
177
+ totalWritten = 0
178
+ let overwriteExpectation = self . expectation ( description: " File Overwritten " )
179
+
180
+ guard let root = root else {
181
+ XCTFail ( " No root translator. " )
182
+ return
183
+ }
184
+
185
+ let cancelOverwrite = root. walkTo ( " /tmp " )
186
+ . flatMap ( ) { $0. create ( name: " newfile " , mode: S_IRWXU) }
187
+ . flatMap ( ) { file in
188
+ return gen. read ( max: 4 * 1024 * 1024 )
189
+ . flatMap ( ) { data in
190
+ return file. write ( data, max: data. count)
191
+ }
192
+ } . sink ( receiveCompletion: { completion in
193
+ switch completion {
194
+ case . finished:
195
+ overwriteExpectation. fulfill ( )
196
+ case . failure( let error) :
197
+ // Problem here is we can have both SFTP and SSHError
198
+ if let err = error as? SSH . FileError {
199
+ XCTFail ( err. description)
200
+ } else {
201
+ XCTFail ( " Crash " )
202
+ }
203
+ }
204
+ } , receiveValue: { written in
205
+ totalWritten += written
206
+ } )
207
+
208
+ waitForExpectations ( timeout: 15 , handler: nil )
209
+ XCTAssert ( totalWritten == 4 * 1024 * 1024 , " Did not write all data " )
210
+
211
+ let statExpectation = self . expectation ( description: " File Stat " )
212
+ let cancelStat = root. walkTo ( " /tmp/newfile " )
213
+ . flatMap { ( t: Translator ) -> AnyPublisher < FileAttributes , Error > in t. stat ( ) }
214
+ . assertNoFailure ( )
215
+ . sink { ( stats: FileAttributes ) in
216
+ XCTAssertTrue ( stats [ . size] as! Int == 4 * 1024 * 1024 )
217
+ statExpectation. fulfill ( )
218
+ }
219
+
220
+ waitForExpectations ( timeout: 15 , handler: nil )
172
221
}
173
222
174
223
func testWriteToWriter( ) throws {
@@ -190,7 +239,7 @@ class SFTPTests: XCTestCase {
190
239
} . flatMap ( ) { f -> AnyPublisher < Int , Error > in
191
240
let file = f as! SFTPFile
192
241
return translator!. walkTo ( " /tmp/ " )
193
- . flatMap { $0. create ( name: " linux.tar.xz " , flags : O_WRONLY , mode: S_IRWXU) }
242
+ . flatMap { $0. create ( name: " linux.tar.xz " , mode: S_IRWXU) }
194
243
. flatMap ( ) { file. writeTo ( $0) } . eraseToAnyPublisher ( )
195
244
} . sink ( receiveCompletion: { completion in
196
245
switch completion {
0 commit comments