Implemented the TerminalService service for the library target
This commit is contained in:
@@ -26,6 +26,7 @@ struct TemplateServiceTests {
|
||||
|
||||
@Test(arguments: [TemplateServiceError.serviceNotInitialized, .resourcePathNotFound, .templateNotFound, .contentNotRendered])
|
||||
func render(throws error: TemplateServiceError) async throws {
|
||||
// GIVEN
|
||||
let service = TemplateServiceMock(action: .error(error), spy: spy)
|
||||
|
||||
// WHEN
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
struct TerminalServiceTests {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let spy = TerminalServiceSpy()
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
@Test(arguments: [URL.someNewFile], [[], ["--example"], ["--example", "--more", "--etc"]])
|
||||
func run(with executableURL: URL, and arguments: [String]) async throws {
|
||||
// GIVEN
|
||||
let service = TerminalServiceMock(action: .run(executableURL, arguments), spy: spy)
|
||||
|
||||
// WHEN
|
||||
let result = try await service.run(executableURL, arguments: arguments)
|
||||
|
||||
// THEN
|
||||
#expect(result == .content)
|
||||
|
||||
#expect(spy.actions.isEmpty == false)
|
||||
|
||||
let action = try #require(spy.actions.last)
|
||||
|
||||
#expect(action == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [TerminalServiceError.unexpected, .captured("Some captured error"), .output("Some output error")])
|
||||
func run(throws error: TerminalServiceError) async throws {
|
||||
// GIVEN
|
||||
let service = TerminalServiceMock(action: .error(error), spy: spy)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.run(URL.someNewFile, arguments: [])
|
||||
}
|
||||
|
||||
#expect(spy.actions.isEmpty == true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let content = ""
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
|
||||
final class TerminalServiceMock {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private var actions: [Action] = []
|
||||
|
||||
private weak var spy: TerminalServiceSpy?
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(
|
||||
action: Action,
|
||||
spy: TerminalServiceSpy? = nil
|
||||
) {
|
||||
self.actions.append(action)
|
||||
self.spy = spy
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TerminalServicing
|
||||
|
||||
extension TerminalServiceMock: TerminalServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func run(_ executableURL: URL, arguments: [String]) async throws (TerminalServiceError) -> String {
|
||||
guard let nextAction else { return .empty }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
case let .run(executableURL, arguments):
|
||||
try await spy?.run(executableURL, arguments: arguments)
|
||||
return .empty
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension TerminalServiceMock {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
var nextAction: Action? {
|
||||
guard !actions.isEmpty else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return actions.removeFirst()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
extension TerminalServiceMock {
|
||||
enum Action {
|
||||
case error(TerminalServiceError)
|
||||
case run(URL, [String])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let empty = ""
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
|
||||
final class TerminalServiceSpy {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private(set) var actions: [Action] = []
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TerminalServicing
|
||||
|
||||
extension TerminalServiceSpy: TerminalServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
@discardableResult
|
||||
func run(_ executableURL: URL, arguments: [String]) async throws(TerminalServiceError) -> String {
|
||||
actions.append(.ran(executableURL, arguments))
|
||||
|
||||
return .content
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
extension TerminalServiceSpy {
|
||||
enum Action: Equatable {
|
||||
case ran(_ executableURL: URL, _ arguments: [String])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let content = ""
|
||||
}
|
||||
Reference in New Issue
Block a user