feat: More cli client tests and documentation.
All checks were successful
CI / Run Tests (push) Successful in 4m57s

This commit is contained in:
2024-11-18 22:55:54 -05:00
parent 55ea88a29f
commit 99f39b91af
3 changed files with 114 additions and 22 deletions

View File

@@ -32,6 +32,8 @@ final class CliClientTests: XCTestCase {
XCTAssertEqual(cliClient.parseMqttClientVersion(string), version)
}
}
XCTAssertEqual(MQTTClient.Version.parseOrDefault(string: nil), .v3_1_1)
}
func testLogLevelFromEnvironment() {
@@ -98,6 +100,26 @@ final class CliClientTests: XCTestCase {
}
}
func testMakeEnvVarsWithFailingDecoder() async throws {
try await withDependencies {
$0.environment.coders = { ThrowingDecoder() }
} operation: {
@Dependency(\.cliClient) var cliClient
let envVars = try await cliClient.makeEnvVars(.init())
XCTAssertEqual(envVars, EnvVars())
}
}
func testMakeEnvVarsWithFailingEncoder() async throws {
try await withDependencies {
$0.environment.coders = { ThrowingEncoder() }
} operation: {
@Dependency(\.cliClient) var cliClient
let envVars = try await cliClient.makeEnvVars(.init())
XCTAssertEqual(envVars, EnvVars())
}
}
func testFileType() {
let arguments = [
(EnvironmentDependency.FileType.dotEnv(path: "test.env"), "test.env"),
@@ -139,12 +161,16 @@ final class CliClientTests: XCTestCase {
// - MARK: Helper
private func cleanFilePath(_ path: String) -> String {
let split = path.split(separator: ".")
let fileName = split.first!
let ext = split.last!
let url = Bundle.module.url(forResource: String(fileName), withExtension: String(ext))!.absoluteString
let cleaned = url.split(separator: "file://").last!
return String(cleaned)
#if os(Linux)
return "Tests/CliClientTests/\(path)"
#else
let split = path.split(separator: ".")
let fileName = split.first!
let ext = split.last!
let url = Bundle.module.url(forResource: String(fileName), withExtension: String(ext))!.absoluteString
let cleaned = url.split(separator: "file://").last!
return String(cleaned)
#endif
}
extension EnvVars {
@@ -159,3 +185,28 @@ extension EnvVars {
version: "5.0"
)
}
struct ThrowingDecoder: Coderable {
func encode<T>(_ instance: T) throws -> Data where T: Encodable {
try JSONEncoder().encode(instance)
}
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable {
throw DecodeError()
}
}
struct ThrowingEncoder: Coderable {
func encode<T>(_ instance: T) throws -> Data where T: Encodable {
throw EncodeError()
}
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable {
try JSONDecoder().decode(T.self, from: data)
}
}
struct DecodeError: Error {}
struct EncodeError: Error {}