Template support for input parameters #4
53
Sources/Library/Tasks/CopyFilesTask.swift
Normal file
53
Sources/Library/Tasks/CopyFilesTask.swift
Normal file
@ -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"
|
||||||
|
}
|
43
Tests/Library/Cases/Tasks/CopyFilesTaskTests.swift
Normal file
43
Tests/Library/Cases/Tasks/CopyFilesTaskTests.swift
Normal file
@ -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]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user