Skip to content

TestFoundation: remove mkstemp usage #2008

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
Apr 17, 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
49 changes: 26 additions & 23 deletions TestFoundation/TestFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,38 +301,41 @@ class TestFileHandle : XCTestCase {
}

func test_truncateFile() {
mkstemp(template: "test_truncateFile.XXXXXX") { (fh) in
fh.truncateFile(atOffset: 50)
XCTAssertEqual(fh.offsetInFile, 50)
let url: URL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString, isDirectory: false)
_ = FileManager.default.createFile(atPath: url.path, contents: Data())

fh.truncateFile(atOffset: 0)
XCTAssertEqual(fh.offsetInFile, 0)
let fh: FileHandle = FileHandle(forUpdatingAtPath: url.path)!

fh.truncateFile(atOffset: 100)
XCTAssertEqual(fh.offsetInFile, 100)
fh.truncateFile(atOffset: 50)
XCTAssertEqual(fh.offsetInFile, 50)

fh.write(Data([1, 2]))
XCTAssertEqual(fh.offsetInFile, 102)
fh.truncateFile(atOffset: 0)
XCTAssertEqual(fh.offsetInFile, 0)

fh.seek(toFileOffset: 4)
XCTAssertEqual(fh.offsetInFile, 4)
fh.truncateFile(atOffset: 100)
XCTAssertEqual(fh.offsetInFile, 100)

(0..<20).forEach { fh.write(Data([$0])) }
XCTAssertEqual(fh.offsetInFile, 24)
fh.write(Data([1, 2]))
XCTAssertEqual(fh.offsetInFile, 102)

fh.seekToEndOfFile()
XCTAssertEqual(fh.offsetInFile, 102)
fh.seek(toFileOffset: 4)
XCTAssertEqual(fh.offsetInFile, 4)

fh.truncateFile(atOffset: 10)
XCTAssertEqual(fh.offsetInFile, 10)
(0..<20).forEach { fh.write(Data([$0])) }
XCTAssertEqual(fh.offsetInFile, 24)

fh.seek(toFileOffset: 0)
XCTAssertEqual(fh.offsetInFile, 0)
fh.seekToEndOfFile()
XCTAssertEqual(fh.offsetInFile, 102)

let data = fh.readDataToEndOfFile()
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, Data([0, 0, 0, 0, 0, 1, 2, 3, 4, 5]))
}
fh.truncateFile(atOffset: 10)
XCTAssertEqual(fh.offsetInFile, 10)

fh.seek(toFileOffset: 0)
XCTAssertEqual(fh.offsetInFile, 0)

let data = fh.readDataToEndOfFile()
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, Data([0, 0, 0, 0, 0, 1, 2, 3, 4, 5]))
}

func test_readabilityHandlerCloseFileRace() throws {
Expand Down
27 changes: 15 additions & 12 deletions TestFoundation/TestProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,24 @@ class TestProcess : XCTestCase {
process.launchPath = "/usr/bin/which"
process.arguments = ["which"]

mkstemp(template: "TestProcess.XXXXXX") { handle in
process.standardOutput = handle
let url: URL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString, isDirectory: false)
_ = FileManager.default.createFile(atPath: url.path, contents: Data())

process.launch()
process.waitUntilExit()
XCTAssertEqual(process.terminationStatus, 0)
let handle: FileHandle = FileHandle(forUpdatingAtPath: url.path)!

handle.seek(toFileOffset: 0)
let data = handle.readDataToEndOfFile()
guard let string = String(data: data, encoding: .ascii) else {
XCTFail("Could not read stdout")
return
}
XCTAssertTrue(string.hasSuffix("/which\n"))
process.standardOutput = handle

process.launch()
process.waitUntilExit()
XCTAssertEqual(process.terminationStatus, 0)

handle.seek(toFileOffset: 0)
let data = handle.readDataToEndOfFile()
guard let string = String(data: data, encoding: .ascii) else {
XCTFail("Could not read stdout")
return
}
XCTAssertTrue(string.hasSuffix("/which\n"))
}

func test_passthrough_environment() {
Expand Down
13 changes: 0 additions & 13 deletions TestFoundation/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,3 @@ func ensureFiles(_ fileNames: [String]) -> Bool {
}
return result
}

func mkstemp(template: String, body: (FileHandle) throws -> Void) rethrows {
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(template)

try url.withUnsafeFileSystemRepresentation {
switch mkstemp(UnsafeMutablePointer(mutating: $0!)) {
case -1: XCTFail("Could not create temporary file")
case let fd:
defer { url.withUnsafeFileSystemRepresentation { _ = unlink($0!) } }
try body(FileHandle(fileDescriptor: fd, closeOnDealloc: true))
}
}
}