Root folder creation (#2)
This PR contains the work done to create the root folder of a new project when executing the _colibri_ executable. In addition, some other work has been done: * added the `ArgumentParser` package dependency to the package; * implemented the `FileService` service in the _library_ target; * implemented the `CreateRootFolderTask` task in the _library_ target; * removed some unnecessary comments and boilerplate code; Reviewed-on: #2 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 #2.
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
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
|
||||
|
||||
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||
func createFolder(with url: URL) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .createFolder(url),
|
||||
spy: spy
|
||||
)
|
||||
|
||||
// WHEN
|
||||
try await service.createFolder(at: url)
|
||||
|
||||
// THEN
|
||||
#expect(spy.isCreateFolderCalled == true)
|
||||
#expect(spy.urlCalled == url)
|
||||
}
|
||||
|
||||
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someRandomURL],
|
||||
[FileServiceError.urlAlreadyExists, .urlAlreadyExists, .urlNotFileURL]))
|
||||
func createFolder(
|
||||
with url: URL,
|
||||
throws error: FileServiceError
|
||||
) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .error(error),
|
||||
spy: spy
|
||||
)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.createFolder(at: url)
|
||||
}
|
||||
|
||||
#expect(spy.isCreateFolderCalled == false)
|
||||
#expect(spy.urlCalled == nil)
|
||||
}
|
||||
|
||||
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||
func delete(with url: URL) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .delete(url),
|
||||
spy: spy
|
||||
)
|
||||
|
||||
// WHEN
|
||||
try await service.delete(at: url)
|
||||
|
||||
// THEN
|
||||
#expect(spy.isDeleteCalled == true)
|
||||
#expect(spy.urlCalled == url)
|
||||
}
|
||||
|
||||
@Test(arguments: zip([URL.someNewFolder, .someNewFile, .someRandomURL],
|
||||
[FileServiceError.urlNotExists, .urlNotExists, .urlNotFileURL]))
|
||||
func delete(
|
||||
with url: URL,
|
||||
throws error: FileServiceError
|
||||
) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .error(error),
|
||||
spy: spy
|
||||
)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.delete(at: url)
|
||||
}
|
||||
|
||||
#expect(spy.isDeleteCalled == false)
|
||||
#expect(spy.urlCalled == nil)
|
||||
}
|
||||
|
||||
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNewFolder, .someNewFile],
|
||||
[true, true, false, false]))
|
||||
func exists(
|
||||
with url: URL,
|
||||
expects outcome: Bool
|
||||
) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .exists(url, outcome),
|
||||
spy: spy
|
||||
)
|
||||
|
||||
// WHEN
|
||||
let result = try await service.exists(at: url)
|
||||
|
||||
// THEN
|
||||
#expect(result == outcome)
|
||||
|
||||
#expect(spy.isExistsAtCalled == true)
|
||||
#expect(spy.urlCalled == url)
|
||||
}
|
||||
|
||||
@Test(arguments: zip([URL.someRandomURL], [FileServiceError.urlNotFileURL]))
|
||||
func exists(
|
||||
with url: URL,
|
||||
throws error: FileServiceError
|
||||
) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .error(error),
|
||||
spy: spy
|
||||
)
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.exists(at: url)
|
||||
}
|
||||
|
||||
#expect(spy.isExistsAtCalled == false)
|
||||
#expect(spy.urlCalled == nil)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import ColibriLibrary
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
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 fileService = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .createFolder(folder)
|
||||
)
|
||||
|
||||
let task = CreateRootFolderTask(fileService: fileService)
|
||||
|
||||
// WHEN
|
||||
let result = try await task(name: name,
|
||||
at: location)
|
||||
|
||||
// THEN
|
||||
#expect(result == folder)
|
||||
#expect(result.isFileURL == true)
|
||||
}
|
||||
|
||||
@Test(arguments: [String.someProjectName], [FileServiceError.urlAlreadyExists])
|
||||
func task(
|
||||
name: String,
|
||||
throws error: FileServiceError
|
||||
) async throws {
|
||||
// GIVEN
|
||||
let fileService = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .error(error)
|
||||
)
|
||||
|
||||
let task = CreateRootFolderTask(fileService: fileService)
|
||||
|
||||
// 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 fileService = FileServiceMock(currentFolder: .someCurrentFolder)
|
||||
|
||||
let task = CreateRootFolderTask(fileService: fileService)
|
||||
|
||||
// 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.appending(component: String.someProjectName)
|
||||
static let someDotProjectFolder = URL.someDotFolder.appending(component: String.someProjectName)
|
||||
static let someNewProjectFolder = URL.someNewFolder.appending(component: String.someProjectName)
|
||||
static let someTildeProjectFolder = URL.someTildeFolder.appending(component: String.someProjectName)
|
||||
}
|
||||
Reference in New Issue
Block a user