feat: Moves logging setup and generate-json for the create command to cli-client module.
This commit is contained in:
@@ -10,6 +10,12 @@ import TestSupport
|
||||
@Suite("CliClientTests")
|
||||
struct CliClientTests: TestCase {
|
||||
|
||||
static let loggingOptions: CliClient.LoggingOptions = {
|
||||
let levelString = ProcessInfo.processInfo.environment["LOGGING_LEVEL"] ?? "debug"
|
||||
let logLevel = Logger.Level(rawValue: levelString) ?? .debug
|
||||
return .init(commandName: "CliClientTests", logLevel: logLevel)
|
||||
}()
|
||||
|
||||
@Test
|
||||
func capturingClient() async throws {
|
||||
let captured = CliClient.CapturingClient()
|
||||
@@ -35,7 +41,10 @@ struct CliClientTests: TestCase {
|
||||
@Dependency(\.cliClient) var cliClient
|
||||
let configuration = Configuration.mock
|
||||
|
||||
try await cliClient.runVaultCommand(.init(arguments: [argument], quiet: false, shell: nil))
|
||||
try await cliClient.runVaultCommand(
|
||||
.init(arguments: [argument], quiet: false, shell: nil),
|
||||
logging: Self.loggingOptions
|
||||
)
|
||||
|
||||
let shell = await captured.shell
|
||||
#expect(shell == .zsh(useDashC: true))
|
||||
@@ -74,11 +83,14 @@ struct CliClientTests: TestCase {
|
||||
try await withMockConfiguration(captured, configuration: configuration, key: "runPlaybook") {
|
||||
@Dependency(\.cliClient) var cliClient
|
||||
|
||||
try await cliClient.runPlaybookCommand(.init(
|
||||
arguments: [],
|
||||
quiet: false,
|
||||
shell: nil
|
||||
))
|
||||
try await cliClient.runPlaybookCommand(
|
||||
.init(
|
||||
arguments: [],
|
||||
quiet: false,
|
||||
shell: nil
|
||||
),
|
||||
logging: Self.loggingOptions
|
||||
)
|
||||
|
||||
let expectedArguments = [
|
||||
"ansible-playbook", "playbook/main.yml",
|
||||
@@ -156,6 +168,118 @@ struct CliClientTests: TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(
|
||||
arguments: [
|
||||
GenerateJsonTestOption(
|
||||
options: .init(
|
||||
templateDirectory: nil,
|
||||
templateRepo: nil,
|
||||
version: nil,
|
||||
useLocalTemplateDirectory: true
|
||||
),
|
||||
configuration: nil,
|
||||
expectation: .failing
|
||||
),
|
||||
GenerateJsonTestOption(
|
||||
options: .init(
|
||||
templateDirectory: nil,
|
||||
templateRepo: nil,
|
||||
version: nil,
|
||||
useLocalTemplateDirectory: false
|
||||
),
|
||||
configuration: nil,
|
||||
expectation: .failing
|
||||
),
|
||||
GenerateJsonTestOption(
|
||||
options: .init(
|
||||
templateDirectory: "template",
|
||||
templateRepo: nil,
|
||||
version: nil,
|
||||
useLocalTemplateDirectory: true
|
||||
),
|
||||
configuration: nil,
|
||||
expectation: .success("""
|
||||
{
|
||||
"template" : {
|
||||
"path" : "template"
|
||||
}
|
||||
}
|
||||
""")
|
||||
),
|
||||
GenerateJsonTestOption(
|
||||
options: .init(
|
||||
templateDirectory: nil,
|
||||
templateRepo: nil,
|
||||
version: nil,
|
||||
useLocalTemplateDirectory: false
|
||||
),
|
||||
configuration: .init(template: .init(directory: "template")),
|
||||
expectation: .success("""
|
||||
{
|
||||
"template" : {
|
||||
"path" : "template"
|
||||
}
|
||||
}
|
||||
""")
|
||||
),
|
||||
GenerateJsonTestOption(
|
||||
options: .init(
|
||||
templateDirectory: nil,
|
||||
templateRepo: "https://git.example.com/template.git",
|
||||
version: nil,
|
||||
useLocalTemplateDirectory: false
|
||||
),
|
||||
configuration: nil,
|
||||
expectation: .success("""
|
||||
{
|
||||
"template" : {
|
||||
"repo" : "https://git.example.com/template.git",
|
||||
"version" : "main"
|
||||
}
|
||||
}
|
||||
""")
|
||||
),
|
||||
GenerateJsonTestOption(
|
||||
options: .init(
|
||||
templateDirectory: nil,
|
||||
templateRepo: nil,
|
||||
version: nil,
|
||||
useLocalTemplateDirectory: false
|
||||
),
|
||||
configuration: .init(template: .init(url: "https://git.example.com/template.git", version: "v0.1.0")),
|
||||
expectation: .success("""
|
||||
{
|
||||
"template" : {
|
||||
"repo" : "https://git.example.com/template.git",
|
||||
"version" : "v0.1.0"
|
||||
}
|
||||
}
|
||||
""")
|
||||
)
|
||||
]
|
||||
)
|
||||
func generateJson(input: GenerateJsonTestOption) async {
|
||||
await withTestLogger(key: "generateJson") {
|
||||
$0.configurationClient = .mock(input.configuration ?? .init())
|
||||
$0.cliClient = .liveValue
|
||||
} operation: {
|
||||
@Dependency(\.cliClient) var cliClient
|
||||
|
||||
let json = try? await cliClient.generateJSON(
|
||||
input.options,
|
||||
logging: Self.loggingOptions,
|
||||
encoder: jsonEncoder
|
||||
)
|
||||
|
||||
switch input.expectation {
|
||||
case let .success(expected):
|
||||
#expect(json == expected)
|
||||
case .failing:
|
||||
#expect(json == nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withMockConfiguration(
|
||||
_ capturing: CliClient.CapturingClient,
|
||||
configuration: Configuration = .mock,
|
||||
@@ -174,6 +298,17 @@ struct CliClientTests: TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
struct GenerateJsonTestOption: Sendable {
|
||||
let options: CliClient.GenerateJsonOptions
|
||||
let configuration: Configuration?
|
||||
let expectation: GenerateJsonExpectation
|
||||
}
|
||||
|
||||
enum GenerateJsonExpectation: Sendable {
|
||||
case failing
|
||||
case success(String)
|
||||
}
|
||||
|
||||
extension ConfigurationClient {
|
||||
static func mock(_ configuration: Configuration) -> Self {
|
||||
var mock = Self.testValue
|
||||
@@ -184,3 +319,9 @@ extension ConfigurationClient {
|
||||
}
|
||||
|
||||
struct TestError: Error {}
|
||||
|
||||
let jsonEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes, .sortedKeys]
|
||||
return encoder
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user