Skip to content

SR-11408: Foundation.Process: can invoke run() again without error #2526

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
Oct 27, 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
5 changes: 5 additions & 0 deletions Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ open class Process: NSObject {
// Dispatch the manager thread if it isn't already running
Process.setup()

// Check that the process isnt run more than once
guard hasStarted == false && hasFinished == false else {
throw NSError(domain: NSCocoaErrorDomain, code: NSExecutableLoadError)
}

// Ensure that the launch path is set
guard let launchPath = self.executableURL?.path else {
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError)
Expand Down
27 changes: 17 additions & 10 deletions TestFoundation/TestProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,26 @@ class TestProcess : XCTestCase {
XCTAssertEqual(fm.currentDirectoryPath, cwd)

do {
// Check running the process twice throws an error.
let process = Process()
process.executableURL = xdgTestHelperURL()
process.arguments = ["--exit", "0"]
process.currentDirectoryURL = URL(fileURLWithPath: "/.../_no_such_directory", isDirectory: true)
try process.run()
XCTFail("Executed \(xdgTestHelperURL().path) with invalid currentDirectoryURL")
process.terminate()
XCTAssertNoThrow(try process.run())
process.waitUntilExit()
} catch {
XCTAssertThrowsError(try process.run()) {
let nserror = ($0 as! NSError)
XCTAssertEqual(nserror.domain, NSCocoaErrorDomain)
let code = CocoaError(_nsError: nserror).code
XCTAssertEqual(code, .executableLoad)
}
}

do {
let process = Process()
process.executableURL = xdgTestHelperURL()
process.arguments = ["--exit", "0"]
process.currentDirectoryURL = URL(fileURLWithPath: "/.../_no_such_directory", isDirectory: true)
XCTAssertThrowsError(try process.run())
}
XCTAssertEqual(fm.currentDirectoryPath, cwd)

Expand All @@ -336,11 +347,7 @@ class TestProcess : XCTestCase {
process.executableURL = URL(fileURLWithPath: "/..", isDirectory: false)
process.arguments = []
process.currentDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
try process.run()
XCTFail("Somehow executed a directory!")
process.terminate()
process.waitUntilExit()
} catch {
XCTAssertThrowsError(try process.run())
}
XCTAssertEqual(fm.currentDirectoryPath, cwd)
fm.changeCurrentDirectoryPath(cwd)
Expand Down