diff --git a/Sources/Library/Tasks/CopyFilesTask.swift b/Sources/Library/Tasks/CopyFilesTask.swift new file mode 100644 index 0000000..182f19a --- /dev/null +++ b/Sources/Library/Tasks/CopyFilesTask.swift @@ -0,0 +1,53 @@ +import Foundation + +public struct CopyFilesTask { + + // MARK: Properties + + private let fileService: FileServicing + + // MARK: Initialisers + + public init(fileService: FileServicing) { + self.fileService = fileService + } + + // MARK: Functions + + public func callAsFunction(to rootFolder: URL) async throws { + let filesFolder = URL(at: .folderFiles) + + for fileToCopy in Self.filesToCopy { + try await fileService.copyItem( + from: filesFolder.appendingPath(fileToCopy), + to: rootFolder.appendingPath(fileToCopy) + ) + } + } + +} + +// MARK: - Helpers + +extension CopyFilesTask { + + // MARK: Constants + + static let filesToCopy: [String] = [ + .fileDockerIgnore, + .fileGitIgnore, + .fileLicense, + .fileReadme + ] + +} + +// MARK: - URL+Constants + +private extension String { + static let folderFiles = "./Resources/Files" + static let fileDockerIgnore = ".dockerignore" + static let fileGitIgnore = ".gitignore" + static let fileLicense = "LICENSE" + static let fileReadme = "README.md" +} diff --git a/Tests/Library/Cases/Tasks/CopyFilesTaskTests.swift b/Tests/Library/Cases/Tasks/CopyFilesTaskTests.swift new file mode 100644 index 0000000..b473717 --- /dev/null +++ b/Tests/Library/Cases/Tasks/CopyFilesTaskTests.swift @@ -0,0 +1,43 @@ +import Foundation +import Testing + +@testable import ColibriLibrary + +struct CopyFilesTaskTests { + + // MARK: Properties + + private let spy = FileServiceSpy() + + // MARK: Functions tests + + @Test(arguments: zip([URL.someExistingFolder], [URL.someNewFolder])) + func copyFiles(from source: URL, to destination: URL) async throws { + // GIVEN + let filesToCopy = CopyFilesTask.filesToCopy + let destinations = filesToCopy.map { destination.appendingPath($0) } + let sources = filesToCopy.map { source.appendingPath($0) } + let actions = filesToCopy.indices.map { index -> FileServiceMock.Action in + .copyItem(sources[index], destinations[index]) + } + + let service = FileServiceMock( + currentFolder: .someCurrentFolder, + actions: actions, + spy: spy + ) + + let copyFiles = CopyFilesTask(fileService: service) + + // WHEN + try await copyFiles(to: destination) + + // THEN + #expect(spy.actions.count == actions.count) + + for index in actions.indices { + #expect(spy.actions[index] == .itemCopied(sources[index], destinations[index])) + } + } + +}