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

@@ -24,11 +24,7 @@ public extension DependencyValues {
@DependencyClient
public struct EnvironmentDependency: Sendable {
/// A json decoder to use to decode files and environment variables.
public var jsonDecoder: @Sendable () -> JSONDecoder = { JSONDecoder() }
/// A json encoder to use to encode files and environment variables.
public var jsonEncoder: @Sendable () -> JSONEncoder = { JSONEncoder() }
public var coders: @Sendable () -> any Coderable = { JSONCoders() }
/// Load the variables based on the request.
public var load: @Sendable (FileType) async throws -> [String: String] = { _ in [:] }
@@ -61,6 +57,8 @@ public struct EnvironmentDependency: Sendable {
}
}
struct DecodeError: Error {}
@_spi(Internal)
extension EnvironmentDependency: DependencyKey {
@@ -71,8 +69,7 @@ extension EnvironmentDependency: DependencyKey {
encoder: JSONEncoder = .init()
) -> Self {
Self(
jsonDecoder: { decoder },
jsonEncoder: { encoder },
coders: { JSONCoders(decoder: decoder, encoder: encoder) },
load: { file in
switch file {
case let .dotEnv(path: path):
@@ -95,6 +92,39 @@ extension EnvironmentDependency: DependencyKey {
public static let liveValue: EnvironmentDependency = .live()
}
/// A type that encode and decode values.
///
/// This is really just here to override tests with coders that will throw an error,
/// instead of encoding or decoding data.
///
@_spi(Internal)
public protocol Coderable {
func encode<T: Encodable>(_ instance: T) throws -> Data
func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T
}
struct JSONCoders: Coderable {
let decoder: JSONDecoder
let encoder: JSONEncoder
init(
decoder: JSONDecoder = .init(),
encoder: JSONEncoder = .init()
) {
self.decoder = decoder
self.encoder = encoder
}
func encode<T>(_ instance: T) throws -> Data where T: Encodable {
try encoder.encode(instance)
}
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable {
try decoder.decode(T.self, from: data)
}
}
private func url(for path: String) -> URL {
#if os(Linux)
return URL(fileURLWithPath: path)