From d4c2bbed89207c5b5b21fca3bdbb045932babc34 Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Sun, 4 Oct 2015 19:22:23 +0200 Subject: [PATCH] Add unit tests for running commands from custom context. --- SwiftShellTests/Context_Tests.swift | 35 ++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/SwiftShellTests/Context_Tests.swift b/SwiftShellTests/Context_Tests.swift index 89cc890..38bdfe7 100644 --- a/SwiftShellTests/Context_Tests.swift +++ b/SwiftShellTests/Context_Tests.swift @@ -9,7 +9,7 @@ import XCTest import SwiftShell -class Context_Tests: XCTestCase { +class MainContext_Tests: XCTestCase { func testCurrentDirectory_IsCurrentDirectory () { XCTAssertEqual( main.currentdirectory, NSFileManager.defaultManager().currentDirectoryPath ) @@ -22,6 +22,9 @@ class Context_Tests: XCTestCase { XCTAssertEqual( main.run("/bin/pwd"), "/tmp" ) XCTAssertEqual( main.currentdirectory, NSFileManager.defaultManager().currentDirectoryPath ) } +} + +class ShellContext_Tests: XCTestCase { func testBlankShellContext () { let context = ShellContext() @@ -34,4 +37,34 @@ class Context_Tests: XCTestCase { XCTAssert( context.stdin === main.stdin ) } + + func testRunCommand () { + let context = ShellContext() + + XCTAssertEqual(context.run("echo", "one"), "one") + } + + func testRunAsyncCommand () { + let context = ShellContext() + let task = context.runAsync("echo", "one") + + XCTAssertEqual(task.stdout.read(), "one\n") + } + + func testRunAndPrintCommand () { + var context = ShellContext() + + AssertNoThrow { + try context.runAndPrint("echo", "one") // sent to null + } + + let outputpipe = NSPipe() + context.stdout = outputpipe.fileHandleForWriting + let output = outputpipe.fileHandleForReading + + AssertNoThrow { + try context.runAndPrint("echo", "two") + } + XCTAssertEqual(output.readSome(), "two\n") + } }