Template support for input parameters #4
@ -8,7 +8,7 @@ public protocol FileServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func copyItem(from source: URL, to destination: URL) async throws (FileServiceError)
|
||||
func copyFile(from source: URL, to destination: URL) async throws (FileServiceError)
|
||||
func createFolder(at location: URL) async throws (FileServiceError)
|
||||
func deleteItem(at location: URL) async throws (FileServiceError)
|
||||
func isItemExists(at location: URL) async throws (FileServiceError) -> Bool
|
||||
@ -19,8 +19,9 @@ public protocol FileServicing {
|
||||
|
||||
public enum FileServiceError: Error, Equatable {
|
||||
case folderNotCreated
|
||||
case itemNotCopied
|
||||
case itemAlreadyExists
|
||||
case itemEmptyData
|
||||
case itemNotCopied
|
||||
case itemNotDeleted
|
||||
case itemNotExists
|
||||
case itemNotFileURL
|
||||
|
@ -22,7 +22,7 @@ public struct FileService: FileServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
public func copyItem(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
|
||||
}
|
||||
@ -30,8 +30,16 @@ public struct FileService: FileServicing {
|
||||
throw FileServiceError.itemAlreadyExists
|
||||
}
|
||||
|
||||
var itemData: Data?
|
||||
|
||||
do {
|
||||
try fileManager.copyItem(at: source, to: destination)
|
||||
itemData = try Data(contentsOf: source)
|
||||
} catch {
|
||||
throw FileServiceError.itemEmptyData
|
||||
}
|
||||
|
||||
do {
|
||||
try itemData?.write(to: destination, options: .atomic)
|
||||
} catch {
|
||||
throw FileServiceError.itemNotCopied
|
||||
}
|
||||
|
@ -28,26 +28,26 @@ struct FileServiceTests {
|
||||
|
||||
@Test(arguments: zip([URL.someExistingFile, .someExistingFolder],
|
||||
[URL.someNewFile, .someNewFolder]))
|
||||
func copyItem(from source: URL, to destination: URL) async throws {
|
||||
func copyFile(from source: URL, to destination: URL) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
currentFolder: .someCurrentFolder,
|
||||
action: .copyItem(source, destination),
|
||||
action: .copyFile(source, destination),
|
||||
spy: spy
|
||||
)
|
||||
|
||||
// WHEN
|
||||
try await service.copyItem(from: source, to: destination)
|
||||
try await service.copyFile(from: source, to: destination)
|
||||
|
||||
// THENn
|
||||
#expect(spy.actions.count == 1)
|
||||
|
||||
let action = try #require(spy.actions.last)
|
||||
|
||||
#expect(action == .itemCopied(source, destination))
|
||||
#expect(action == .fileCopied(source, destination))
|
||||
}
|
||||
|
||||
@Test(arguments: [FileServiceError.itemNotExists, .itemAlreadyExists, .itemNotCopied])
|
||||
@Test(arguments: [FileServiceError.itemNotExists, .itemAlreadyExists, .itemEmptyData, .itemNotCopied])
|
||||
func copyItem(throws error: FileServiceError) async throws {
|
||||
// GIVEN
|
||||
let service = FileServiceMock(
|
||||
@ -59,7 +59,7 @@ struct FileServiceTests {
|
||||
// WHEN
|
||||
// THEN
|
||||
await #expect(throws: error) {
|
||||
try await service.copyItem(from: .someExistingFile, to: .someNewFile)
|
||||
try await service.copyFile(from: .someExistingFile, to: .someNewFile)
|
||||
}
|
||||
|
||||
#expect(spy.actions.isEmpty == true)
|
||||
|
@ -51,14 +51,14 @@ extension FileServiceMock: FileServicing {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func copyItem(from source: URL, to destination: URL) async throws (FileServiceError) {
|
||||
func copyFile(from source: URL, to destination: URL) async throws (FileServiceError) {
|
||||
guard let nextAction else { return }
|
||||
|
||||
switch nextAction {
|
||||
case .error(let error):
|
||||
throw error
|
||||
case let .copyItem(source, destination):
|
||||
try await spy?.copyItem(from: source, to: destination)
|
||||
case let .copyFile(source, destination):
|
||||
try await spy?.copyFile(from: source, to: destination)
|
||||
default:
|
||||
break
|
||||
}
|
||||
@ -126,7 +126,7 @@ private extension FileServiceMock {
|
||||
|
||||
extension FileServiceMock {
|
||||
enum Action {
|
||||
case copyItem(URL, URL)
|
||||
case copyFile(URL, URL)
|
||||
case createFolder(URL)
|
||||
case deleteItem(URL)
|
||||
case error(FileServiceError)
|
||||
|
@ -18,8 +18,8 @@ extension FileServiceSpy: FileServicing {
|
||||
get async { .someCurrentFolder }
|
||||
}
|
||||
|
||||
func copyItem(from source: URL, to destination: URL) async throws (FileServiceError) {
|
||||
actions.append(.itemCopied(source, destination))
|
||||
func copyFile(from source: URL, to destination: URL) async throws (FileServiceError) {
|
||||
actions.append(.fileCopied(source, destination))
|
||||
}
|
||||
|
||||
func createFolder(at location: URL) async throws (FileServiceError) {
|
||||
@ -43,8 +43,8 @@ extension FileServiceSpy: FileServicing {
|
||||
|
||||
extension FileServiceSpy {
|
||||
enum Action: Equatable {
|
||||
case fileCopied(_ source: URL, _ destination: URL)
|
||||
case folderCreated(_ location: URL)
|
||||
case itemCopied(_ source: URL, _ destination: URL)
|
||||
case itemDeleted(_ location: URL)
|
||||
case itemExists(_ location: URL)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user