From 93809fa6949b6da97b5384e115e7d11e327e110f Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Wed, 7 Oct 2015 18:48:04 +0200 Subject: [PATCH] Enable strings to run commands using themselves as standard input. --- SwiftShell/General/String.swift | 12 ++++++++++++ SwiftShellTests/General/String_Tests.swift | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/SwiftShell/General/String.swift b/SwiftShell/General/String.swift index 4000fc3..2d9dd89 100644 --- a/SwiftShell/General/String.swift +++ b/SwiftShell/General/String.swift @@ -7,3 +7,15 @@ import Foundation +/** Let Strings run commands using itself as stdin. */ +extension String: ShellRunnable { + public var shellcontext: ShellContextType { + var context = ShellContext(main) + let (writer,reader) = streams() + + writer.write(self) + writer.close() + context.stdin = reader + return context + } +} diff --git a/SwiftShellTests/General/String_Tests.swift b/SwiftShellTests/General/String_Tests.swift index 718f31b..ed2d93c 100644 --- a/SwiftShellTests/General/String_Tests.swift +++ b/SwiftShellTests/General/String_Tests.swift @@ -11,4 +11,8 @@ import SwiftShell class String_Tests: XCTestCase { + func testRunCommands () { + XCTAssertEqual("one two".run("wc","-w"), "2") + XCTAssertEqual("one".runAsync("cat").stdout.read(), "one") + } }