Skip to content

Handle Windows Errors in FileHandle Tests #2441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,24 @@ extension FileHandle {
if error == 0 {
userInfo[NSFileHandleNotificationDataItem] = Data(data)
} else {
#if os(Windows)
// On Windows, reading from a directory results in an
// ERROR_ACCESS_DENIED. If we get ERROR_ACCESS_DENIED
// and the handle we attempt to read from is a
// directory, replace it with
// ERROR_DIRECTORY_NOT_SUPPORTED to match POSIX's EISDIR
var translatedError = error
if error == ERROR_ACCESS_DENIED {
var fileInfo = BY_HANDLE_FILE_INFORMATION()
GetFileInformationByHandle(self.handle, &fileInfo)
if fileInfo.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == DWORD(FILE_ATTRIBUTE_DIRECTORY) {
translatedError = ERROR_DIRECTORY_NOT_SUPPORTED
}
}
userInfo["NSFileHandleError"] = Int(translatedError)
#else
userInfo["NSFileHandleError"] = Int(error)
#endif
}

DispatchQueue.main.async {
Expand Down
8 changes: 8 additions & 0 deletions TestFoundation/TestFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ class TestFileHandle : XCTestCase {
return fh
}

#if os(Windows)
let readError = NSError(domain: NSCocoaErrorDomain, code: NSFileReadUnknownError, userInfo: [ NSUnderlyingErrorKey: NSError(domain: "org.swift.Foundation.WindowsError", code: 1, userInfo: [:])])
#else
let readError = NSError(domain: NSCocoaErrorDomain, code: NSFileReadUnknownError, userInfo: [ NSUnderlyingErrorKey: NSError(domain: NSPOSIXErrorDomain, code: Int(EISDIR), userInfo: [:])])
#endif

override func tearDown() {
for handle in allHandles {
Expand Down Expand Up @@ -463,7 +467,11 @@ class TestFileHandle : XCTestCase {
}

XCTAssertNil(notification.userInfo?[NSFileHandleNotificationDataItem])
#if os(Windows)
XCTAssertEqual(error, NSNumber(value: ERROR_DIRECTORY_NOT_SUPPORTED))
#else
XCTAssertEqual(error, NSNumber(value: EISDIR))
#endif
return true
}

Expand Down