|
|
|
|
@@ -1,124 +1,121 @@
|
|
|
|
|
// @_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)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // @Test
|
|
|
|
|
// // func writeToml() throws {
|
|
|
|
|
// // let encoded: String = try TOMLEncoder().encode(Configuration.mock)
|
|
|
|
|
// // try encoded.write(to: URL(filePath: "hpa.toml"), atomically: true, encoding: .utf8)
|
|
|
|
|
// // }
|
|
|
|
|
// }
|
|
|
|
|
import CliClient
|
|
|
|
|
import ConfigurationClient
|
|
|
|
|
import Dependencies
|
|
|
|
|
import Foundation
|
|
|
|
|
import ShellClient
|
|
|
|
|
import Testing
|
|
|
|
|
import TestSupport
|
|
|
|
|
|
|
|
|
|
@Suite("CliClientTests")
|
|
|
|
|
struct CliClientTests: TestCase {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
func capturingClient() async throws {
|
|
|
|
|
let captured = CliClient.CapturingClient()
|
|
|
|
|
let client = CliClient.capturing(captured)
|
|
|
|
|
try await client.runCommand(quiet: false, shell: .zsh(), "foo", "bar")
|
|
|
|
|
|
|
|
|
|
let quiet = await captured.quiet!
|
|
|
|
|
#expect(quiet == false)
|
|
|
|
|
|
|
|
|
|
let shell = await captured.shell
|
|
|
|
|
#expect(shell == .zsh())
|
|
|
|
|
|
|
|
|
|
let arguments = await captured.arguments!
|
|
|
|
|
#expect(arguments == ["foo", "bar"])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(arguments: ["encrypt", "decrypt"])
|
|
|
|
|
func runVault(argument: String) async throws {
|
|
|
|
|
let captured = CliClient.CapturingClient()
|
|
|
|
|
try await withMockConfiguration(captured, key: "runVault") {
|
|
|
|
|
$0.fileClient.findVaultFileInCurrentDirectory = { URL(filePath: "vault.yml") }
|
|
|
|
|
} operation: {
|
|
|
|
|
@Dependency(\.cliClient) var cliClient
|
|
|
|
|
let configuration = Configuration.mock
|
|
|
|
|
|
|
|
|
|
try await cliClient.runVaultCommand(.init(arguments: [argument], quiet: false, shell: nil))
|
|
|
|
|
|
|
|
|
|
let shell = await captured.shell
|
|
|
|
|
#expect(shell == .zsh(useDashC: true))
|
|
|
|
|
|
|
|
|
|
let vaultPath = URL(filePath: #file)
|
|
|
|
|
.deletingLastPathComponent()
|
|
|
|
|
.deletingLastPathComponent()
|
|
|
|
|
.appending(path: "vault.yml")
|
|
|
|
|
|
|
|
|
|
var encryptArgs: [String] = []
|
|
|
|
|
if argument == "encrypt", let id = configuration.vault.encryptId {
|
|
|
|
|
encryptArgs = ["--encrypt-vault-id", id]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let expectedArguments = [
|
|
|
|
|
"ansible-vault", argument
|
|
|
|
|
] + configuration.vault.args!
|
|
|
|
|
+ encryptArgs
|
|
|
|
|
+ [vaultPath.cleanFilePath]
|
|
|
|
|
|
|
|
|
|
let arguments = await captured.arguments
|
|
|
|
|
#expect(arguments == expectedArguments)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(arguments: [
|
|
|
|
|
Configuration(
|
|
|
|
|
args: ["--tags", "debug"],
|
|
|
|
|
useVaultArgs: true,
|
|
|
|
|
playbook: .init(directory: "playbook", inventory: nil),
|
|
|
|
|
vault: .mock
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
func runPlaybook(configuration: Configuration) async throws {
|
|
|
|
|
let captured = CliClient.CapturingClient()
|
|
|
|
|
try await withMockConfiguration(captured, configuration: configuration, key: "runPlaybook") {
|
|
|
|
|
@Dependency(\.cliClient) var cliClient
|
|
|
|
|
|
|
|
|
|
try await cliClient.runPlaybookCommand(.init(
|
|
|
|
|
arguments: [],
|
|
|
|
|
quiet: false,
|
|
|
|
|
shell: nil
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
let expectedArguments = [
|
|
|
|
|
"ansible-playbook", "playbook/main.yml",
|
|
|
|
|
"--inventory", "playbook/inventory.ini",
|
|
|
|
|
"--tags", "debug",
|
|
|
|
|
"--vault-id=myId@$SCRIPTS/vault-gopass-client"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
let arguments = await captured.arguments
|
|
|
|
|
#expect(arguments == expectedArguments)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func withMockConfiguration(
|
|
|
|
|
_ capturing: CliClient.CapturingClient,
|
|
|
|
|
configuration: Configuration = .mock,
|
|
|
|
|
key: String,
|
|
|
|
|
logLevel: Logger.Level = .trace,
|
|
|
|
|
depednencies setupDependencies: @escaping (inout DependencyValues) -> Void = { _ in },
|
|
|
|
|
operation: @Sendable @escaping () async throws -> Void
|
|
|
|
|
) async rethrows {
|
|
|
|
|
try await withTestLogger(key: key, logLevel: logLevel) {
|
|
|
|
|
$0.configurationClient = .mock(configuration)
|
|
|
|
|
$0.cliClient = .capturing(capturing)
|
|
|
|
|
setupDependencies(&$0)
|
|
|
|
|
} operation: {
|
|
|
|
|
try await operation()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension ConfigurationClient {
|
|
|
|
|
static func mock(_ configuration: Configuration) -> Self {
|
|
|
|
|
var mock = Self.testValue
|
|
|
|
|
mock.find = { throw TestError() }
|
|
|
|
|
mock.load = { _ in configuration }
|
|
|
|
|
return mock
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct TestError: Error {}
|
|
|
|
|
|