Skip to content

NSTask - capture output from commands #348

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 3 commits into from
May 6, 2016
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
6 changes: 6 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@
D3E8D6D31C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */; };
D3E8D6D51C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */; };
D5C40F331CDA1D460005690C /* TestNSOperationQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */; };
D51239DF1CD9DA0800D433EE /* CFSocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88E01BBC9B0300234F36 /* CFSocket.c */; };
D512D17C1CD883F00032E6A5 /* TestNSFileHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */; };
E1A03F361C4828650023AF4D /* PropertyList-1.0.dtd in Resources */ = {isa = PBXBuildFile; fileRef = E1A03F351C4828650023AF4D /* PropertyList-1.0.dtd */; };
E1A03F381C482C730023AF4D /* NSXMLDTDTestData.xml in Resources */ = {isa = PBXBuildFile; fileRef = E1A03F371C482C730023AF4D /* NSXMLDTDTestData.xml */; };
E1A3726F1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml in Resources */ = {isa = PBXBuildFile; fileRef = E1A3726E1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml */; };
Expand Down Expand Up @@ -648,6 +650,7 @@
D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-EdgeInsetsTest.plist"; sourceTree = "<group>"; };
D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-RectTest.plist"; sourceTree = "<group>"; };
D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOperationQueue.swift; sourceTree = "<group>"; };
D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSFileHandle.swift; sourceTree = "<group>"; };
D834F9931C31C4060023812A /* TestNSOrderedSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOrderedSet.swift; sourceTree = "<group>"; };
DCDBB8321C1768AC00313299 /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
E19E17DB1C2225930023AF4D /* TestNSXMLDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSXMLDocument.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1219,6 +1222,7 @@
5B6F17961C48631C00935030 /* TestUtils.swift */,
E19E17DB1C2225930023AF4D /* TestNSXMLDocument.swift */,
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */,
D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */,
);
name = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1907,6 +1911,7 @@
5B7C8A921BEA7FEC00C5B690 /* CFXMLInputStream.c in Sources */,
5B7C8AAA1BEA800D00C5B690 /* CFBurstTrie.c in Sources */,
5B7C8A9E1BEA7FF900C5B690 /* CFPlugIn_Instance.c in Sources */,
D51239DF1CD9DA0800D433EE /* CFSocket.c in Sources */,
5B7C8A801BEA7FCE00C5B690 /* CFDictionary.c in Sources */,
5BC2C00F1C07833200CC214E /* CFStringTransform.c in Sources */,
5B7C8AAE1BEA800D00C5B690 /* CFStringScanner.c in Sources */,
Expand Down Expand Up @@ -1997,6 +2002,7 @@
5B13B3401C582D4C00651CE2 /* TestNSRange.swift in Sources */,
5B13B3371C582D4C00651CE2 /* TestNSNotificationCenter.swift in Sources */,
5B13B3251C582D4700651CE2 /* main.swift in Sources */,
D512D17C1CD883F00032E6A5 /* TestNSFileHandle.swift in Sources */,
5B13B33A1C582D4C00651CE2 /* TestNSNumber.swift in Sources */,
5B13B3521C582D4C00651CE2 /* TestNSValue.swift in Sources */,
5B13B3311C582D4C00651CE2 /* TestNSIndexPath.swift in Sources */,
Expand Down
40 changes: 22 additions & 18 deletions Foundation/NSFileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ public class NSFileHandle : NSObject, NSSecureCoding {
internal var _closeOnDealloc: Bool
internal var _closed: Bool = false

/*@NSCopying*/ public var availableData: NSData {
NSUnimplemented()
public var availableData: NSData {
return _readDataOfLength(Int.max, untilEOF: false)
}

public func readDataToEndOfFile() -> NSData {
return readDataOfLength(Int.max)
}

public func readDataOfLength(_ length: Int) -> NSData {
return _readDataOfLength(length, untilEOF: true)
}

internal func _readDataOfLength(_ length: Int, untilEOF: Bool) -> NSData {
var statbuf = stat()
var dynamicBuffer: UnsafeMutablePointer<UInt8>? = nil
var total = 0
Expand All @@ -49,21 +53,21 @@ public class NSFileHandle : NSObject, NSSecureCoding {
if dynamicBuffer == nil {
fatalError("unable to allocate backing buffer")
}
let amtRead = read(_fd, dynamicBuffer!.advanced(by: total), amountToRead)
if 0 > amtRead {
free(dynamicBuffer)
fatalError("read failure")
}
if 0 == amtRead {
break // EOF
}
total += amtRead
remaining -= amtRead
if total == length {
break // We read everything the client asked for.
}
}
let amtRead = read(_fd, dynamicBuffer!.advanced(by: total), amountToRead)
if 0 > amtRead {
free(dynamicBuffer)
fatalError("read failure")
}
if 0 == amtRead {
break // EOF
}

total += amtRead
remaining -= amtRead

if total == length || !untilEOF {
break // We read everything the client asked for.
}
}
} else {
Expand Down
83 changes: 75 additions & 8 deletions Foundation/NSTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,24 @@ public class NSTask : NSObject {
public var currentDirectoryPath: String = NSFileManager.defaultInstance.currentDirectoryPath

// standard I/O channels; could be either an NSFileHandle or an NSPipe
public var standardInput: AnyObject?
public var standardOutput: AnyObject?
public var standardError: AnyObject?
public var standardInput: AnyObject? {
willSet {
precondition(newValue is NSPipe || newValue is NSFileHandle,
"standardInput must be either NSPipe or NSFileHandle")
}
}
public var standardOutput: AnyObject? {
willSet {
precondition(newValue is NSPipe || newValue is NSFileHandle,
"standardOutput must be either NSPipe or NSFileHandle")
}
}
public var standardError: AnyObject? {
willSet {
precondition(newValue is NSPipe || newValue is NSFileHandle,
"standardError must be either NSPipe or NSFileHandle")
}
}

private var runLoopSourceContext : CFRunLoopSourceContext?
private var runLoopSource : CFRunLoopSource?
Expand Down Expand Up @@ -284,16 +299,68 @@ public class NSTask : NSObject {

let source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0)
CFRunLoopAddSource(managerThreadRunLoop?._cfRunLoop, source, kCFRunLoopDefaultMode)


// file_actions
#if os(OSX) || os(iOS)
var fileActions: posix_spawn_file_actions_t? = nil
#else
var fileActions: posix_spawn_file_actions_t = posix_spawn_file_actions_t()
#endif
posix_spawn_file_actions_init(&fileActions)

switch self.standardInput {
case let pipe as NSPipe:
posix_spawn_file_actions_adddup2(&fileActions, pipe.fileHandleForReading.fileDescriptor, STDIN_FILENO)
posix_spawn_file_actions_addclose(&fileActions, pipe.fileHandleForWriting.fileDescriptor)
case let handle as NSFileHandle:
posix_spawn_file_actions_adddup2(&fileActions, handle.fileDescriptor, STDIN_FILENO)
posix_spawn_file_actions_addclose(&fileActions, handle.fileDescriptor)
default: break
}

switch self.standardOutput {
case let pipe as NSPipe:
posix_spawn_file_actions_adddup2(&fileActions, pipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO)
posix_spawn_file_actions_addclose(&fileActions, pipe.fileHandleForWriting.fileDescriptor)
case let handle as NSFileHandle:
posix_spawn_file_actions_adddup2(&fileActions, handle.fileDescriptor, STDOUT_FILENO)
posix_spawn_file_actions_addclose(&fileActions, handle.fileDescriptor)
default: break
}

switch self.standardError {
case let pipe as NSPipe:
posix_spawn_file_actions_adddup2(&fileActions, pipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)
posix_spawn_file_actions_addclose(&fileActions, pipe.fileHandleForWriting.fileDescriptor)
case let handle as NSFileHandle:
posix_spawn_file_actions_adddup2(&fileActions, handle.fileDescriptor, STDERR_FILENO)
posix_spawn_file_actions_addclose(&fileActions, handle.fileDescriptor)
default: break
}

// Launch

var pid = pid_t()
let status = posix_spawn(&pid, launchPath, nil, nil, argv, envp)

let status = posix_spawn(&pid, launchPath, &fileActions, nil, argv, envp)

// cleanup file_actions
posix_spawn_file_actions_destroy(&fileActions)

// Close the write end of the input and output pipes.
if let pipe = self.standardInput as? NSPipe {
pipe.fileHandleForReading.closeFile()
}
if let pipe = self.standardOutput as? NSPipe {
pipe.fileHandleForWriting.closeFile()
}
if let pipe = self.standardError as? NSPipe {
pipe.fileHandleForWriting.closeFile()
}

guard status == 0 else {
fatalError()
}

close(taskSocketPair[1])

self.runLoop = NSRunLoop.currentRunLoop()
Expand Down
41 changes: 41 additions & 0 deletions TestFoundation/TestNSFileHandle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif

class TestNSFileHandle : XCTestCase {
static var allTests : [(String, TestNSFileHandle -> () throws -> ())] {
return [
("test_pipe", test_pipe),
]
}

func test_pipe() {
let pipe = NSPipe()
let inputs = ["Hello", "world", "🐶"]

for input in inputs {
let inputData = input.data(using: NSUTF8StringEncoding)!

// write onto pipe
pipe.fileHandleForWriting.writeData(inputData)

let outputData = pipe.fileHandleForReading.availableData
let output = String(data: outputData, encoding: NSUTF8StringEncoding)

XCTAssertEqual(output, input)
}
}
}
123 changes: 118 additions & 5 deletions TestFoundation/TestNSTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ import CoreFoundation

class TestNSTask : XCTestCase {
static var allTests: [(String, TestNSTask -> () throws -> Void)] {
return [("test_exit0" , test_exit0),
("test_exit1" , test_exit1),
("test_exit100" , test_exit100),
("test_sleep2", test_sleep2),
("test_sleep2_exit1", test_sleep2_exit1)]
return [
("test_exit0" , test_exit0),
("test_exit1" , test_exit1),
("test_exit100" , test_exit100),
("test_sleep2", test_sleep2),
("test_sleep2_exit1", test_sleep2_exit1),
("test_pipe_stdin", test_pipe_stdin),
("test_pipe_stdout", test_pipe_stdout),
("test_pipe_stderr", test_pipe_stderr),
("test_file_stdout", test_file_stdout),
]
}

func test_exit0() {
Expand Down Expand Up @@ -84,4 +90,111 @@ class TestNSTask : XCTestCase {
task.waitUntilExit()
XCTAssertEqual(task.terminationStatus, 1)
}


func test_pipe_stdin() {
let task = NSTask()

task.launchPath = "/bin/cat"

let outputPipe = NSPipe()
task.standardOutput = outputPipe

let inputPipe = NSPipe()
task.standardInput = inputPipe

task.launch()

inputPipe.fileHandleForWriting.writeData("Hello, 🐶.\n".data(using: NSUTF8StringEncoding)!)

// Close the input pipe to send EOF to cat.
inputPipe.fileHandleForWriting.closeFile()

task.waitUntilExit()
XCTAssertEqual(task.terminationStatus, 0)

let data = outputPipe.fileHandleForReading.availableData
guard let string = String(data: data, encoding: NSUTF8StringEncoding) else {
XCTFail("Could not read stdout")
return
}
XCTAssertEqual(string, "Hello, 🐶.\n")
}

func test_pipe_stdout() {
let task = NSTask()

task.launchPath = "/usr/bin/which"
task.arguments = ["which"]

let pipe = NSPipe()
task.standardOutput = pipe

task.launch()
task.waitUntilExit()
XCTAssertEqual(task.terminationStatus, 0)

let data = pipe.fileHandleForReading.availableData
guard let string = String(data: data, encoding: NSASCIIStringEncoding) else {
XCTFail("Could not read stdout")
return
}
XCTAssertEqual(string, "/usr/bin/which\n")
}

func test_pipe_stderr() {
let task = NSTask()

task.launchPath = "/bin/cat"
task.arguments = ["invalid_file_name"]

let errorPipe = NSPipe()
task.standardError = errorPipe

task.launch()
task.waitUntilExit()
XCTAssertEqual(task.terminationStatus, 1)

let data = errorPipe.fileHandleForReading.availableData
guard let string = String(data: data, encoding: NSASCIIStringEncoding) else {
XCTFail("Could not read stdout")
return
}
XCTAssertEqual(string, "cat: invalid_file_name: No such file or directory\n")
}

func test_file_stdout() {
let task = NSTask()

task.launchPath = "/usr/bin/which"
task.arguments = ["which"]

mkstemp(template: "TestNSTask.XXXXXX") { handle in
task.standardOutput = handle

task.launch()
task.waitUntilExit()
XCTAssertEqual(task.terminationStatus, 0)

handle.seekToFileOffset(0)
let data = handle.readDataToEndOfFile()
guard let string = String(data: data, encoding: NSASCIIStringEncoding) else {
XCTFail("Could not read stdout")
return
}
XCTAssertEqual(string, "/usr/bin/which\n")
}
}
}

private func mkstemp(template: String, body: @noescape (NSFileHandle) throws -> Void) rethrows {
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("TestNSTask.XXXXXX")!
var buffer = [Int8](repeating: 0, count: Int(PATH_MAX))
url.getFileSystemRepresentation(&buffer, maxLength: buffer.count)
switch mkstemp(&buffer) {
case -1: XCTFail("Could not create temporary file")
case let fd:
defer { unlink(&buffer) }
try body(NSFileHandle(fileDescriptor: fd, closeOnDealloc: true))
}
}
3 changes: 2 additions & 1 deletion TestFoundation/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ XCTMain([
testCase(TestNSScanner.allTests),
testCase(TestNSSet.allTests),
testCase(TestNSString.allTests),
// testCase(TestNSTask.allTests),
// testCase(TestNSThread.allTests),
testCase(TestNSTask.allTests),
testCase(TestNSTimer.allTests),
testCase(TestNSTimeZone.allTests),
testCase(TestNSURL.allTests),
Expand All @@ -75,4 +75,5 @@ XCTMain([
testCase(TestNSXMLParser.allTests),
testCase(TestNSXMLDocument.allTests),
testCase(TestNSAttributedString.allTests),
testCase(TestNSFileHandle.allTests),
])