Implemented some tests for the RunProcessTaskTests case tests in the tests target.

This commit is contained in:
Javier Cicchelli 2025-01-18 23:12:55 +01:00
parent c7a1258273
commit 36f1b8fe5c
2 changed files with 71 additions and 3 deletions

View File

@ -60,7 +60,7 @@ struct RunProcessTask {
} catch let error as RunProcessError { } catch let error as RunProcessError {
throw error throw error
} catch { } catch {
throw RunProcessError.captured(error) throw RunProcessError.captured(error.localizedDescription)
} }
} }
@ -68,8 +68,8 @@ struct RunProcessTask {
// MARK: - Errors // MARK: - Errors
public enum RunProcessError: Error { public enum RunProcessError: Error, Equatable {
case captured(_ error: Error) case captured(_ output: String)
case output(_ output: String) case output(_ output: String)
case unexpected case unexpected
} }

View File

@ -0,0 +1,68 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct RunProcessTaskTests {
// MARK: Properties
private var process: Process
// MARK: Initialisers
init() {
self.process = Process()
}
// MARK: Functions tests
@Test(arguments: [Argument.empty, Argument.listAllInFolder])
func run(with arguments: [String]) async throws {
// GIVEN
var task = RunProcessTask(process: process)
// WHEN
let output = try await task(path: .ls, arguments: arguments)
// THEN
#expect(output.isEmpty == false)
}
@Test(arguments: zip([Argument.help, Argument.listAllInPWD], Throw.outputs))
func runThrows(with arguments: [String], throws error: RunProcessError) async throws {
// GIVEN
var task = RunProcessTask(process: process)
// WHEN
// THEN
await #expect(throws: error) {
try await task(path: .ls, arguments: arguments)
}
}
}
// MARK: - String+Constants
private extension String {
static let ls = "/bin/ls"
}
// MARK: - Parameters
private extension RunProcessTaskTests {
enum Argument {
static let empty: [String] = []
static let help: [String] = ["--help"]
static let listAllInFolder: [String] = ["-la", "."]
static let listAllInPWD: [String] = ["-la", "~"]
}
enum Throw {
static let outputs: [RunProcessError] = [
.output("ls: unrecognized option `--help\'\nusage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]\n"),
.output("ls: ~: No such file or directory\n")
]
}
}