Implemented the CopyFilesTask task in the library module.

This commit is contained in:
Javier Cicchelli 2025-01-18 03:06:39 +01:00
parent 647c5bd32a
commit 1466bff250
6 changed files with 78 additions and 67 deletions

View File

@ -16,7 +16,7 @@ extension URL {
var pathString: String { var pathString: String {
if #available(macOS 13.0, *) { if #available(macOS 13.0, *) {
path() path(percentEncoded: true)
} else { } else {
path path
} }

View File

@ -23,9 +23,6 @@ public struct FileService: FileServicing {
// MARK: Functions // MARK: Functions
public func copyFile(from source: URL, to destination: URL) async throws (FileServiceError) { public func copyFile(from source: URL, to destination: URL) async throws (FileServiceError) {
guard try await isItemExists(at: source) else {
throw FileServiceError.itemNotExists
}
guard try await !isItemExists(at: destination) else { guard try await !isItemExists(at: destination) else {
throw FileServiceError.itemAlreadyExists throw FileServiceError.itemAlreadyExists
} }

View File

@ -4,50 +4,36 @@ public struct CopyFilesTask {
// MARK: Properties // MARK: Properties
private let bundleService: BundleServicing
private let fileService: FileServicing private let fileService: FileServicing
// MARK: Initialisers // MARK: Initialisers
public init(fileService: FileServicing) { public init(
bundleService: BundleServicing? = nil,
fileService: FileServicing
) {
self.bundleService = bundleService ?? Bundle.module
self.fileService = fileService self.fileService = fileService
} }
// MARK: Functions // MARK: Functions
public func callAsFunction(to rootFolder: URL) async throws { public func callAsFunction(to rootFolder: URL) async throws (FileServiceError) {
let filesFolder = URL(at: .folderFiles) for resource in ResourceFile.allCases {
guard let source = bundleService.url(
for fileToCopy in Self.filesToCopy { forResource: resource.rawValue,
try await fileService.copyItem( withExtension: nil,
from: filesFolder.appendingPath(fileToCopy), subdirectory: "Resources/Files"
to: rootFolder.appendingPath(fileToCopy) ) else {
) assertionFailure("URL should have been initialized.")
return
}
let destination = rootFolder.appendingPath(resource.fileName)
try await fileService.copyFile(from: source, to: destination)
} }
} }
} }
// 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"
}

View File

@ -31,19 +31,10 @@ extension CreateFoldersTask {
// MARK: Constants // MARK: Constants
static let foldersToCreate: [String] = [ static let foldersToCreate: [String] = [
.folderApp, "Sources/App",
.folderAppInfrastructure, "Sources/AppInfrastructure",
.folderAppTestCases, "Tests/App/Cases",
.folderAppTestSources "Tests/App/Sources"
] ]
} }
// MARK: - String+Constants
private extension String {
static let folderApp = "Sources/App"
static let folderAppInfrastructure = "Sources/AppInfrastructure"
static let folderAppTestCases = "Tests/App/Cases"
static let folderAppTestSources = "Tests/App/Sources"
}

View File

@ -47,7 +47,7 @@ struct FileServiceTests {
#expect(action == .fileCopied(source, destination)) #expect(action == .fileCopied(source, destination))
} }
@Test(arguments: [FileServiceError.itemNotExists, .itemAlreadyExists, .itemEmptyData, .itemNotCopied]) @Test(arguments: [FileServiceError.itemAlreadyExists, .itemEmptyData, .itemNotCopied])
func copyItem(throws error: FileServiceError) async throws { func copyItem(throws error: FileServiceError) async throws {
// GIVEN // GIVEN
let service = FileServiceMock( let service = FileServiceMock(

View File

@ -7,37 +7,74 @@ struct CopyFilesTaskTests {
// MARK: Properties // MARK: Properties
private let resourceFolder = URL.someExistingFolder
private let rootFolder = URL.someNewFolder
private let spy = FileServiceSpy() private let spy = FileServiceSpy()
// MARK: Functions tests // MARK: Functions tests
@Test(arguments: zip([URL.someExistingFolder], [URL.someNewFolder])) @Test func copyFiles() async throws {
func copyFiles(from source: URL, to destination: URL) async throws {
// GIVEN // GIVEN
let filesToCopy = CopyFilesTask.filesToCopy let files = files(of: ResourceFile.allCases)
let destinations = filesToCopy.map { destination.appendingPath($0) } let actions = files.map { FileServiceMock.Action.copyFile($0.source, $0.destination) }
let sources = filesToCopy.map { source.appendingPath($0) }
let actions = filesToCopy.indices.map { index -> FileServiceMock.Action in
.copyItem(sources[index], destinations[index])
}
let service = FileServiceMock( let copyFiles = CopyFilesTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder, currentFolder: .someCurrentFolder,
actions: actions, actions: actions,
spy: spy spy: spy
) ))
let copyFiles = CopyFilesTask(fileService: service)
// WHEN // WHEN
try await copyFiles(to: destination) try await copyFiles(to: rootFolder)
// THEN // THEN
#expect(spy.actions.count == actions.count) #expect(spy.actions.count == actions.count)
for index in actions.indices { files.enumerated().forEach { index, file in
#expect(spy.actions[index] == .itemCopied(sources[index], destinations[index])) #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(ResourceFile.allCases[0...2]))
let actions = files.map { FileServiceMock.Action.copyFile($0.source, $0.destination) }
let copyFiles = CopyFilesTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
actions: actions + [.error(error)],
spy: spy
))
// 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 File = (source: URL, destination: URL)
// MARK: Functions
func files(of resourceFiles: [ResourceFile]) -> [File] {
resourceFiles.map { (resourceFolder.appendingPath($0.rawValue), rootFolder.appendingPath($0.fileName)) }
}
}