Skip to content

Commit ead2ab8

Browse files
authored
Merge pull request #2526 from spevans/pr_sr_11408
2 parents 6d9af20 + ed168b0 commit ead2ab8

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

Foundation/Process.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ open class Process: NSObject {
409409
// Dispatch the manager thread if it isn't already running
410410
Process.setup()
411411

412+
// Check that the process isnt run more than once
413+
guard hasStarted == false && hasFinished == false else {
414+
throw NSError(domain: NSCocoaErrorDomain, code: NSExecutableLoadError)
415+
}
416+
412417
// Ensure that the launch path is set
413418
guard let launchPath = self.executableURL?.path else {
414419
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError)

TestFoundation/TestProcess.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,26 @@ class TestProcess : XCTestCase {
333333
XCTAssertEqual(fm.currentDirectoryPath, cwd)
334334

335335
do {
336+
// Check running the process twice throws an error.
336337
let process = Process()
337338
process.executableURL = xdgTestHelperURL()
338339
process.arguments = ["--exit", "0"]
339-
process.currentDirectoryURL = URL(fileURLWithPath: "/.../_no_such_directory", isDirectory: true)
340-
try process.run()
341-
XCTFail("Executed \(xdgTestHelperURL().path) with invalid currentDirectoryURL")
342-
process.terminate()
340+
XCTAssertNoThrow(try process.run())
343341
process.waitUntilExit()
344-
} catch {
342+
XCTAssertThrowsError(try process.run()) {
343+
let nserror = ($0 as! NSError)
344+
XCTAssertEqual(nserror.domain, NSCocoaErrorDomain)
345+
let code = CocoaError(_nsError: nserror).code
346+
XCTAssertEqual(code, .executableLoad)
347+
}
348+
}
349+
350+
do {
351+
let process = Process()
352+
process.executableURL = xdgTestHelperURL()
353+
process.arguments = ["--exit", "0"]
354+
process.currentDirectoryURL = URL(fileURLWithPath: "/.../_no_such_directory", isDirectory: true)
355+
XCTAssertThrowsError(try process.run())
345356
}
346357
XCTAssertEqual(fm.currentDirectoryPath, cwd)
347358

@@ -350,11 +361,7 @@ class TestProcess : XCTestCase {
350361
process.executableURL = URL(fileURLWithPath: "/..", isDirectory: false)
351362
process.arguments = []
352363
process.currentDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
353-
try process.run()
354-
XCTFail("Somehow executed a directory!")
355-
process.terminate()
356-
process.waitUntilExit()
357-
} catch {
364+
XCTAssertThrowsError(try process.run())
358365
}
359366
XCTAssertEqual(fm.currentDirectoryPath, cwd)
360367
fm.changeCurrentDirectoryPath(cwd)

0 commit comments

Comments
 (0)