Template support for input parameters (#4)
This PR contains the work done to support input parameters for the `create` command of the executable target, and to render content dynamically for the newly-generated project. Reviewed-on: #4 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #4.
This commit is contained in:
@@ -53,45 +53,37 @@ struct FileTests {
|
||||
private extension FileTests {
|
||||
enum Expectation {
|
||||
static let fileNames: [String] = [
|
||||
"App.swift",
|
||||
"AppArguments.swift",
|
||||
"AppBuilder.swift",
|
||||
"AppOptions.swift",
|
||||
"AppTests.swift",
|
||||
"Dockerfile",
|
||||
".dockerignore",
|
||||
"Environment+Properties.swift",
|
||||
".gitignore",
|
||||
"LICENSE",
|
||||
"LoggerLevel+Conformances.swift",
|
||||
"Package.swift",
|
||||
"README.md",
|
||||
"TestArguments.swift"
|
||||
]
|
||||
|
||||
static let filePaths: [String] = [
|
||||
"App/Sources/App.swift",
|
||||
"Library/Sources/Public/AppArguments.swift",
|
||||
"Library/Sources/Public/AppBuilder.swift",
|
||||
"App/Sources/AppOptions.swift",
|
||||
"Test/Sources/Cases/Public/AppTests.swift",
|
||||
"Dockerfile",
|
||||
".dockerignore",
|
||||
"Library/Sources/Internal/Environment+Properties.swift",
|
||||
".gitignore",
|
||||
"LICENSE",
|
||||
"Library/Sources/Internal/LoggerLevel+Conformances.swift",
|
||||
"Package.swift",
|
||||
"README.md",
|
||||
"Test/Sources/Helpers/TestArguments.swift"
|
||||
]
|
||||
|
||||
static let folders: [Folder] = [
|
||||
.app,
|
||||
.libraryPublic,
|
||||
.libraryPublic,
|
||||
.app,
|
||||
.testCasesPublic,
|
||||
.root,
|
||||
.root,
|
||||
.libraryInternal,
|
||||
@@ -99,17 +91,13 @@ private extension FileTests {
|
||||
.root,
|
||||
.libraryInternal,
|
||||
.root,
|
||||
.root,
|
||||
.testHelpers
|
||||
]
|
||||
|
||||
|
||||
static let resourcePaths: [String] = [
|
||||
"Resources/Files/Sources/App",
|
||||
"Resources/Files/Sources/Library",
|
||||
"Resources/Files/Sources/Library",
|
||||
"Resources/Files/Sources/App",
|
||||
"Resources/Files/Sources/Test",
|
||||
"Resources/Files/Sources",
|
||||
"Resources/Files/Sources",
|
||||
"Resources/Files/Sources/Library",
|
||||
@@ -117,9 +105,7 @@ private extension FileTests {
|
||||
"Resources/Files/Sources",
|
||||
"Resources/Files/Sources/Library",
|
||||
"Resources/Files/Sources",
|
||||
"Resources/Files/Sources",
|
||||
"Resources/Files/Sources/Test"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct TemplateTests {
|
||||
|
||||
// MARK: Properties tests
|
||||
|
||||
@Test(arguments: zip(Template.allCases, Expectation.fileNames))
|
||||
func fileName(for template: Template, expects fileName: String) async throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let result = template.fileName
|
||||
|
||||
// THEN
|
||||
#expect(result == fileName)
|
||||
}
|
||||
|
||||
@Test(arguments: zip(Template.allCases, Expectation.filePaths))
|
||||
func filePath(for template: Template, expects filePath: String) async throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let result = template.filePath
|
||||
|
||||
// THEN
|
||||
#expect(result == filePath)
|
||||
}
|
||||
|
||||
@Test(arguments: zip(Template.allCases, Expectation.folders))
|
||||
func folder(for template: Template, expects folder: Folder) async throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let result = template.folder
|
||||
|
||||
// THEN
|
||||
#expect(result == folder)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Expectations
|
||||
|
||||
private extension TemplateTests {
|
||||
enum Expectation {
|
||||
static let fileNames: [String] = [
|
||||
"App.swift",
|
||||
"AppTests.swift",
|
||||
"Package.swift",
|
||||
]
|
||||
|
||||
static let filePaths: [String] = [
|
||||
"App/Sources/App.swift",
|
||||
"Test/Sources/Cases/Public/AppTests.swift",
|
||||
"Package.swift",
|
||||
]
|
||||
|
||||
static let folders: [Folder] = [
|
||||
.app,
|
||||
.testCasesPublic,
|
||||
.root,
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct RunProcessTaskTests {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private var process: Process
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init() {
|
||||
self.process = Process()
|
||||
}
|
||||
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test(arguments: [Argument.empty, Argument.listAllInFolder])
|
||||
func run(with arguments: [String]) async throws {
|
||||
// GIVEN
|
||||
var task = RunProcessTask(process: process)
|
||||
|
||||
// WHEN
|
||||
let output = try await task(path: .ls, arguments: arguments)
|
||||
|
||||
// THEN
|
||||
#expect(output.isEmpty == false)
|
||||
}
|
||||
|
||||
@Test(arguments: zip([Argument.help, Argument.listAllInPWD], Throw.outputs))
|
||||
func runThrows(with arguments: [String], throws error: RunProcessError) async throws {
|
||||
// GIVEN
|
||||
var task = RunProcessTask(process: process)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await task(path: .ls, arguments: arguments)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let ls = "/bin/ls"
|
||||
}
|
||||
|
||||
// MARK: - Parameters
|
||||
|
||||
private extension RunProcessTaskTests {
|
||||
enum Argument {
|
||||
static let empty: [String] = []
|
||||
static let help: [String] = ["--help"]
|
||||
static let listAllInFolder: [String] = ["-la", "."]
|
||||
static let listAllInPWD: [String] = ["-la", "~"]
|
||||
}
|
||||
|
||||
enum Throw {
|
||||
static let outputs: [RunProcessError] = [
|
||||
.output("ls: unrecognized option `--help\'\nusage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]\n"),
|
||||
.output("ls: ~: No such file or directory\n")
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ struct FileServiceTests {
|
||||
// WHEN
|
||||
try await service.copyFile(from: source, to: destination)
|
||||
|
||||
// THENn
|
||||
// THEN
|
||||
#expect(spy.actions.count == 1)
|
||||
|
||||
let action = try #require(spy.actions.last)
|
||||
@@ -57,6 +57,37 @@ struct FileServiceTests {
|
||||
#expect(spy.actions.isEmpty == true)
|
||||
}
|
||||
|
||||
@Test(arguments: zip([URL.someNewFile],
|
||||
[Data("some data goes here...".utf8)]))
|
||||
func createFile(with location: URL, and data: Data) async throws {
|
||||
// GIVEN
|
||||
let service = service(action: .createFile(location, data))
|
||||
|
||||
// WHEN
|
||||
try await service.createFile(at: location, with: data)
|
||||
|
||||
// THEN
|
||||
#expect(spy.actions.count == 1)
|
||||
|
||||
let action = try #require(spy.actions.last)
|
||||
|
||||
#expect(action == .fileCreated(location, data))
|
||||
}
|
||||
|
||||
@Test(arguments: [FileServiceError.itemAlreadyExists, .fileDataIsEmpty, .fileNotCreated])
|
||||
func createFile(throws error: FileServiceError) async throws {
|
||||
// GIVEN
|
||||
let service = service(action: .error(error))
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.createFile(at: .someNewFile, with: .init())
|
||||
}
|
||||
|
||||
#expect(spy.actions.isEmpty == true)
|
||||
}
|
||||
|
||||
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||
func createFolder(with location: URL) async throws {
|
||||
// GIVEN
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
struct TemplateServiceTests {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let spy = TemplateServiceSpy()
|
||||
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test(arguments: [String.content])
|
||||
func render(_ content: String) async throws {
|
||||
// GIVEN
|
||||
let service = TemplateServiceMock(action: .render(content), spy: spy)
|
||||
|
||||
// WHEN
|
||||
let result = try await service.render([:], on: .template)
|
||||
|
||||
// THEN
|
||||
#expect(result == content)
|
||||
|
||||
#expect(spy.actions.isEmpty == false)
|
||||
}
|
||||
|
||||
@Test(arguments: [TemplateServiceError.serviceNotInitialized, .resourcePathNotFound, .templateNotFound, .contentNotRendered])
|
||||
func render(throws error: TemplateServiceError) async throws {
|
||||
// GIVEN
|
||||
let service = TemplateServiceMock(action: .error(error), spy: spy)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.render([:], on: .template)
|
||||
}
|
||||
|
||||
#expect(spy.actions.isEmpty == true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let content = ""
|
||||
static let template = ""
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
struct TerminalServiceTests {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let spy = TerminalServiceSpy()
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
@Test(arguments: [URL.someNewFile], [[], ["--example"], ["--example", "--more", "--etc"]])
|
||||
func run(with executableURL: URL, and arguments: [String]) async throws {
|
||||
// GIVEN
|
||||
let service = TerminalServiceMock(action: .run(executableURL, arguments), spy: spy)
|
||||
|
||||
// WHEN
|
||||
let result = try await service.run(executableURL, arguments: arguments)
|
||||
|
||||
// THEN
|
||||
#expect(result == .content)
|
||||
|
||||
#expect(spy.actions.isEmpty == false)
|
||||
|
||||
let action = try #require(spy.actions.last)
|
||||
|
||||
#expect(action == .ran(executableURL, arguments))
|
||||
}
|
||||
|
||||
@Test(arguments: [TerminalServiceError.unexpected, .captured("Some captured error"), .output("Some output error")])
|
||||
func run(throws error: TerminalServiceError) async throws {
|
||||
// GIVEN
|
||||
let service = TerminalServiceMock(action: .error(error), spy: spy)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.run(URL.someNewFile, arguments: [])
|
||||
}
|
||||
|
||||
#expect(spy.actions.isEmpty == true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let content = ""
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct InitGitInFolderTaskTests {
|
||||
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test(arguments: [URL.someCurrentFolder, .someNewFolder, .someDotFolder, .someTildeFolder])
|
||||
func task(at rootFolder: URL) async throws {
|
||||
// GIVEN
|
||||
let terminalService = TerminalServiceSpy()
|
||||
|
||||
let initGitInFolder = InitGitInFolderTask(terminalService: terminalService)
|
||||
|
||||
// WHEN
|
||||
try await initGitInFolder(at: rootFolder)
|
||||
|
||||
// THEN
|
||||
let executableURL = URL(at: "/usr/bin/git")
|
||||
let pathFolder = rootFolder.pathString
|
||||
|
||||
#expect(terminalService.actions.count == 3)
|
||||
#expect(terminalService.actions[0] == .ran(executableURL, ["init", pathFolder]))
|
||||
#expect(terminalService.actions[1] == .ran(executableURL, ["-C", pathFolder, "add", "."]))
|
||||
#expect(terminalService.actions[2] == .ran(executableURL, ["-C", pathFolder, "commit", "-m", "Initial commit"]))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
struct RenderFilesTaskTests {
|
||||
|
||||
@Test(arguments: [URL.someCurrentFolder], [Project(name: "Some name goes here...")])
|
||||
func task(at rootFolder: URL, with project: Project) async throws {
|
||||
// GIVEN
|
||||
let fileService = FileServiceSpy()
|
||||
let templateService = TemplateServiceSpy()
|
||||
|
||||
let renderFiles = RenderFilesTask(fileService: fileService,
|
||||
templateService: templateService)
|
||||
|
||||
// WHEN
|
||||
try await renderFiles(at: rootFolder, with: project)
|
||||
|
||||
// THEN
|
||||
let fileData = Data()
|
||||
let templates = Template.allCases
|
||||
|
||||
#expect(fileService.actions.count == 3)
|
||||
#expect(templateService.actions.count == 3)
|
||||
|
||||
fileService.actions.enumerated().forEach { index, action in
|
||||
#expect(action == .fileCreated(rootFolder.appendingPath(templates[index].filePath), fileData))
|
||||
}
|
||||
|
||||
templateService.actions.enumerated().forEach { index, action in
|
||||
if case let .rendered(object, template) = action {
|
||||
#expect(object as? Project == project)
|
||||
#expect(template == templates[index].rawValue)
|
||||
} else {
|
||||
Issue.record("Action should have been a case of the `TemplateServiceSpy.Action` enumeration.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -64,6 +64,19 @@ extension FileServiceMock: FileServicing {
|
||||
}
|
||||
}
|
||||
|
||||
func createFile(at location: URL, with data: Data) async throws (FileServiceError) {
|
||||
guard let nextAction else { return }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
case let .createFile(location, data):
|
||||
try await spy?.createFile(at: location, with: data)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func createFolder(at location: URL) async throws (FileServiceError) {
|
||||
guard let nextAction else { return }
|
||||
|
||||
@@ -127,6 +140,7 @@ private extension FileServiceMock {
|
||||
extension FileServiceMock {
|
||||
enum Action {
|
||||
case copyFile(URL, URL)
|
||||
case createFile(URL, Data)
|
||||
case createFolder(URL)
|
||||
case deleteItem(URL)
|
||||
case error(FileServiceError)
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
|
||||
final class TemplateServiceMock {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private var actions: [Action] = []
|
||||
|
||||
private weak var spy: TemplateServiceSpy?
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(
|
||||
action: Action,
|
||||
spy: TemplateServiceSpy? = nil
|
||||
) {
|
||||
self.actions.append(action)
|
||||
self.spy = spy
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TemplateServicing
|
||||
|
||||
extension TemplateServiceMock: TemplateServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
@discardableResult
|
||||
func render(_ object: Any, on template: String) async throws(TemplateServiceError) -> String {
|
||||
guard let nextAction else { return .empty }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
case .render(let content):
|
||||
try await spy?.render(object, on: template)
|
||||
return content
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension TemplateServiceMock {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
var nextAction: Action? {
|
||||
guard !actions.isEmpty else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return actions.removeFirst()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
extension TemplateServiceMock {
|
||||
enum Action {
|
||||
case error(TemplateServiceError)
|
||||
case render(String)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let empty = ""
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
|
||||
final class TerminalServiceMock {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private var actions: [Action] = []
|
||||
|
||||
private weak var spy: TerminalServiceSpy?
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(
|
||||
action: Action,
|
||||
spy: TerminalServiceSpy? = nil
|
||||
) {
|
||||
self.actions.append(action)
|
||||
self.spy = spy
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TerminalServicing
|
||||
|
||||
extension TerminalServiceMock: TerminalServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func run(_ executableURL: URL, arguments: [String]) async throws (TerminalServiceError) -> String {
|
||||
guard let nextAction else { return .empty }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
case let .run(executableURL, arguments):
|
||||
try await spy?.run(executableURL, arguments: arguments)
|
||||
return .empty
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension TerminalServiceMock {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
var nextAction: Action? {
|
||||
guard !actions.isEmpty else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return actions.removeFirst()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
extension TerminalServiceMock {
|
||||
enum Action {
|
||||
case error(TerminalServiceError)
|
||||
case run(URL, [String])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let empty = ""
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
|
||||
@testable import ColibriLibrary
|
||||
|
||||
final class FileServiceSpy {
|
||||
|
||||
// MARK: Properties
|
||||
@@ -22,6 +21,10 @@ extension FileServiceSpy: FileServicing {
|
||||
actions.append(.fileCopied(source, destination))
|
||||
}
|
||||
|
||||
func createFile(at location: URL, with data: Data) async throws (FileServiceError) {
|
||||
actions.append(.fileCreated(location, data))
|
||||
}
|
||||
|
||||
func createFolder(at location: URL) async throws (FileServiceError) {
|
||||
actions.append(.folderCreated(location))
|
||||
}
|
||||
@@ -43,6 +46,7 @@ extension FileServiceSpy: FileServicing {
|
||||
|
||||
extension FileServiceSpy {
|
||||
enum Action: Equatable {
|
||||
case fileCreated(_ location: URL, _ data: Data)
|
||||
case fileCopied(_ source: URL, _ destination: URL)
|
||||
case folderCreated(_ location: URL)
|
||||
case itemDeleted(_ location: URL)
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import ColibriLibrary
|
||||
|
||||
final class TemplateServiceSpy {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private(set) var actions: [Action] = []
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TemplateServicing
|
||||
|
||||
extension TemplateServiceSpy: TemplateServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
@discardableResult
|
||||
func render(_ object: Any, on template: String) async throws (TemplateServiceError) -> String {
|
||||
actions.append(.rendered(object, template))
|
||||
|
||||
return .content
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
extension TemplateServiceSpy {
|
||||
enum Action {
|
||||
case rendered(_ object: Any, _ template: String)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let content = ""
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
|
||||
final class TerminalServiceSpy {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private(set) var actions: [Action] = []
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TerminalServicing
|
||||
|
||||
extension TerminalServiceSpy: TerminalServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
@discardableResult
|
||||
func run(_ executableURL: URL, arguments: [String]) async throws(TerminalServiceError) -> String {
|
||||
actions.append(.ran(executableURL, arguments))
|
||||
|
||||
return .content
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Actions
|
||||
|
||||
extension TerminalServiceSpy {
|
||||
enum Action: Equatable {
|
||||
case ran(_ executableURL: URL, _ arguments: [String])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
static let content = ""
|
||||
}
|
||||
Reference in New Issue
Block a user