diff --git a/SwiftShell/Context.swift b/SwiftShell/Context.swift index 49ee4da..cca821b 100644 --- a/SwiftShell/Context.swift +++ b/SwiftShell/Context.swift @@ -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 { diff --git a/SwiftShellTests/Context_Tests.swift b/SwiftShellTests/Context_Tests.swift index bcf9984..89cc890 100644 --- a/SwiftShellTests/Context_Tests.swift +++ b/SwiftShellTests/Context_Tests.swift @@ -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 ) + } }