From 36f1b8fe5c2a54932733e5fb0dd35e7e99bc72ba Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sat, 18 Jan 2025 23:12:55 +0100 Subject: [PATCH] Implemented some tests for the RunProcessTaskTests case tests in the tests target. --- .../Internal/Tasks/RunProcessTask.swift | 6 +- .../Internal/Tasks/RunProcessTaskTests.swift | 68 +++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 Test/Sources/Cases/Internal/Tasks/RunProcessTaskTests.swift diff --git a/Library/Sources/Internal/Tasks/RunProcessTask.swift b/Library/Sources/Internal/Tasks/RunProcessTask.swift index 7835b87..ea267b4 100644 --- a/Library/Sources/Internal/Tasks/RunProcessTask.swift +++ b/Library/Sources/Internal/Tasks/RunProcessTask.swift @@ -60,7 +60,7 @@ struct RunProcessTask { } catch let error as RunProcessError { throw error } catch { - throw RunProcessError.captured(error) + throw RunProcessError.captured(error.localizedDescription) } } @@ -68,8 +68,8 @@ struct RunProcessTask { // MARK: - Errors -public enum RunProcessError: Error { - case captured(_ error: Error) +public enum RunProcessError: Error, Equatable { + case captured(_ output: String) case output(_ output: String) case unexpected } diff --git a/Test/Sources/Cases/Internal/Tasks/RunProcessTaskTests.swift b/Test/Sources/Cases/Internal/Tasks/RunProcessTaskTests.swift new file mode 100644 index 0000000..f493e1f --- /dev/null +++ b/Test/Sources/Cases/Internal/Tasks/RunProcessTaskTests.swift @@ -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") + ] + } +}