Skip to content

Commit f358af3

Browse files
fixup: No swift-system, duh
Signed-off-by: Si Beaumont <beaumont@apple.com>
1 parent 5c6e2fa commit f358af3

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

Tests/NIOCoreTests/LinuxTest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import XCTest
1818
class LinuxTest: XCTestCase {
1919
func testCoreCountQuota() {
2020
#if os(Linux) || os(Android)
21-
[
21+
try [
2222
("50000", "100000", 1),
2323
("100000", "100000", 1),
2424
("100000\n", "100000", 1),
@@ -31,8 +31,8 @@ class LinuxTest: XCTestCase {
3131
("100000", "", nil),
3232
("100000", "0", nil)
3333
].forEach { quota, period, count in
34-
withTemporaryFile(content: quota) { (_, quotaPath) -> Void in
35-
withTemporaryFile(content: period) { (_, periodPath) -> Void in
34+
try withTemporaryFile(content: quota) { (_, quotaPath) -> Void in
35+
try withTemporaryFile(content: period) { (_, periodPath) -> Void in
3636
XCTAssertEqual(Linux.coreCount(quota: quotaPath, period: periodPath), count)
3737
}
3838
}
@@ -42,7 +42,7 @@ class LinuxTest: XCTestCase {
4242

4343
func testCoreCountCpuset() {
4444
#if os(Linux) || os(Android)
45-
[
45+
try [
4646
("0", 1),
4747
("0,3", 2),
4848
("0-3", 4),
@@ -51,7 +51,7 @@ class LinuxTest: XCTestCase {
5151
("0,2-4,6,7,9-11", 9),
5252
("", nil)
5353
].forEach { cpuset, count in
54-
withTemporaryFile(content: cpuset) { (_, path) -> Void in
54+
try withTemporaryFile(content: cpuset) { (_, path) -> Void in
5555
XCTAssertEqual(Linux.coreCount(cpuset: path), count)
5656
}
5757
}

Tests/NIOCoreTests/XCTest+Extensions.swift

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import XCTest
1616
import NIOCore
17-
import struct System.FileDescriptor
1817

1918
func assert(_ condition: @autoclosure () -> Bool, within time: TimeAmount, testInterval: TimeAmount? = nil, _ message: String = "condition not satisfied in time", file: StaticString = #file, line: UInt = #line) {
2019
let testInterval = testInterval ?? TimeAmount.nanoseconds(time.nanoseconds / 5)
@@ -44,18 +43,18 @@ func assertNoThrowWithValue<T>(_ body: @autoclosure () throws -> T, defaultValue
4443
}
4544

4645
func withTemporaryFile<T>(content: String? = nil, _ body: (NIOCore.NIOFileHandle, String) throws -> T) throws -> T {
47-
let (fd, path) = openTemporaryFile()
48-
let fileHandle = NIOFileHandle(descriptor: fd)
46+
let temporaryFilePath = "\(temporaryDirectory)/nio_\(UUID())"
47+
FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8))
4948
defer {
50-
XCTAssertNoThrow(try fileHandle.close())
51-
XCTAssertEqual(0, unlink(path))
49+
XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath))
5250
}
53-
if let content = content {
54-
let fileDesciptor = FileDescriptor(rawValue: fd)
55-
try fileDesciptor.writeAll(content.utf8)
56-
XCTAssertEqual(try fileDesciptor.seek(offset: 0, from: .start), 0)
51+
52+
let fileHandle = try NIOFileHandle(path: temporaryFilePath, mode: .write)
53+
defer {
54+
XCTAssertNoThrow(try fileHandle.close())
5755
}
58-
return try body(fileHandle, path)
56+
57+
return try body(fileHandle, temporaryFilePath)
5958
}
6059

6160
fileprivate var temporaryDirectory: String {
@@ -77,16 +76,3 @@ fileprivate var temporaryDirectory: String {
7776
#endif // targetEnvironment
7877
}
7978
}
80-
81-
func openTemporaryFile() -> (CInt, String) {
82-
let template = "\(temporaryDirectory)/nio_XXXXXX"
83-
var templateBytes = template.utf8 + [0]
84-
let templateBytesCount = templateBytes.count
85-
let fd = templateBytes.withUnsafeMutableBufferPointer { ptr in
86-
ptr.baseAddress!.withMemoryRebound(to: Int8.self, capacity: templateBytesCount) { (ptr: UnsafeMutablePointer<Int8>) in
87-
return mkstemp(ptr)
88-
}
89-
}
90-
templateBytes.removeLast()
91-
return (fd, String(decoding: templateBytes, as: Unicode.UTF8.self))
92-
}

0 commit comments

Comments
 (0)