feat: Moved cli client tests to XCTest to work in docker
Some checks failed
CI / Run Tests (push) Has been cancelled

This commit is contained in:
2024-11-18 15:53:23 -05:00
parent ce18c44363
commit 24f2ad63a7
14 changed files with 227 additions and 486 deletions

View File

@@ -6,6 +6,11 @@ import Models
@_spi(Internal)
public extension DependencyValues {
/// A dependecy responsible for loding environment variables.
///
/// This is just used internally of this module, but is useful to
/// override for testing purposes, so import using `@_spi(Internal)`.
var environment: EnvironmentDependency {
get { self[EnvironmentDependency.self] }
set { self[EnvironmentDependency.self] = newValue }
@@ -19,16 +24,27 @@ 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() }
/// Load the variables based on the request.
public var load: @Sendable (FileType) async throws -> [String: String] = { _ in [:] }
/// Load the environment variables based on the current process environment.
///
/// You can override this to use an empty environment, which is useful for testing purposes.
public var processInfo: @Sendable () -> [String: String] = { [:] }
/// Represents the types of files that can be loaded and decoded into
/// the environment.
public enum FileType: Equatable {
case dotEnv(path: String)
case json(path: String)
init?(path: String) {
public init?(path: String) {
let strings = path.split(separator: ".")
guard let ext = strings.last else {
return nil
@@ -50,22 +66,39 @@ extension EnvironmentDependency: DependencyKey {
public static let testValue: EnvironmentDependency = Self()
public static let liveValue: EnvironmentDependency = Self(
load: { file in
switch file {
case let .dotEnv(path: path):
let file = try DotEnv.read(path: path)
return file.lines.reduce(into: [String: String]()) { partialResult, line in
partialResult[line.key] = line.value
public static func live(
decoder: JSONDecoder = .init(),
encoder: JSONEncoder = .init()
) -> Self {
Self(
jsonDecoder: { decoder },
jsonEncoder: { encoder },
load: { file in
switch file {
case let .dotEnv(path: path):
let file = try DotEnv.read(path: path)
return file.lines.reduce(into: [String: String]()) { partialResult, line in
partialResult[line.key] = line.value
}
case let .json(path: path):
let url = url(for: path)
return try decoder.decode(
[String: String].self,
from: Data(contentsOf: url)
)
}
case let .json(path: path):
let url = URL(filePath: path)
return try JSONDecoder().decode(
[String: String].self,
from: Data(contentsOf: url)
)
}
},
processInfo: { ProcessInfo.processInfo.environment }
)
},
processInfo: { ProcessInfo.processInfo.environment }
)
}
public static let liveValue: EnvironmentDependency = .live()
}
private func url(for path: String) -> URL {
#if os(Linux)
return URL(fileURLWithPath: path)
#else
return URL(filePath: path)
#endif
}