Create struct ShellContext:ShellContextType .

This commit is contained in:
Kare Morstol
2015-09-23 03:33:29 +02:00
parent 1bb3b0fcf7
commit a747b13228
2 changed files with 52 additions and 0 deletions
+40
View File
@@ -24,6 +24,46 @@ public protocol ShellContextType {
var currentdirectory: String {get set}
}
public struct ShellContext: ShellContextType {
public var encoding: NSStringEncoding
public var env: [String: String]
public var stdin: NSFileHandle
public var stdout: NSFileHandle
public var stderror: NSFileHandle
/**
The current working directory.
Must be used instead of `run("cd", "...")` because all the `run` commands are executed in a
separate process and changing the directory there will not affect the rest of the Swift script.
*/
public var currentdirectory: String
/** Creates a blank ShellContext. */
public init () {
encoding = NSUTF8StringEncoding
env = [String:String]()
stdin = NSFileHandle.fileHandleWithNullDevice()
stdout = NSFileHandle.fileHandleWithNullDevice()
stderror = NSFileHandle.fileHandleWithNullDevice()
currentdirectory = main.currentdirectory
}
/** Creates a new ShellContext from another ShellContextType. */
public init (_ context: ShellContextType) {
encoding = context.encoding
env = context.env
stdin = context.stdin
stdout = context.stdout
stderror = context.stderror
currentdirectory = context.currentdirectory
}
}
public final class MainShellContext: ShellContextType {
+12
View File
@@ -22,4 +22,16 @@ class Context_Tests: XCTestCase {
XCTAssertEqual( main.run("/bin/pwd"), "/tmp" )
XCTAssertEqual( main.currentdirectory, NSFileManager.defaultManager().currentDirectoryPath )
}
func testBlankShellContext () {
let context = ShellContext()
XCTAssert( context.stdin === NSFileHandle.fileHandleWithNullDevice() )
}
func testCopiedShellContext () {
let context = ShellContext(main)
XCTAssert( context.stdin === main.stdin )
}
}