Skip to content

Commit

Permalink
mocking capability
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Aug 21, 2019
1 parent e3e91b5 commit 2b8a0df
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 33 deletions.
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ let package = Package(
"NIO"
]
),
.testTarget(
name: "ExecutorMocksTests",
dependencies: [
"ExecutorMocks",
"CommandKit"
]
),
.testTarget(
name: "CommandKitTests",
dependencies: [
Expand Down
17 changes: 10 additions & 7 deletions Sources/CommandKit/Commands/File+Cmd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ extension Cmd {
/// - Parameter relativePath: Relative path to be converted into full
public func pwd(relativePath path: String? = nil) -> EventLoopFuture<String> {
if let path = path {
return shell.run(bash: "TMP_P=$(pwd) && cd \(path.quoteEscape) && pwd && cd \"$TMP_P\"").future.trimMap()
return shell.run(bash: "TMP_P=$(pwd) && cd \(path.quoteEscape) && pwd && cd \"$TMP_P\"", output: nil).future.trimMap()
} else {
return shell.run(bash: "pwd").future.trimMap()
return shell.run(bash: "pwd", output: nil).future.trimMap()
}
}

Expand All @@ -31,13 +31,13 @@ extension Cmd {
/// Return a command path if exists
/// - Parameter command: Command
public func which(_ command: String) -> EventLoopFuture<String> {
return shell.run(bash: "which \(command)").future.trimMap()
return shell.run(bash: "which \(command)", output: nil).future.trimMap()
}

/// Check is folder is empty
/// - Parameter path: Command
public func isEmpty(path: String) -> EventLoopFuture<Bool> {
return shell.run(bash: "[ '$(ls -A /path/to/directory)' ] && echo 'not empty' || echo 'empty'").future.map { output in
return shell.run(bash: "[ '$(ls -A /path/to/directory)' ] && echo 'not empty' || echo 'empty'", output: nil).future.map { output in
return output.trimmingCharacters(in: .whitespacesAndNewlines) == "empty"
}
}
Expand All @@ -51,16 +51,19 @@ extension Cmd {
/// Return content of a file as a string
/// - Parameter path: Path to file
public func cat(path: String) -> EventLoopFuture<String> {
return shell.run(bash: "cat \(path.quoteEscape)").future
return shell.run(bash: "cat \(path.quoteEscape)", output: nil).future
}

/// List files in a path
/// - Parameter path: Path to file
/// - Parameter flags: Flags
/// - Parameter output: Future
public func ls(path: String, flags: FlagsConvertible? = nil, output: ((String) -> ())? = nil) -> EventLoopFuture<String> {
let flags = flags?.flags ?? ""
return shell.run(bash: "ls \(flags) \(path.quoteEscape)", output: output).future
var flags = flags?.flags ?? ""
if !flags.isEmpty {
flags.append(contentsOf: " ")
}
return shell.run(bash: "ls \(flags)\(path.quoteEscape)", output: output).future
}

/// Remove flags
Expand Down
2 changes: 1 addition & 1 deletion Sources/CommandKit/Commands/Platform+Cmd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension Cmd {

/// Get target platform
public func platform() -> EventLoopFuture<String> {
return shell.run(bash: Os.command).future.trimMap()
return shell.run(bash: Os.command, output: nil).future.trimMap()
}

}
2 changes: 1 addition & 1 deletion Sources/CommandKit/Commands/System+Cmd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension Cmd {

/// Who Am I (whoami)
public func whoami() -> EventLoopFuture<String> {
return shell.run(bash: "whoami").future.trimMap()
return shell.run(bash: "whoami", output: nil).future.trimMap()
}

}
8 changes: 4 additions & 4 deletions Sources/CommandKit/Commands/Tools+Install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension Install {
/// Install HomeBrew
/// - Note: macOS only
public func brew() -> EventLoopFuture<Void> {
return shell.run(bash: "/usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"").future.void()
return shell.run(bash: "/usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"", output: nil).future.void()
}

/// Install cURL
Expand All @@ -27,9 +27,9 @@ extension Install {
return shell.cmd.os().flatMap { os in
switch os {
case .macOs:
return self.shell.run(bash: "brew install \(name)").future.void()
return self.shell.run(bash: "brew install \(name)", output: nil).future.void()
case .linux:
return self.shell.run(bash: "sudo apt-get install \(name)").future.void()
return self.shell.run(bash: "sudo apt-get install \(name)", output: nil).future.void()
default:
return self.shell.eventLoop.makeFailedFuture(Cmd.CmdError.unsupportedPlatform)
}
Expand All @@ -42,7 +42,7 @@ extension Install {
return shell.cmd.os().flatMap { os in
switch os {
case .macOs:
return self.shell.run(bash: "brew install einstore/homebrew-tap/systemator").future.void()
return self.shell.run(bash: "brew install einstore/homebrew-tap/systemator", output: nil).future.void()
default:
return self.shell.eventLoop.makeFailedFuture(Cmd.CmdError.unsupportedPlatform)
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/CommandKit/Property categories/Cmd.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import ExecutorKit
import WebErrorKit


Expand All @@ -15,16 +16,16 @@ public struct Cmd {

}

let shell: Shell
let shell: MasterExecutor

init(_ shell: Shell) {
init(_ shell: MasterExecutor) {
self.shell = shell
}

}


extension Shell {
extension MasterExecutor {

public var cmd: Cmd {
return Cmd(self)
Expand Down
5 changes: 3 additions & 2 deletions Sources/CommandKit/Property categories/Install.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import WebErrorKit
import ExecutorKit


/// Extension with commands
Expand All @@ -13,9 +14,9 @@ public struct Install {

}

let shell: Shell
let shell: MasterExecutor

init(_ shell: Shell) {
init(_ shell: MasterExecutor) {
self.shell = shell
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/ExecutorKit/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import NIO
/// Executor protocol
public protocol Executor {

var output: ((String) -> ())? { get set }

var eventLoop: EventLoop { get }

/// Run bash command
/// - Parameter bash: bash command
/// - Parameter output: Closure to output console text
Expand Down
25 changes: 17 additions & 8 deletions Sources/ExecutorMocks/ExecutorMock.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import Foundation
import ExecutorKit
import ShellKit


public class ExecutorMock: Executor {
public class ExecutorMock: MasterExecutor {

public var executor: Executor {
return self
}

public var output: ((String) -> ())? = nil

/// Event loop on which all commands execute
public var eventLoop = EmbeddedEventLoop()
public var eventLoop: EventLoop = EmbeddedEventLoop()

/// Responses registered for commands
/// Commads registered for commands
/// - Note: Format is [Command: [Output piece]]
public var responses: [String: [String]] = [:]
public var mockResults: [String: [String]] = [:]

/// Errors for responses that are supposed to fail
public var failingResponses: [String: Error] = [:]
/// Errors for results that are supposed to fail
public var failingMockResults: [String: Error] = [:]

public func run(bash: String, output: ((String) -> ())?) -> ProcessFuture<String> {
guard let response = responses[bash] else {
guard let error = failingResponses[bash] else {
guard let response = mockResults[bash] else {
guard let error = failingMockResults[bash] else {
fatalError("[ShellKit] Missing mock response for:\n\(bash)\n\n")
}
let f: EventLoopFuture<String> = eventLoop.makeFailedFuture(error)
Expand Down Expand Up @@ -80,6 +87,8 @@ public class ExecutorMock: Executor {
return upload(data: string.data(using: .utf8)!, to: path)
}

public init() { }

}


Expand Down
2 changes: 2 additions & 0 deletions Sources/ExecutorMocks/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@_exported import CommandKit
@_exported import ExecutorKit
5 changes: 4 additions & 1 deletion Sources/LocalShell/LocalExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import NIO

public class LocalExecutor: Executor {

public var output: ((String) -> ())? = nil

public enum LocalExecutorError: SerializableWebError {

case processFailed(exit: Int32)
Expand All @@ -22,7 +24,7 @@ public class LocalExecutor: Executor {

}

let eventLoop: EventLoop
public let eventLoop: EventLoop
public private(set) var currentDirectoryPath: String
let identifier: String = UUID().uuidString

Expand Down Expand Up @@ -92,6 +94,7 @@ public class LocalExecutor: Executor {
if let text = data.map({ String(decoding: $0, as: Unicode.UTF8.self) }), !text.isEmpty {
outputText.append(text)
output?(text)
self.output?(text)
print(text, terminator: "")
}
}
Expand Down
4 changes: 1 addition & 3 deletions Sources/SSHShell/Exports.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@_exported import Foundation
@_exported import protocol NIO.EventLoop
@_exported import class NIO.EventLoopFuture
@_exported import ExecutorKit
@_exported import protocol Shout.SSHAuthMethod
@_exported import struct Shout.SSHPassword
@_exported import struct Shout.SSHAgent
Expand Down
5 changes: 4 additions & 1 deletion Sources/SSHShell/SSHExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import NIO
/// SSH executor
public class SSHExecutor: Executor {

let eventLoop: EventLoop
public var output: ((String) -> ())?

public let eventLoop: EventLoop
let ssh: SSH
var sftp: SFTP?

Expand Down Expand Up @@ -38,6 +40,7 @@ public class SSHExecutor: Executor {
outputText += text
self.eventLoop.execute {
output?(text)
self.output?(text)
}
}
if res == 0 {
Expand Down
1 change: 0 additions & 1 deletion Sources/ShellKit/Extensions/File.swift

This file was deleted.

8 changes: 8 additions & 0 deletions Sources/ShellKit/MasterExecutor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ExecutorKit


public protocol MasterExecutor: Executor {

var executor: Executor { get }

}
2 changes: 1 addition & 1 deletion Sources/ShellKit/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NIO


/// Main executor
public class Shell: Executor {
public class Shell: MasterExecutor {

/// Connection type
public struct Connection {
Expand Down
32 changes: 32 additions & 0 deletions Tests/ExecutorMocksTests/Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import XCTest
import ExecutorMocks
import CommandKit
import NIO


class Tests: XCTestCase {

var shell: ExecutorMock!

override func setUp() {
super.setUp()

shell = try! ExecutorMock()
}

func testBasicMock() {
let hu = "huhuhuhu :)"
shell.mockResults["najs woe"] = [hu]
let out = try! shell.run(bash: "najs woe", output: nil).wait()
XCTAssertEqual(out, hu)
}

func testCommandMock() {
shell.mockResults["ls /tmp"] = [
"file1"
]
let out = try! shell.cmd.ls(path: "/tmp").wait()
XCTAssertEqual(out, "file1")
}

}

0 comments on commit 2b8a0df

Please sign in to comment.