Implemented the CopyFilesTask task in the library module.

This commit is contained in:
2025-01-18 03:06:39 +01:00
parent 647c5bd32a
commit 1466bff250
6 changed files with 78 additions and 67 deletions
@@ -47,7 +47,7 @@ struct FileServiceTests {
#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 {
// GIVEN
let service = FileServiceMock(
@@ -7,37 +7,74 @@ struct CopyFilesTaskTests {
// MARK: Properties
private let resourceFolder = URL.someExistingFolder
private let rootFolder = URL.someNewFolder
private let spy = FileServiceSpy()
// MARK: Functions tests
@Test(arguments: zip([URL.someExistingFolder], [URL.someNewFolder]))
func copyFiles(from source: URL, to destination: URL) async throws {
@Test func copyFiles() 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 files = files(of: ResourceFile.allCases)
let actions = files.map { FileServiceMock.Action.copyFile($0.source, $0.destination) }
let service = FileServiceMock(
let copyFiles = CopyFilesTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
actions: actions,
spy: spy
)
let copyFiles = CopyFilesTask(fileService: service)
))
// WHEN
try await copyFiles(to: destination)
try await copyFiles(to: rootFolder)
// THEN
#expect(spy.actions.count == actions.count)
for index in actions.indices {
#expect(spy.actions[index] == .itemCopied(sources[index], destinations[index]))
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(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)) }
}
}