Skip to content

Commit 00dccdb

Browse files
authored
chore: test percent encoding (#241)
* chore: test percent encoding * chore: format --------- Co-authored-by: FL33TW00D <FL33TW00D@users.noreply.github.com>
1 parent 28dbbc4 commit 00dccdb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Sources/Hub/Downloader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ extension Downloader: URLSessionDownloadDelegate {
346346
}
347347

348348
extension FileManager {
349-
func moveDownloadedFile(from srcURL: URL, to dstURL: URL) throws {
350-
if fileExists(atPath: dstURL.path(percentEncoded: false)) {
349+
func moveDownloadedFile(from srcURL: URL, to dstURL: URL, percentEncoded: Bool = false) throws {
350+
if fileExists(atPath: dstURL.path(percentEncoded: percentEncoded)) {
351351
try removeItem(at: dstURL)
352352
}
353353

Tests/HubTests/DownloaderTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,28 @@ final class DownloaderTests: XCTestCase {
165165
throw error
166166
}
167167
}
168+
169+
func testMoveDownloadedFilePercentEncodedFlag() throws {
170+
let appSupport = tempDir.appendingPathComponent("Application Support")
171+
let destination = appSupport.appendingPathComponent("config.json")
172+
let source1 = tempDir.appendingPathComponent("v1.incomplete")
173+
let source2 = tempDir.appendingPathComponent("v2.incomplete")
174+
175+
try FileManager.default.createDirectory(at: appSupport, withIntermediateDirectories: true)
176+
try "existing".write(to: destination, atomically: true, encoding: .utf8)
177+
try "v1".write(to: source1, atomically: true, encoding: .utf8)
178+
try "v2".write(to: source2, atomically: true, encoding: .utf8)
179+
180+
XCTAssertThrowsError(
181+
try FileManager.default.moveDownloadedFile(from: source1, to: destination, percentEncoded: true)
182+
) { error in
183+
XCTAssertEqual((error as NSError).code, 516)
184+
}
185+
XCTAssertEqual(try String(contentsOf: destination), "existing")
186+
187+
XCTAssertNoThrow(
188+
try FileManager.default.moveDownloadedFile(from: source2, to: destination, percentEncoded: false)
189+
)
190+
XCTAssertEqual(try String(contentsOf: destination), "v2")
191+
}
168192
}

0 commit comments

Comments
 (0)