Basic project creation (#3)

This PR contains the work done to create a new *Hummingbird* project with very basic configuration from the _colibri_ executable, just like the project you could create with the [Hummingbird template](https://github.com/hummingbird-project/template) project in Github.

Reviewed-on: #3
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 #3.
This commit is contained in:
2025-01-28 00:07:24 +00:00
committed by Javier Cicchelli
parent b8c354e614
commit 9be8fa4a31
52 changed files with 1936 additions and 475 deletions
@@ -0,0 +1,181 @@
import ColibriLibrary
import Foundation
import Testing
struct FileServiceTests {
// MARK: Properties
private let spy = FileServiceSpy()
// MARK: Properties tests
@Test func currentFolder() async {
// GIVEN
let url: URL = .someCurrentFolder
let service = FileServiceMock(currentFolder: url)
// WHEN
let folder = await service.currentFolder
// THEN
#expect(folder == url)
#expect(folder.isFileURL == true)
}
// MARK: Functions tests
@Test(arguments: zip([URL.someExistingFile, .someExistingFolder],
[URL.someNewFile, .someNewFolder]))
func copyFile(from source: URL, to destination: URL) async throws {
// GIVEN
let service = service(action: .copyFile(source, destination))
// WHEN
try await service.copyFile(from: source, to: destination)
// THENn
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .fileCopied(source, destination))
}
@Test(arguments: [FileServiceError.itemAlreadyExists, .itemEmptyData, .itemNotCopied])
func copyItem(throws error: FileServiceError) async throws {
// GIVEN
let service = service(action: .error(error))
// WHEN
// THEN
await #expect(throws: error) {
try await service.copyFile(from: .someExistingFile, to: .someNewFile)
}
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: [URL.someNewFolder, .someNewFile])
func createFolder(with location: URL) async throws {
// GIVEN
let service = service(action: .createFolder(location))
// WHEN
try await service.createFolder(at: location)
// THEN
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .folderCreated(location))
}
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someRandomURL],
[FileServiceError.itemAlreadyExists, .itemAlreadyExists, .itemNotFileURL]))
func createFolder(
with location: URL,
throws error: FileServiceError
) async throws {
// GIVEN
let service = service(action: .error(error))
// WHEN
// THEN
await #expect(throws: error) {
try await service.createFolder(at: location)
}
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: [URL.someNewFolder, .someNewFile])
func deleteItem(with location: URL) async throws {
// GIVEN
let service = service(action: .deleteItem(location))
// WHEN
try await service.deleteItem(at: location)
// THEN
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .itemDeleted(location))
}
@Test(arguments: zip([URL.someNewFolder, .someNewFile, .someRandomURL],
[FileServiceError.itemNotExists, .itemNotExists, .itemNotFileURL]))
func deleteItem(
with location: URL,
throws error: FileServiceError
) async throws {
// GIVEN
let service = service(action: .error(error))
// WHEN
// THEN
await #expect(throws: error) {
try await service.deleteItem(at: location)
}
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNewFolder, .someNewFile],
[true, true, false, false]))
func isItemExists(
with location: URL,
expects outcome: Bool
) async throws {
// GIVEN
let service = service(action: .isItemExists(location, outcome))
// WHEN
let result = try await service.isItemExists(at: location)
// THEN
#expect(result == outcome)
let action = try #require(spy.actions.last)
#expect(action == .itemExists(location))
}
@Test(arguments: zip([URL.someRandomURL], [FileServiceError.itemNotFileURL]))
func isItemExists(
with location: URL,
throws error: FileServiceError
) async throws {
// GIVEN
let service = service(action: .error(error))
// WHEN
// THEN
await #expect(throws: error) {
try await service.isItemExists(at: location)
}
#expect(spy.actions.isEmpty == true)
}
}
// MARK: - Helpers
private extension FileServiceTests {
// MARK: Functions
func service(action: FileServiceMock.Action) -> FileServiceMock {
.init(
currentFolder: .someCurrentFolder,
action: action,
spy: spy
)
}
}
@@ -0,0 +1,80 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct CopyFilesTaskTests {
// MARK: Properties
private let resourceFolder = URL.someExistingFolder
private let rootFolder = URL.someNewFolder
private let spy = FileServiceSpy()
// MARK: Functions tests
@Test func copyFiles() async throws {
// GIVEN
let files = files(of: File.allCases)
let actions = files.map { FileServiceMock.Action.copyFile($0.source, $0.destination) }
let copyFiles = task(actions: actions)
// WHEN
try await copyFiles(to: rootFolder)
// THEN
#expect(spy.actions.count == actions.count)
files.enumerated().forEach { index, file in
#expect(spy.actions[index] == .fileCopied(file.source, file.destination))
}
}
@Test(arguments: [FileServiceError.itemAlreadyExists, .itemEmptyData, .itemNotCopied])
func copyFiles(throws error: FileServiceError) async throws {
// GIVEN
let files = files(of: Array(File.allCases[0...2]))
let actions = files.map { FileServiceMock.Action.copyFile($0.source, $0.destination) }
let copyFiles = task(actions: actions + [.error(error)])
// WHEN
// THEN
await #expect(throws: error) {
try await copyFiles(to: rootFolder)
}
#expect(spy.actions.count == actions.count)
files.enumerated().forEach { index, file in
#expect(spy.actions[index] == .fileCopied(file.source, file.destination))
}
}
}
// MARK: - Helpers
private extension CopyFilesTaskTests {
// MARK: Type aliases
typealias FileURL = (source: URL, destination: URL)
// MARK: Functions
func files(of resourceFiles: [File]) -> [FileURL] {
resourceFiles.map { (resourceFolder.appendingPath($0.rawValue), rootFolder.appendingPath($0.fileName)) }
}
func task(actions: [FileServiceMock.Action]) -> CopyFilesTask {
.init(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
actions: actions,
spy: spy
))
}
}
@@ -0,0 +1,49 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct CreateFoldersTaskTests {
// MARK: Properties
private let spy = FileServiceSpy()
// MARK: Functions tests
@Test(arguments: [URL.someCurrentFolder, .someDotFolder, .someTildeFolder])
func createFolders(with rootFolder: URL) async throws {
// GIVEN
let folders = Folder.allCases.map { rootFolder.appendingPath($0.path) }
let actions = folders.map { FileServiceMock.Action.createFolder($0) }
let createFolders = task(actions: actions)
// WHEN
try await createFolders(at: rootFolder)
// THEN
#expect(spy.actions.count == actions.count)
for index in actions.indices {
#expect(spy.actions[index] == .folderCreated(folders[index]))
}
}
}
// MARK: - Helpers
private extension CreateFoldersTaskTests {
// MARK: Functions
func task(actions: [FileServiceMock.Action]) -> CreateFoldersTask {
.init(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
actions: actions,
spy: spy
))
}
}
@@ -0,0 +1,88 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct CreateRootFolderTaskTests {
// MARK: Functions tests
@Test(arguments: [String.someProjectName], [URL.someCurrentProjectFolder, .someNewProjectFolder, .someDotProjectFolder, .someTildeProjectFolder])
func task(
name: String,
expects folder: URL
) async throws {
// GIVEN
let location: URL? = switch folder {
case .someNewProjectFolder: .someNewFolder
case .someDotProjectFolder: .someDotFolder
case .someTildeProjectFolder: .someTildeFolder
default: nil
}
let task = CreateRootFolderTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
action: .createFolder(folder)
))
// WHEN
let result = try await task(name: name,
at: location)
// THEN
#expect(result == folder)
#expect(result.isFileURL == true)
}
@Test(arguments: [String.someProjectName], [FileServiceError.itemAlreadyExists])
func task(
name: String,
throws error: FileServiceError
) async throws {
// GIVEN
let task = CreateRootFolderTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
action: .error(error)
))
// WHEN
// THEN
await #expect(throws: error) {
try await task(name: name)
}
}
@Test(arguments: [String.someEmptyName], [CreateRootFolderError.nameIsEmpty])
func task(
name: String,
throws error: CreateRootFolderError
) async throws {
// GIVEN
let task = CreateRootFolderTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder
))
// WHEN
// THEN
await #expect(throws: error) {
try await task(name: name)
}
}
}
// MARK: - String+Constants
private extension String {
static let someEmptyName = ""
static let someProjectName = "SomeProjectName"
}
// MARK: - URL+Constants
private extension URL {
static let someCurrentProjectFolder = URL.someCurrentFolder.appendingPath(.someProjectName)
static let someDotProjectFolder = URL.someDotFolder.appendingPath(.someProjectName)
static let someNewProjectFolder = URL.someNewFolder.appendingPath(.someProjectName)
static let someTildeProjectFolder = URL.someTildeFolder.appendingPath(.someProjectName)
}