feat: Begins work on supporting toml for configuration.

This commit is contained in:
2024-12-09 17:00:52 -05:00
parent a8d35fed37
commit 87390c4b63
14 changed files with 354 additions and 196 deletions

View File

@@ -1,32 +1,124 @@
@_spi(Internal) import CliClient
import Dependencies
import Foundation
import ShellClient
import Testing
import TOMLKit
@Test
func testFindConfigPaths() throws {
try withTestLogger(key: "testFindConfigPaths") {
$0.fileClient = .liveValue
} operation: {
@Dependency(\.logger) var logger
let urls = try findConfigurationFiles()
logger.debug("urls: \(urls)")
// #expect(urls.count == 1)
}
}
@Suite("CliClientTests")
struct CliClientTests {
@Test
func loadConfiguration() throws {
try withTestLogger(key: "loadConfiguration", logLevel: .debug) {
$0.cliClient = .liveValue
$0.fileClient = .liveValue
} operation: {
@Dependency(\.cliClient) var client
@Dependency(\.logger) var logger
let config = try client.loadConfiguration()
logger.debug("\(config)")
#expect(config.playbookDir != nil)
@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)
}
}
// @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 withTestLogger(

View File

@@ -0,0 +1,15 @@
{
"HPA_TEMPLATE_VERSION" : "main",
"HPA_TEMPLATE_DIR" : "/path/to/local/template",
"HPA_PLAYBOOK_DIR" : "/path/to/playbook",
"HPA_DEFAULT_VAULT_ARGS" : [
"--vault-id=myId@$SCRIPTS/vault-gopass-client"
],
"HPA_TEMPLATE_REPO" : "https://git.example.com/consult-template.git",
"HPA_DEFAULT_PLAYBOOK_ARGS" : [
"--tags",
"debug"
],
"HPA_DEFAULT_VAULT_ENCRYPT_ID" : "myId",
"HPA_DEFAULT_INVENTORY" : "/path/to/inventory.ini"
}

View File

@@ -0,0 +1,15 @@
{
"HPA_TEMPLATE_VERSION" : "main",
"HPA_TEMPLATE_DIR" : "/path/to/local/template",
"HPA_PLAYBOOK_DIR" : "/path/to/playbook",
"HPA_DEFAULT_VAULT_ARGS" : [
"--vault-id=myId@$SCRIPTS/vault-gopass-client"
],
"HPA_TEMPLATE_REPO" : "https://git.example.com/consult-template.git",
"HPA_DEFAULT_PLAYBOOK_ARGS" : [
"--tags",
"debug"
],
"HPA_DEFAULT_VAULT_ENCRYPT_ID" : "myId",
"HPA_DEFAULT_INVENTORY" : "/path/to/inventory.ini"
}

View File

@@ -0,0 +1,15 @@
{
"HPA_TEMPLATE_VERSION" : "main",
"HPA_TEMPLATE_DIR" : "/path/to/local/template",
"HPA_PLAYBOOK_DIR" : "/path/to/playbook",
"HPA_DEFAULT_VAULT_ARGS" : [
"--vault-id=myId@$SCRIPTS/vault-gopass-client"
],
"HPA_TEMPLATE_REPO" : "https://git.example.com/consult-template.git",
"HPA_DEFAULT_PLAYBOOK_ARGS" : [
"--tags",
"debug"
],
"HPA_DEFAULT_VAULT_ENCRYPT_ID" : "myId",
"HPA_DEFAULT_INVENTORY" : "/path/to/inventory.ini"
}

View File

@@ -0,0 +1,2 @@
---
# this is just here for testing, doesn't need to be encoded.