1414
1515import XCTest
1616import NIOCore
17- import struct System. FileDescriptor
1817
1918func 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
4645func 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
6160fileprivate 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