Skip to content

Commit 6d9ba3a

Browse files
pvieitoparkera
authored andcommitted
Added URL API to Foundation.Process (#1473)
* Added URL API to Foundation.Process * Added test for Process URL API
1 parent 6682092 commit 6d9ba3a

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

Foundation/Process.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ open class Process: NSObject {
114114
static var done = false
115115
static let lock = NSLock()
116116
}
117+
117118
Once.lock.synchronized {
118119
if !Once.done {
119120
let thread = Thread {
@@ -164,26 +165,52 @@ open class Process: NSObject {
164165

165166
}
166167

167-
// these methods can only be set before a launch
168+
// These methods can only be set before a launch.
169+
168170
open var launchPath: String?
169171
open var arguments: [String]?
170172
open var environment: [String : String]? // if not set, use current
171173

172174
open var currentDirectoryPath: String = FileManager.default.currentDirectoryPath
173175

174-
// standard I/O channels; could be either a FileHandle or a Pipe
176+
open var executableURL: URL? {
177+
get {
178+
guard let launchPath = self.launchPath else {
179+
return nil
180+
}
181+
182+
return URL(fileURLWithPath: launchPath)
183+
}
184+
set {
185+
self.launchPath = newValue?.path
186+
}
187+
}
188+
189+
open var currentDirectoryURL: URL {
190+
get {
191+
return URL(fileURLWithPath: self.currentDirectoryPath)
192+
}
193+
set {
194+
self.currentDirectoryPath = newValue.path
195+
}
196+
}
197+
198+
// Standard I/O channels; could be either a FileHandle or a Pipe
199+
175200
open var standardInput: Any? {
176201
willSet {
177202
precondition(newValue is Pipe || newValue is FileHandle,
178203
"standardInput must be either Pipe or FileHandle")
179204
}
180205
}
206+
181207
open var standardOutput: Any? {
182208
willSet {
183209
precondition(newValue is Pipe || newValue is FileHandle,
184210
"standardOutput must be either Pipe or FileHandle")
185211
}
186212
}
213+
187214
open var standardError: Any? {
188215
willSet {
189216
precondition(newValue is Pipe || newValue is FileHandle,
@@ -198,7 +225,8 @@ open class Process: NSObject {
198225

199226
private var processLaunchedCondition = NSCondition()
200227

201-
// actions
228+
// Actions
229+
202230
open func launch() {
203231

204232
self.processLaunchedCondition.lock()

TestFoundation/TestProcess.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ class TestProcess : XCTestCase {
4545

4646
let process = Process()
4747

48-
process.launchPath = "/bin/bash"
49-
process.arguments = ["-c", "exit 0"]
48+
let executablePath = "/bin/bash"
49+
process.executableURL = URL(fileURLWithPath: executablePath)
50+
51+
XCTAssertEqual(executablePath, process.launchPath)
5052

53+
process.arguments = ["-c", "exit 0"]
5154
process.launch()
5255
process.waitUntilExit()
56+
5357
XCTAssertEqual(process.terminationStatus, 0)
5458
XCTAssertEqual(process.terminationReason, .exit)
5559
}

0 commit comments

Comments
 (0)