feat: Begins work on supporting toml for configuration.
This commit is contained in:
@@ -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(
|
||||
|
||||
15
Tests/CliClientTests/Resources/.hparc
Normal file
15
Tests/CliClientTests/Resources/.hparc
Normal 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"
|
||||
}
|
||||
15
Tests/CliClientTests/Resources/config.json
Normal file
15
Tests/CliClientTests/Resources/config.json
Normal 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"
|
||||
}
|
||||
15
Tests/CliClientTests/Resources/hpa-playbook/config.json
Normal file
15
Tests/CliClientTests/Resources/hpa-playbook/config.json
Normal 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"
|
||||
}
|
||||
2
Tests/CliClientTests/Resources/vault.yml
Normal file
2
Tests/CliClientTests/Resources/vault.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
# this is just here for testing, doesn't need to be encoded.
|
||||
@@ -1,156 +0,0 @@
|
||||
@_spi(Internal) import CliDoc
|
||||
@preconcurrency import Rainbow
|
||||
import Testing
|
||||
import XCTest
|
||||
|
||||
final class CliDocTests: XCTestCase {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
Rainbow.outputTarget = .console
|
||||
Rainbow.enabled = true
|
||||
}
|
||||
|
||||
func testStringChecks() {
|
||||
let expected = "Foo".green.bold
|
||||
|
||||
XCTAssert("Foo".green.bold == expected)
|
||||
XCTAssert(expected != "Foo")
|
||||
}
|
||||
|
||||
func testRepeatingModifier() {
|
||||
let node = AnyNode {
|
||||
Text("foo").color(.green).style(.bold)
|
||||
"\n".repeating(2)
|
||||
Text("bar").repeating(2, separator: " ")
|
||||
}
|
||||
let expected = """
|
||||
\("foo".green.bold)
|
||||
|
||||
bar bar
|
||||
"""
|
||||
XCTAssert(node.render() == expected)
|
||||
}
|
||||
|
||||
func testGroup1() {
|
||||
let arguments = [
|
||||
(true, "foo bar"),
|
||||
(false, """
|
||||
foo
|
||||
bar
|
||||
""")
|
||||
]
|
||||
|
||||
for (inline, expected) in arguments {
|
||||
let node = AnyNode {
|
||||
Group(separator: inline ? " " : "\n") {
|
||||
Text("foo")
|
||||
Text("bar")
|
||||
}
|
||||
}
|
||||
XCTAssert(node.render() == expected)
|
||||
}
|
||||
}
|
||||
|
||||
func testHeader() {
|
||||
let header = Header("Foo")
|
||||
let expected = "\("Foo".yellow.bold)"
|
||||
XCTAssert(header.render() == expected)
|
||||
|
||||
let header2 = Header {
|
||||
"Foo".yellow.bold
|
||||
}
|
||||
XCTAssert(header2.render() == expected)
|
||||
}
|
||||
|
||||
func testGroup() {
|
||||
let group = Group {
|
||||
Text("foo")
|
||||
Text("bar")
|
||||
}
|
||||
|
||||
XCTAssert(group.render() == "foo bar")
|
||||
|
||||
let group2 = Group(separator: "\n") {
|
||||
Text("foo")
|
||||
Text("bar")
|
||||
}
|
||||
let expected = """
|
||||
foo
|
||||
bar
|
||||
"""
|
||||
XCTAssert(group2.render() == expected)
|
||||
}
|
||||
|
||||
func testLabeledContent() {
|
||||
let node = LabeledContent("Foo") {
|
||||
Text("Bar")
|
||||
}
|
||||
.labelStyle(.green)
|
||||
.labelStyle(.bold)
|
||||
|
||||
let expected = """
|
||||
\("Foo".green.bold)
|
||||
Bar
|
||||
"""
|
||||
XCTAssert(node.render() == expected)
|
||||
}
|
||||
|
||||
func testLabeledContent2() {
|
||||
let node = LabeledContent2 {
|
||||
"Foo"
|
||||
} content: {
|
||||
Text("Bar")
|
||||
}
|
||||
// .labelStyle(.green)
|
||||
// .labelStyle(.bold)
|
||||
|
||||
let expected = """
|
||||
Foo Bar
|
||||
"""
|
||||
XCTAssert(node.render() == expected)
|
||||
print(type(of: node.body))
|
||||
XCTAssertNotNil(node.body as? _ManyNode)
|
||||
}
|
||||
|
||||
func testShellCommand() {
|
||||
let node = ShellCommand {
|
||||
"ls -lah"
|
||||
}
|
||||
let expected = " $ ls -lah"
|
||||
XCTAssert(node.render() == expected)
|
||||
}
|
||||
|
||||
func testDiscussion() {
|
||||
let node = Discussion {
|
||||
Group(separator: "\n") {
|
||||
LabeledContent(separator: " ") {
|
||||
"NOTE:".yellow.bold
|
||||
} content: {
|
||||
"Foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let expected = """
|
||||
\("NOTE:".yellow.bold) Foo
|
||||
"""
|
||||
|
||||
XCTAssert(node.render() == expected)
|
||||
}
|
||||
|
||||
func testFooNode() {
|
||||
let foo = Foo()
|
||||
XCTAssertNotNil(foo.body as? LabeledContent)
|
||||
}
|
||||
|
||||
func testGroup2() {
|
||||
let node = Group2 {
|
||||
Text("foo")
|
||||
Text("bar")
|
||||
}
|
||||
print(node.render())
|
||||
XCTAssertNotNil(node.body as? _ManyNode)
|
||||
// XCTAssert(false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user