Skip to content

Added URL API to Foundation.Process #1473

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 2 commits into from
Mar 22, 2018
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
34 changes: 31 additions & 3 deletions Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ open class Process: NSObject {
static var done = false
static let lock = NSLock()
}

Once.lock.synchronized {
if !Once.done {
let thread = Thread {
Expand Down Expand Up @@ -164,26 +165,52 @@ open class Process: NSObject {

}

// these methods can only be set before a launch
// These methods can only be set before a launch.

open var launchPath: String?
open var arguments: [String]?
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 executableURL: URL? {
get {
guard let launchPath = self.launchPath else {
return nil
}

return URL(fileURLWithPath: launchPath)
}
set {
self.launchPath = newValue?.path
}
}

open var currentDirectoryURL: URL {
get {
return URL(fileURLWithPath: self.currentDirectoryPath)
}
set {
self.currentDirectoryPath = newValue.path
}
}

// Standard I/O channels; could be either a FileHandle or a Pipe

open var standardInput: Any? {
willSet {
precondition(newValue is Pipe || newValue is FileHandle,
"standardInput must be either Pipe or FileHandle")
}
}

open var standardOutput: Any? {
willSet {
precondition(newValue is Pipe || newValue is FileHandle,
"standardOutput must be either Pipe or FileHandle")
}
}

open var standardError: Any? {
willSet {
precondition(newValue is Pipe || newValue is FileHandle,
Expand All @@ -198,7 +225,8 @@ open class Process: NSObject {

private var processLaunchedCondition = NSCondition()

// actions
// Actions

open func launch() {

self.processLaunchedCondition.lock()
Expand Down
8 changes: 6 additions & 2 deletions TestFoundation/TestProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ class TestProcess : XCTestCase {

let process = Process()

process.launchPath = "/bin/bash"
process.arguments = ["-c", "exit 0"]
let executablePath = "/bin/bash"
process.executableURL = URL(fileURLWithPath: executablePath)

XCTAssertEqual(executablePath, process.launchPath)

process.arguments = ["-c", "exit 0"]
process.launch()
process.waitUntilExit()

XCTAssertEqual(process.terminationStatus, 0)
XCTAssertEqual(process.terminationReason, .exit)
}
Expand Down