Skip to content

Commit 83ef20f

Browse files
fixup: LinuxTest
Signed-off-by: Si Beaumont <beaumont@apple.com>
1 parent 906e798 commit 83ef20f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Tests/NIOCoreTests/LinuxTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import XCTest
16+
@testable import NIOCore
1617

1718
class LinuxTest: XCTestCase {
1819
func testCoreCountQuota() {

Tests/NIOCoreTests/XCTest+Extensions.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,31 @@ func assertNoThrowWithValue<T>(_ body: @autoclosure () throws -> T, defaultValue
4141
}
4242
}
4343
}
44+
45+
func withTemporaryFile<T>(content: String? = nil, _ body: (NIOCore.NIOFileHandle, String) throws -> T) rethrows -> T {
46+
let (fd, path) = openTemporaryFile()
47+
let fileHandle = NIOFileHandle(descriptor: fd)
48+
defer {
49+
XCTAssertNoThrow(try fileHandle.close())
50+
XCTAssertEqual(0, unlink(path))
51+
}
52+
if let content = content {
53+
try Array(content.utf8).withUnsafeBufferPointer { ptr in
54+
var toWrite = ptr.count
55+
var start = ptr.baseAddress!
56+
while toWrite > 0 {
57+
let res = try Posix.write(descriptor: fd, pointer: start, size: toWrite)
58+
switch res {
59+
case .processed(let written):
60+
toWrite -= written
61+
start = start + written
62+
case .wouldBlock:
63+
XCTFail("unexpectedly got .wouldBlock from a file")
64+
continue
65+
}
66+
}
67+
XCTAssertEqual(0, lseek(fd, 0, SEEK_SET))
68+
}
69+
}
70+
return try body(fileHandle, path)
71+
}

0 commit comments

Comments
 (0)