From 026251ad6d11a6725fe65244642fbdf842fd0599 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sat, 18 Jan 2025 04:25:01 +0100 Subject: [PATCH] Improved the implementation for the CreateFolderTask task in the library target. --- .../Internal/Enumerations/Folder.swift | 12 ++++++++ .../Public/Tasks/CreateFoldersTask.swift | 17 +---------- .../Public/Tasks/CreateFoldersTaskTests.swift | 30 +++++++++++++------ 3 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 Library/Sources/Internal/Enumerations/Folder.swift diff --git a/Library/Sources/Internal/Enumerations/Folder.swift b/Library/Sources/Internal/Enumerations/Folder.swift new file mode 100644 index 0000000..ce66129 --- /dev/null +++ b/Library/Sources/Internal/Enumerations/Folder.swift @@ -0,0 +1,12 @@ +enum Folder: String { + case app = "App/Sources" + case libraryPublic = "Library/Sources/Public" + case libraryInternal = "Library/Sources/Internal" + case testCasesPublic = "Test/Sources/Cases/Public" + case testCasesInternal = "Test/Sources/Cases/Internal" + case testHelpers = "Test/Sources/Helpers" +} + +// MARK: - CaseIterable + +extension Folder: CaseIterable {} diff --git a/Library/Sources/Public/Tasks/CreateFoldersTask.swift b/Library/Sources/Public/Tasks/CreateFoldersTask.swift index 2db9de9..3895fe6 100644 --- a/Library/Sources/Public/Tasks/CreateFoldersTask.swift +++ b/Library/Sources/Public/Tasks/CreateFoldersTask.swift @@ -15,7 +15,7 @@ public struct CreateFoldersTask { // MARK: Functions public func callAsFunction(at rootFolder: URL) async throws { - let folders = Self.foldersToCreate.map { rootFolder.appendingPath($0) } + let folders = Folder.allCases.map { rootFolder.appendingPath($0.rawValue) } for folder in folders { try await fileService.createFolder(at: folder) @@ -23,18 +23,3 @@ public struct CreateFoldersTask { } } - -// MARK: - Helpers - -extension CreateFoldersTask { - - // MARK: Constants - - static let foldersToCreate: [String] = [ - "Sources/App", - "Sources/AppInfrastructure", - "Tests/App/Cases", - "Tests/App/Sources" - ] - -} diff --git a/Test/Sources/Cases/Public/Tasks/CreateFoldersTaskTests.swift b/Test/Sources/Cases/Public/Tasks/CreateFoldersTaskTests.swift index 396ac89..ddbfa29 100644 --- a/Test/Sources/Cases/Public/Tasks/CreateFoldersTaskTests.swift +++ b/Test/Sources/Cases/Public/Tasks/CreateFoldersTaskTests.swift @@ -14,24 +14,36 @@ struct CreateFoldersTaskTests { @Test(arguments: [URL.someCurrentFolder, .someDotFolder, .someTildeFolder]) func createFolders(with rootFolder: URL) async throws { // GIVEN - let folders = CreateFoldersTask.foldersToCreate.map { rootFolder.appendingPath($0) } - let actions: [FileServiceMock.Action] = folders.map { .createFolder($0) } + let folders = Folder.allCases.map { rootFolder.appendingPath($0.rawValue) } + let actions = folders.map { FileServiceMock.Action.createFolder($0) } - let service = FileServiceMock( - currentFolder: .someCurrentFolder, - actions: actions, - spy: spy - ) - - let createFolders = CreateFoldersTask(fileService: service) + 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 + )) + } + +}