Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

Commit 2b8a0df

Browse files
committed
mocking capability
1 parent e3e91b5 commit 2b8a0df

File tree

17 files changed

+103
-33
lines changed

17 files changed

+103
-33
lines changed

Package.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ let package = Package(
6868
"NIO"
6969
]
7070
),
71+
.testTarget(
72+
name: "ExecutorMocksTests",
73+
dependencies: [
74+
"ExecutorMocks",
75+
"CommandKit"
76+
]
77+
),
7178
.testTarget(
7279
name: "CommandKitTests",
7380
dependencies: [

Sources/CommandKit/Commands/File+Cmd.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ extension Cmd {
99
/// - Parameter relativePath: Relative path to be converted into full
1010
public func pwd(relativePath path: String? = nil) -> EventLoopFuture<String> {
1111
if let path = path {
12-
return shell.run(bash: "TMP_P=$(pwd) && cd \(path.quoteEscape) && pwd && cd \"$TMP_P\"").future.trimMap()
12+
return shell.run(bash: "TMP_P=$(pwd) && cd \(path.quoteEscape) && pwd && cd \"$TMP_P\"", output: nil).future.trimMap()
1313
} else {
14-
return shell.run(bash: "pwd").future.trimMap()
14+
return shell.run(bash: "pwd", output: nil).future.trimMap()
1515
}
1616
}
1717

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

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

5757
/// List files in a path
5858
/// - Parameter path: Path to file
5959
/// - Parameter flags: Flags
6060
/// - Parameter output: Future
6161
public func ls(path: String, flags: FlagsConvertible? = nil, output: ((String) -> ())? = nil) -> EventLoopFuture<String> {
62-
let flags = flags?.flags ?? ""
63-
return shell.run(bash: "ls \(flags) \(path.quoteEscape)", output: output).future
62+
var flags = flags?.flags ?? ""
63+
if !flags.isEmpty {
64+
flags.append(contentsOf: " ")
65+
}
66+
return shell.run(bash: "ls \(flags)\(path.quoteEscape)", output: output).future
6467
}
6568

6669
/// Remove flags

Sources/CommandKit/Commands/Platform+Cmd.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension Cmd {
4949

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

5555
}

Sources/CommandKit/Commands/System+Cmd.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extension Cmd {
66

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

1212
}

Sources/CommandKit/Commands/Tools+Install.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extension Install {
66
/// Install HomeBrew
77
/// - Note: macOS only
88
public func brew() -> EventLoopFuture<Void> {
9-
return shell.run(bash: "/usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"").future.void()
9+
return shell.run(bash: "/usr/bin/ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"", output: nil).future.void()
1010
}
1111

1212
/// Install cURL
@@ -27,9 +27,9 @@ extension Install {
2727
return shell.cmd.os().flatMap { os in
2828
switch os {
2929
case .macOs:
30-
return self.shell.run(bash: "brew install \(name)").future.void()
30+
return self.shell.run(bash: "brew install \(name)", output: nil).future.void()
3131
case .linux:
32-
return self.shell.run(bash: "sudo apt-get install \(name)").future.void()
32+
return self.shell.run(bash: "sudo apt-get install \(name)", output: nil).future.void()
3333
default:
3434
return self.shell.eventLoop.makeFailedFuture(Cmd.CmdError.unsupportedPlatform)
3535
}
@@ -42,7 +42,7 @@ extension Install {
4242
return shell.cmd.os().flatMap { os in
4343
switch os {
4444
case .macOs:
45-
return self.shell.run(bash: "brew install einstore/homebrew-tap/systemator").future.void()
45+
return self.shell.run(bash: "brew install einstore/homebrew-tap/systemator", output: nil).future.void()
4646
default:
4747
return self.shell.eventLoop.makeFailedFuture(Cmd.CmdError.unsupportedPlatform)
4848
}

Sources/CommandKit/Property categories/Cmd.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import ExecutorKit
23
import WebErrorKit
34

45

@@ -15,16 +16,16 @@ public struct Cmd {
1516

1617
}
1718

18-
let shell: Shell
19+
let shell: MasterExecutor
1920

20-
init(_ shell: Shell) {
21+
init(_ shell: MasterExecutor) {
2122
self.shell = shell
2223
}
2324

2425
}
2526

2627

27-
extension Shell {
28+
extension MasterExecutor {
2829

2930
public var cmd: Cmd {
3031
return Cmd(self)

Sources/CommandKit/Property categories/Install.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import WebErrorKit
2+
import ExecutorKit
23

34

45
/// Extension with commands
@@ -13,9 +14,9 @@ public struct Install {
1314

1415
}
1516

16-
let shell: Shell
17+
let shell: MasterExecutor
1718

18-
init(_ shell: Shell) {
19+
init(_ shell: MasterExecutor) {
1920
self.shell = shell
2021
}
2122

Sources/ExecutorKit/Executor.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import NIO
55
/// Executor protocol
66
public protocol Executor {
77

8+
var output: ((String) -> ())? { get set }
9+
10+
var eventLoop: EventLoop { get }
11+
812
/// Run bash command
913
/// - Parameter bash: bash command
1014
/// - Parameter output: Closure to output console text

Sources/ExecutorMocks/ExecutorMock.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
import Foundation
22
import ExecutorKit
3+
import ShellKit
34

45

5-
public class ExecutorMock: Executor {
6+
public class ExecutorMock: MasterExecutor {
7+
8+
public var executor: Executor {
9+
return self
10+
}
11+
12+
public var output: ((String) -> ())? = nil
613

714
/// Event loop on which all commands execute
8-
public var eventLoop = EmbeddedEventLoop()
15+
public var eventLoop: EventLoop = EmbeddedEventLoop()
916

10-
/// Responses registered for commands
17+
/// Commads registered for commands
1118
/// - Note: Format is [Command: [Output piece]]
12-
public var responses: [String: [String]] = [:]
19+
public var mockResults: [String: [String]] = [:]
1320

14-
/// Errors for responses that are supposed to fail
15-
public var failingResponses: [String: Error] = [:]
21+
/// Errors for results that are supposed to fail
22+
public var failingMockResults: [String: Error] = [:]
1623

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

90+
public init() { }
91+
8392
}
8493

8594

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@_exported import CommandKit
2+
@_exported import ExecutorKit

0 commit comments

Comments
 (0)