Skip to content

Commit

Permalink
mocks for executor
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Aug 20, 2019
1 parent 09d69b7 commit cce4ec4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/IBM-Swift/BlueSocket",
"state": {
"branch": null,
"revision": "af50c0e67a9fe9050322b93b0cb9044abfc4feaa",
"version": "1.0.47"
"revision": "3cf5de510650d67c551c9e1884fbc9d6e744206b",
"version": "1.0.48"
}
},
{
Expand All @@ -33,8 +33,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "6a79d4687ede7c088fe3e31d9b3452d6e6cd91eb",
"version": "2.5.1"
"revision": "790827800d6af12ca6a8b1dca7a9072606fd2a1e",
"version": "2.7.0"
}
},
{
Expand Down
9 changes: 8 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let package = Package(
.library(name: "ShellKit", targets: ["ShellKit"]),
.library(name: "SSHShell", targets: ["SSHShell"]),
.library(name: "LocalShell", targets: ["LocalShell"]),
.library(name: "CommandKit", targets: ["CommandKit"])
.library(name: "CommandKit", targets: ["CommandKit"]),
.library(name: "ExecutorMocks", targets: ["ExecutorMocks"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.3.0"),
Expand Down Expand Up @@ -54,6 +55,12 @@ let package = Package(
"WebErrorKit"
]
),
.target(
name: "ExecutorMocks",
dependencies: [
"ExecutorKit"
]
),
.testTarget(
name: "LocalShellTests",
dependencies: [
Expand Down
85 changes: 85 additions & 0 deletions Sources/ExecutorMocks/ExecutorMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import Foundation
import ExecutorKit


public class ExecutorMock: Executor {

public let eventLoop = EmbeddedEventLoop()

public var responses: [String: [String]] = [:]

public var failingResponses: [String: Error] = [:]

public func run(bash: String, output: ((String) -> ())?) -> ProcessFuture<String> {
guard let response = responses[bash] else {
guard let error = failingResponses[bash] else {
fatalError("[ShellKit] Missing mock response for:\n\(bash)\n\n")
}
let f: EventLoopFuture<String> = eventLoop.makeFailedFuture(error)
return ProcessFuture<String>(future: f, cancel: { })
}
log(cmd: bash)
var out = ""
for step in response {
output?(step)
out.append(step)
}
let f = eventLoop.makeSucceededFuture(out)
return ProcessFuture<String>(future: f, cancel: { })
}

public func run(command: String, args: [String], output: ((String) -> ())?) -> ProcessFuture<String> {
var cmd = command
for arg in args {
cmd.append(" ")
cmd.append(arg)
}
return run(bash: cmd, output: output)
}

public var existingPaths: [String] = []

public func file(exists path: String) -> EventLoopFuture<Bool> {
guard let _ = existingPaths.lastIndex(of: path) else {
return eventLoop.makeSucceededFuture(false)
}
return eventLoop.makeSucceededFuture(true)
}

public func folder(exists path: String) -> EventLoopFuture<Bool> {
return file(exists: path)
}

public var currentPath = ""

public func cd(path: String) -> EventLoopFuture<Void> {
currentPath = path
return eventLoop.makeSucceededFuture(Void())
}

public func upload(file: String, to path: String) -> EventLoopFuture<Void> {
return upload(data: file.data(using: .utf8)!, to: path)
}

public func upload(data: Data, to path: String) -> EventLoopFuture<Void> {
log(cmd: "Upload \(data.count) bytes to \(path)")
uploaded[path] = data
return eventLoop.makeSucceededFuture(Void())
}

public var uploaded: [String: Data] = [:]

public func upload(string: String, to path: String) -> EventLoopFuture<Void> {
return upload(data: string.data(using: .utf8)!, to: path)
}

}


extension ExecutorMock {

fileprivate func log(cmd: String) {
print("[ShellKit] CMD: \(cmd)")
}

}

0 comments on commit cce4ec4

Please sign in to comment.