feat: Adds createProject and createJson tests for playbook-client.
This commit is contained in:
@@ -28,9 +28,16 @@ extension PlaybookClient.RunPlaybook.BuildOptions {
|
||||
|
||||
extension PlaybookClient.RunPlaybook.CreateOptions {
|
||||
|
||||
func run() async throws {
|
||||
func run(encoder jsonEncoder: JSONEncoder?) async throws {
|
||||
try await shared.run { arguments, configuration in
|
||||
let json = try createJSONData(configuration: configuration)
|
||||
let jsonData = try createJSONData(
|
||||
configuration: configuration,
|
||||
encoder: jsonEncoder
|
||||
)
|
||||
|
||||
guard let json = String(data: jsonData, encoding: .utf8) else {
|
||||
throw PlaybookClientError.encodingError
|
||||
}
|
||||
|
||||
arguments.append(contentsOf: [
|
||||
"--tags", "setup-project",
|
||||
@@ -130,17 +137,20 @@ public extension PlaybookClient.RunPlaybook {
|
||||
// want the output to be `prettyPrinted` or anything, unless we're running
|
||||
// tests, so we use a supplied json encoder.
|
||||
//
|
||||
extension PlaybookClient.RunPlaybook.CreateOptions {
|
||||
@_spi(Internal)
|
||||
public extension PlaybookClient.RunPlaybook.CreateOptions {
|
||||
|
||||
func createJSONData(
|
||||
configuration: Configuration,
|
||||
encoder: JSONEncoder = .init()
|
||||
encoder: JSONEncoder?
|
||||
) throws -> Data {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let templateDir = template.directory ?? configuration.template.directory
|
||||
let templateRepo = template.url ?? configuration.template.url
|
||||
let version = template.version ?? configuration.template.version
|
||||
let encoder = encoder ?? jsonEncoder
|
||||
|
||||
let templateDir = template?.directory ?? configuration.template.directory
|
||||
let templateRepo = template?.url ?? configuration.template.url
|
||||
let version = template?.version ?? configuration.template.version
|
||||
|
||||
logger.debug("""
|
||||
(\(useLocalTemplateDirectory), \(String(describing: templateDir)), \(String(describing: templateRepo)))
|
||||
@@ -203,3 +213,9 @@ private struct TemplateRepo: Encodable {
|
||||
let version: String
|
||||
}
|
||||
}
|
||||
|
||||
private let jsonEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.withoutEscapingSlashes, .sortedKeys]
|
||||
return encoder
|
||||
}()
|
||||
|
||||
@@ -43,7 +43,11 @@ public extension PlaybookClient {
|
||||
@DependencyClient
|
||||
struct RunPlaybook: Sendable {
|
||||
public var buildProject: @Sendable (BuildOptions) async throws -> Void
|
||||
public var createProject: @Sendable (CreateOptions) async throws -> Void
|
||||
public var createProject: @Sendable (CreateOptions, JSONEncoder?) async throws -> Void
|
||||
|
||||
public func createProject(_ options: CreateOptions) async throws {
|
||||
try await createProject(options, nil)
|
||||
}
|
||||
|
||||
public struct SharedRunOptions: Equatable, Sendable {
|
||||
public let extraOptions: [String]?
|
||||
@@ -53,11 +57,11 @@ public extension PlaybookClient {
|
||||
public let shell: String?
|
||||
|
||||
public init(
|
||||
extraOptions: [String]?,
|
||||
inventoryFilePath: String?,
|
||||
extraOptions: [String]? = nil,
|
||||
inventoryFilePath: String? = nil,
|
||||
loggingOptions: LoggingOptions,
|
||||
quiet: Bool,
|
||||
shell: String?
|
||||
quiet: Bool = false,
|
||||
shell: String? = nil
|
||||
) {
|
||||
self.extraOptions = extraOptions
|
||||
self.inventoryFilePath = inventoryFilePath
|
||||
@@ -73,12 +77,20 @@ public extension PlaybookClient {
|
||||
public let shared: SharedRunOptions
|
||||
|
||||
public init(
|
||||
extraOptions: [String]?,
|
||||
inventoryFilePath: String?,
|
||||
projectDirectory: String? = nil,
|
||||
shared: SharedRunOptions
|
||||
) {
|
||||
self.projectDirectory = projectDirectory
|
||||
self.shared = shared
|
||||
}
|
||||
|
||||
public init(
|
||||
extraOptions: [String]? = nil,
|
||||
inventoryFilePath: String? = nil,
|
||||
loggingOptions: LoggingOptions,
|
||||
quiet: Bool,
|
||||
shell: String?,
|
||||
projectDirectory: String
|
||||
quiet: Bool = false,
|
||||
shell: String? = nil,
|
||||
projectDirectory: String? = nil
|
||||
) {
|
||||
self.projectDirectory = projectDirectory
|
||||
self.shared = .init(
|
||||
@@ -99,18 +111,30 @@ public extension PlaybookClient {
|
||||
public struct CreateOptions: Equatable, Sendable {
|
||||
public let projectDirectory: String
|
||||
public let shared: SharedRunOptions
|
||||
public let template: Configuration.Template
|
||||
public let template: Configuration.Template?
|
||||
public let useLocalTemplateDirectory: Bool
|
||||
|
||||
public init(
|
||||
extraOptions: [String]?,
|
||||
inventoryFilePath: String?,
|
||||
projectDirectory: String,
|
||||
shared: SharedRunOptions,
|
||||
template: Configuration.Template? = nil,
|
||||
useLocalTemplateDirectory: Bool
|
||||
) {
|
||||
self.projectDirectory = projectDirectory
|
||||
self.shared = shared
|
||||
self.template = template
|
||||
self.useLocalTemplateDirectory = useLocalTemplateDirectory
|
||||
}
|
||||
|
||||
public init(
|
||||
extraOptions: [String]? = nil,
|
||||
inventoryFilePath: String? = nil,
|
||||
loggingOptions: LoggingOptions,
|
||||
projectDirectory: String,
|
||||
quiet: Bool,
|
||||
shell: String?,
|
||||
template: Configuration.Template,
|
||||
useLocalTemplateDirectory: Bool
|
||||
quiet: Bool = false,
|
||||
shell: String? = nil,
|
||||
template: Configuration.Template? = nil,
|
||||
useLocalTemplateDirectory: Bool = false
|
||||
) {
|
||||
self.projectDirectory = projectDirectory
|
||||
self.template = template
|
||||
@@ -145,7 +169,7 @@ extension PlaybookClient.RunPlaybook: DependencyKey {
|
||||
public static var liveValue: PlaybookClient.RunPlaybook {
|
||||
.init(
|
||||
buildProject: { try await $0.run() },
|
||||
createProject: { try await $0.run() }
|
||||
createProject: { try await $0.run(encoder: $1) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import Foundation
|
||||
|
||||
enum PlaybookClientError: Error {
|
||||
case encodingError
|
||||
case projectDirectoryNotFound
|
||||
case templateDirectoryNotFound
|
||||
case templateDirectoryOrRepoNotSpecified
|
||||
|
||||
Reference in New Issue
Block a user