feat: Breaking out more dependencies.

This commit is contained in:
2024-12-10 17:08:17 -05:00
parent 87390c4b63
commit 92cd6afa2b
20 changed files with 1408 additions and 691 deletions

View File

@@ -1,154 +1,124 @@
@_spi(Internal) import CliClient
import Dependencies
import Foundation
import ShellClient
import Testing
import TOMLKit
@Suite("CliClientTests")
struct CliClientTests {
@Test
func testLiveFileClient() {
withTestLogger(key: "testFindConfigPaths", logLevel: .trace) {
$0.fileClient = .liveValue
} operation: {
@Dependency(\.fileClient) var fileClient
let homeDir = fileClient.homeDir()
#expect(fileClient.isDirectory(homeDir))
#expect(fileClient.isReadable(homeDir))
}
}
@Test
func testFindConfigPaths() throws {
withTestLogger(key: "testFindConfigPaths", logLevel: .trace) {
$0.fileClient = .liveValue
} operation: {
@Dependency(\.logger) var logger
let configURL = Bundle.module.url(forResource: "config", withExtension: "json")!
var env = [
"HPA_CONFIG_FILE": path(for: configURL)
]
var url = try? findConfigurationFiles(env: env)
#expect(url != nil)
env["HPA_CONFIG_FILE"] = nil
env["HPA_CONFIG_HOME"] = path(for: configURL.deletingLastPathComponent())
url = try? findConfigurationFiles(env: env)
#expect(url != nil)
env["HPA_CONFIG_HOME"] = nil
env["PWD"] = path(for: configURL.deletingLastPathComponent())
url = try? findConfigurationFiles(env: env)
#expect(url != nil)
env["PWD"] = nil
env["XDG_CONFIG_HOME"] = path(for: configURL.deletingLastPathComponent())
url = try? findConfigurationFiles(env: env)
#expect(url != nil)
withDependencies {
$0.fileClient.homeDir = { configURL.deletingLastPathComponent() }
} operation: {
url = try? findConfigurationFiles(env: [:])
#expect(url != nil)
}
url = try? findConfigurationFiles(env: [:])
#expect(url == nil)
}
}
@Test
func loadConfiguration() throws {
let configURL = Bundle.module.url(forResource: "config", withExtension: "json")!
let configData = try Data(contentsOf: configURL)
let decodedConfig = try JSONDecoder().decode(Configuration.self, from: configData)
try withTestLogger(key: "loadConfiguration", logLevel: .debug) {
$0.fileClient = .liveValue
} operation: {
@Dependency(\.logger) var logger
let client = CliClient.live(env: ["HPA_CONFIG_FILE": path(for: configURL)])
let config = try client.loadConfiguration()
#expect(config == decodedConfig)
}
}
@Test(arguments: ["config", "config.json"])
func createConfiguration(filePath: String) throws {
try withTestLogger(key: "createConfiguration", logLevel: .trace) {
$0.fileClient = .liveValue
} operation: {
let client = CliClient.liveValue
let tempDir = FileManager.default.temporaryDirectory
let tempPath = path(for: tempDir.appending(path: filePath))
try client.createConfiguration(path: tempPath, json: filePath.contains(".json"))
#expect(FileManager.default.fileExists(atPath: tempPath))
do {
try client.createConfiguration(path: tempPath, json: true)
#expect(Bool(false))
} catch {
#expect(Bool(true))
}
try FileManager.default.removeItem(atPath: tempPath)
}
}
@Test
func findVaultFile() throws {
try withTestLogger(key: "findVaultFile", logLevel: .trace) {
$0.fileClient = .liveValue
} operation: {
@Dependency(\.fileClient) var fileClient
let vaultUrl = Bundle.module.url(forResource: "vault", withExtension: "yml")!
let vaultDir = vaultUrl.deletingLastPathComponent()
let url = try fileClient.findVaultFile(path(for: vaultDir))
#expect(url == vaultUrl)
}
}
// @_spi(Internal) import CliClient
// import Dependencies
// import Foundation
// import ShellClient
// import Testing
// import TOMLKit
//
// @Suite("CliClientTests")
// struct CliClientTests {
//
// @Test
// func writeToml() throws {
// let encoded: String = try TOMLEncoder().encode(Configuration2.mock)
// try encoded.write(to: URL(filePath: "hpa.toml"), atomically: true, encoding: .utf8)
// func testLiveFileClient() {
// withTestLogger(key: "testFindConfigPaths", logLevel: .trace) {
// $0.fileClient = .liveValue
// } operation: {
// @Dependency(\.fileClient) var fileClient
// let homeDir = fileClient.homeDir()
// #expect(fileClient.isDirectory(homeDir))
// #expect(fileClient.isReadable(homeDir))
// }
// }
}
//
// @Test
// func testFindConfigPaths() throws {
// withTestLogger(key: "testFindConfigPaths", logLevel: .trace) {
// $0.fileClient = .liveValue
// } operation: {
// @Dependency(\.logger) var logger
// let configURL = Bundle.module.url(forResource: "config", withExtension: "json")!
// var env = [
// "HPA_CONFIG_FILE": path(for: configURL)
// ]
// var url = try? findConfigurationFiles(env: env)
// #expect(url != nil)
//
// env["HPA_CONFIG_FILE"] = nil
// env["HPA_CONFIG_HOME"] = path(for: configURL.deletingLastPathComponent())
// url = try? findConfigurationFiles(env: env)
// #expect(url != nil)
//
// env["HPA_CONFIG_HOME"] = nil
// env["PWD"] = path(for: configURL.deletingLastPathComponent())
// url = try? findConfigurationFiles(env: env)
// #expect(url != nil)
//
// env["PWD"] = nil
// env["XDG_CONFIG_HOME"] = path(for: configURL.deletingLastPathComponent())
// url = try? findConfigurationFiles(env: env)
// #expect(url != nil)
//
// withDependencies {
// $0.fileClient.homeDir = { configURL.deletingLastPathComponent() }
// } operation: {
// url = try? findConfigurationFiles(env: [:])
// #expect(url != nil)
// }
//
// url = try? findConfigurationFiles(env: [:])
// #expect(url == nil)
// }
// }
//
// // @Test
// // func loadConfiguration() throws {
// // let configURL = Bundle.module.url(forResource: "config", withExtension: "json")!
// // let configData = try Data(contentsOf: configURL)
// // let decodedConfig = try JSONDecoder().decode(Configuration.self, from: configData)
// //
// // try withTestLogger(key: "loadConfiguration", logLevel: .debug) {
// // $0.fileClient = .liveValue
// // } operation: {
// // @Dependency(\.logger) var logger
// // let client = CliClient.live(env: ["HPA_CONFIG_FILE": path(for: configURL)])
// // let config = try client.loadConfiguration()
// // #expect(config == decodedConfig)
// // }
// // }
//
// @Test(arguments: ["config", "config.json"])
// func createConfiguration(filePath: String) throws {
// try withTestLogger(key: "createConfiguration", logLevel: .trace) {
// $0.fileClient = .liveValue
// } operation: {
// let client = CliClient.liveValue
// let tempDir = FileManager.default.temporaryDirectory
//
// let tempPath = path(for: tempDir.appending(path: filePath))
//
// try client.createConfiguration(path: tempPath, json: filePath.contains(".json"))
//
// #expect(FileManager.default.fileExists(atPath: tempPath))
//
// do {
// try client.createConfiguration(path: tempPath, json: true)
// #expect(Bool(false))
// } catch {
// #expect(Bool(true))
// }
//
// try FileManager.default.removeItem(atPath: tempPath)
// }
// }
//
// @Test
// func findVaultFile() throws {
// try withTestLogger(key: "findVaultFile", logLevel: .trace) {
// $0.fileClient = .liveValue
// } operation: {
// @Dependency(\.fileClient) var fileClient
// let vaultUrl = Bundle.module.url(forResource: "vault", withExtension: "yml")!
// let vaultDir = vaultUrl.deletingLastPathComponent()
// let url = try fileClient.findVaultFile(path(for: vaultDir))
// #expect(url == vaultUrl)
// }
// }
//
// // @Test
// // func writeToml() throws {
// // let encoded: String = try TOMLEncoder().encode(Configuration.mock)
// // try encoded.write(to: URL(filePath: "hpa.toml"), atomically: true, encoding: .utf8)
// // }
// }
func withTestLogger(
key: String,
label: String = "CliClientTests",
logLevel: Logger.Level = .debug,
operation: @escaping @Sendable () throws -> Void
) rethrows {
try withDependencies {
$0.logger = .init(label: label)
$0.logger[metadataKey: "test"] = "\(key)"
$0.logger.logLevel = logLevel
} operation: {
try operation()
}
}
func withTestLogger(
key: String,
label: String = "CliClientTests",
logLevel: Logger.Level = .debug,
dependencies setupDependencies: @escaping (inout DependencyValues) -> Void,
operation: @escaping @Sendable () throws -> Void
) rethrows {
try withDependencies {
$0.logger = .init(label: label)
$0.logger[metadataKey: "test"] = "\(key)"
$0.logger.logLevel = logLevel
setupDependencies(&$0)
} operation: {
try operation()
}
}