Skip to content

Commit

Permalink
Merge pull request swiftlang#2009 from compnerd/the-pipes-are-clogged
Browse files Browse the repository at this point in the history
TestFileHandle: make the pipe construction work on Windows
  • Loading branch information
millenomi authored Apr 12, 2019
2 parents 2e4aa1c + 07d2f53 commit 288341e
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions TestFoundation/TestFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,22 @@ class TestFileHandle : XCTestCase {
allTemporaryFileURLs.append(url)
return fh!
}


#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
func createFileHandleForSeekErrors() -> FileHandle {
#if os(Windows)
var hReadPipe: HANDLE? = INVALID_HANDLE_VALUE
var hWritePipe: HANDLE? = INVALID_HANDLE_VALUE
if CreatePipe(&hReadPipe, &hWritePipe, nil, 0) == FALSE {
assert(false)
}

if CloseHandle(hWritePipe) == FALSE {
assert(false)
}

return FileHandle(handle: hReadPipe!, closeOnDealloc: true)
#else
var fds: [Int32] = [-1, -1]
fds.withUnsafeMutableBufferPointer { (pointer) -> Void in
pipe(pointer.baseAddress)
Expand All @@ -78,8 +92,10 @@ class TestFileHandle : XCTestCase {
let fh = FileHandle(fileDescriptor: fds[0], closeOnDealloc: true)
allHandles.append(fh)
return fh
#endif
}

#endif

let seekError = NSError(domain: NSCocoaErrorDomain, code: NSFileReadUnknownError, userInfo: [ NSUnderlyingErrorKey: NSError(domain: NSPOSIXErrorDomain, code: Int(ESPIPE), userInfo: [:])])

func createFileHandleForReadErrors() -> FileHandle {
Expand Down Expand Up @@ -108,13 +124,15 @@ class TestFileHandle : XCTestCase {
allHandles = []
allTemporaryFileURLs = []
}


#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
func testHandleCreationAndCleanup() {
_ = createFileHandle()
_ = createFileHandleForSeekErrors()
_ = createFileHandleForReadErrors()
}

#endif

func testReadUpToCount() {
let handle = createFileHandle()

Expand Down Expand Up @@ -178,7 +196,8 @@ class TestFileHandle : XCTestCase {
_ = try createFileHandleForReadErrors().readToEnd()
}, "Must throw when encountering a read error")
}


#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
func testOffset() {
// One byte at a time:
let handle = createFileHandle()
Expand All @@ -198,7 +217,8 @@ class TestFileHandle : XCTestCase {
_ = try createFileHandleForSeekErrors().offset()
}, "Must throw when encountering a seek error")
}

#endif

func createPipe() -> Pipe {
let pipe = Pipe()
allHandles.append(pipe.fileHandleForWriting)
Expand Down Expand Up @@ -463,11 +483,9 @@ class TestFileHandle : XCTestCase {
}

static var allTests : [(String, (TestFileHandle) -> () throws -> ())] {
return [
("testHandleCreationAndCleanup", testHandleCreationAndCleanup),
var tests: [(String, (TestFileHandle) -> () throws -> ())] = [
("testReadUpToCount", testReadUpToCount),
("testReadToEnd", testReadToEnd),
("testOffset", testOffset),
("testWritingWithData", testWritingWithData),
("testWritingWithBuffer", testWritingWithBuffer),
("testWritingWithMultiregionData", testWritingWithMultiregionData),
Expand All @@ -484,6 +502,15 @@ class TestFileHandle : XCTestCase {
("test_waitForDataInBackgroundAndNotify", test_waitForDataInBackgroundAndNotify),
("test_readWriteHandlers", test_readWriteHandlers),
]

#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
tests.append(contentsOf: [
("testHandleCreationAndCleanup", testHandleCreationAndCleanup),
("testOffset", testOffset),
])
#endif

return tests
}
}

Expand Down

0 comments on commit 288341e

Please sign in to comment.