diff --git a/Foundation/Process.swift b/Foundation/Process.swift index 849bfd90d1..3ed63df07c 100644 --- a/Foundation/Process.swift +++ b/Foundation/Process.swift @@ -164,13 +164,40 @@ open class Process: NSObject { } + @available(*, deprecated) + open var launchPath: String? { + set { + guard let newValue = newValue else { + self.executableURL = nil + return + } + + self.executableURL = URL(fileURLWithPath: newValue) + } + + get { + return self.executableURL?.path + } + } + + @available(*, deprecated) + open var currentDirectoryPath: String { + set { + self.currentDirectoryURL = URL(fileURLWithPath: newValue) + } + + get { + return self.currentDirectoryURL.path + } + } + // these methods can only be set before a launch - open var launchPath: String? open var arguments: [String]? + open var executableURL: URL? + open var currentDirectoryURL: URL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath) + open var environment: [String : String]? // if not set, use current - open var currentDirectoryPath: String = FileManager.default.currentDirectoryPath - // standard I/O channels; could be either a FileHandle or a Pipe open var standardInput: Any? { willSet { @@ -209,7 +236,7 @@ open class Process: NSObject { // Ensure that the launch path is set - guard let launchPath = self.launchPath else { + guard let launchPath = self.executableURL?.path else { fatalError() } @@ -389,7 +416,7 @@ open class Process: NSObject { let fileManager = FileManager() let previousDirectoryPath = fileManager.currentDirectoryPath - if !fileManager.changeCurrentDirectoryPath(currentDirectoryPath) { + if !fileManager.changeCurrentDirectoryPath(currentDirectoryURL.path) { // Foundation throws an NSException when changing the working directory fails, // and unfortunately launch() is not marked `throws`, so we get away with a // fatalError. @@ -485,7 +512,7 @@ extension Process { // convenience; create and launch open class func launchedProcess(launchPath path: String, arguments: [String]) -> Process { let process = Process() - process.launchPath = path + process.executableURL = URL(fileURLWithPath: path) process.arguments = arguments process.launch() diff --git a/TestFoundation/TestProcess.swift b/TestFoundation/TestProcess.swift index 8ebc3cef00..500754cf12 100644 --- a/TestFoundation/TestProcess.swift +++ b/TestFoundation/TestProcess.swift @@ -45,7 +45,7 @@ class TestProcess : XCTestCase { let process = Process() - process.launchPath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: "/bin/bash") process.arguments = ["-c", "exit 0"] process.launch() @@ -58,7 +58,7 @@ class TestProcess : XCTestCase { let process = Process() - process.launchPath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: "/bin/bash") process.arguments = ["-c", "exit 1"] process.launch() @@ -71,7 +71,7 @@ class TestProcess : XCTestCase { let process = Process() - process.launchPath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: "/bin/bash") process.arguments = ["-c", "exit 100"] process.launch() @@ -84,7 +84,7 @@ class TestProcess : XCTestCase { let process = Process() - process.launchPath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: "/bin/bash") process.arguments = ["-c", "sleep 2"] process.launch() @@ -97,7 +97,7 @@ class TestProcess : XCTestCase { let process = Process() - process.launchPath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: "/bin/bash") process.arguments = ["-c", "sleep 2; exit 1"] process.launch() @@ -109,7 +109,7 @@ class TestProcess : XCTestCase { func test_terminationReason_uncaughtSignal() { let process = Process() - process.launchPath = "/bin/bash" + process.executableURL = URL(fileURLWithPath: "/bin/bash") process.arguments = ["-c", "kill -TERM $$"] process.launch() @@ -121,7 +121,7 @@ class TestProcess : XCTestCase { func test_pipe_stdin() { let process = Process() - process.launchPath = "/bin/cat" + process.executableURL = URL(fileURLWithPath: "/bin/cat") let outputPipe = Pipe() process.standardOutput = outputPipe @@ -150,7 +150,7 @@ class TestProcess : XCTestCase { func test_pipe_stdout() { let process = Process() - process.launchPath = "/usr/bin/which" + process.executableURL = URL(fileURLWithPath: "/usr/bin/which") process.arguments = ["which"] let pipe = Pipe() @@ -171,7 +171,7 @@ class TestProcess : XCTestCase { func test_pipe_stderr() { let process = Process() - process.launchPath = "/bin/cat" + process.executableURL = URL(fileURLWithPath: "/bin/cat") process.arguments = ["invalid_file_name"] let errorPipe = Pipe() @@ -193,7 +193,7 @@ class TestProcess : XCTestCase { func test_pipe_stdout_and_stderr_same_pipe() { let process = Process() - process.launchPath = "/bin/cat" + process.executableURL = URL(fileURLWithPath: "/bin/cat") process.arguments = ["invalid_file_name"] let pipe = Pipe() @@ -223,7 +223,7 @@ class TestProcess : XCTestCase { func test_file_stdout() { let process = Process() - process.launchPath = "/usr/bin/which" + process.executableURL = URL(fileURLWithPath: "/usr/bin/which") process.arguments = ["which"] mkstemp(template: "TestProcess.XXXXXX") { handle in @@ -314,7 +314,8 @@ private func runTask(_ arguments: [String], environment: [String: String]? = nil let process = Process() var arguments = arguments - process.launchPath = arguments.removeFirst() + + process.executableURL = URL(fileURLWithPath: arguments.removeFirst()) process.arguments = arguments // Darwin Foundation doesnt allow .environment to be set to nil although the documentation // says it is an optional. https://developer.apple.com/documentation/foundation/process/1409412-environment @@ -323,7 +324,7 @@ private func runTask(_ arguments: [String], environment: [String: String]? = nil } if let directoryPath = currentDirectoryPath { - process.currentDirectoryPath = directoryPath + process.currentDirectoryURL = URL(fileURLWithPath: directoryPath) } let stdoutPipe = Pipe()