Skip to content

Commit 9371cbf

Browse files
authored
FoundationEssentials: correct callbacks for moveItem(atPath:toPath:) (#661)
In the case that the destination exists, we should check for continue after error. If the source does not exist, simply do nothing. Furthermore, adjust the test to ensure that we are using the path spelling rather than the FSR. Take the opportunity to correct an error in the tests where we would not check the correct captured item set.
1 parent 38d2d4e commit 9371cbf

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

Sources/FoundationEssentials/FileManager/FileManager+Directories.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ extension _FileManagerImpl {
176176
var pwszFullPath: PWSTR? = nil
177177
_ = PathAllocCombine(item.fileName, $0, PATHCCH_ALLOW_LONG_PATHS, &pwszFullPath)
178178
defer { LocalFree(pwszFullPath) }
179-
return String(decodingCString: pwszFullPath!, as: UTF16.self).standardizingPath
179+
return String(decodingCString: pwszFullPath!, as: UTF16.self).standardizingPath.replacing("\\", with: "/")
180180
})
181181
}
182182
}

Sources/FoundationEssentials/FileManager/FileOperations.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,21 @@ enum _FileOperations {
566566

567567
try source.withNTPathRepresentation { pwszSource in
568568
var faSourceAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
569-
guard GetFileAttributesExW(pwszSource, GetFileExInfoStandard, &faSourceAttributes) else {
570-
throw CocoaError.moveFileError(GetLastError(), src, dst)
569+
if !GetFileAttributesExW(pwszSource, GetFileExInfoStandard, &faSourceAttributes) {
570+
let error = CocoaError.moveFileError(GetLastError(), src, dst)
571+
guard fileManager._shouldProceedAfter(error: error, movingItemAtPath: source, to: destination) else {
572+
throw error
573+
}
574+
return
571575
}
572576

573577
try destination.withNTPathRepresentation { pwszDestination in
574578
var faDestinationAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
575-
guard GetFileAttributesExW(pwszDestination, GetFileExInfoStandard, &faDestinationAttributes) else {
576-
throw CocoaError.moveFileError(GetLastError(), src, dst)
579+
if GetFileAttributesExW(pwszDestination, GetFileExInfoStandard, &faDestinationAttributes) {
580+
let error = CocoaError.moveFileError(GetLastError(), src, dst)
581+
guard fileManager._shouldProceedAfter(error: error, movingItemAtPath: source, to: destination) else {
582+
throw error
583+
}
577584
}
578585

579586
// `MoveFileExW` does not work if the source and

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ final class FileManagerTests : XCTestCase {
374374
XCTAssertEqual(try $0.subpathsOfDirectory(atPath: ".").sorted(), ["dir2", "dir2/bar", "dir2/foo", "other_file"])
375375
XCTAssertEqual($0.contents(atPath: "dir2/foo"), data)
376376

377-
let rootDir = $0.currentDirectoryPath
377+
let rootDir = URL(fileURLWithPath: $0.currentDirectoryPath).path
378378
XCTAssertEqual($0.delegateCaptures.shouldMove, [.init("\(rootDir)/dir", "\(rootDir)/dir2")])
379-
379+
380380
try $0.moveItem(atPath: "does_not_exist", toPath: "dir3")
381-
XCTAssertEqual($0.delegateCaptures.shouldProceedAfterCopyError, [])
381+
XCTAssertEqual($0.delegateCaptures.shouldProceedAfterMoveError, [.init("\(rootDir)/does_not_exist", "\(rootDir)/dir3", code: .fileNoSuchFile)])
382382

383383
try $0.moveItem(atPath: "dir2", toPath: "other_file")
384384
XCTAssertTrue($0.delegateCaptures.shouldProceedAfterMoveError.contains(.init("\(rootDir)/dir2", "\(rootDir)/other_file", code: .fileWriteFileExists)))

0 commit comments

Comments
 (0)