diff --git a/Sources/Library/Protocols/FileServicing.swift b/Sources/Library/Protocols/FileServicing.swift index cff10db..9f8d20a 100644 --- a/Sources/Library/Protocols/FileServicing.swift +++ b/Sources/Library/Protocols/FileServicing.swift @@ -8,6 +8,7 @@ public protocol FileServicing { // MARK: Functions + func delete(at url: URL) async throws (FileServiceError) func exists(at url: URL) async throws (FileServiceError) -> Bool } @@ -16,8 +17,8 @@ public protocol FileServicing { public enum FileServiceError: Error, Equatable { case folderNotCreated - case folderNotDeleted case urlAlreadyExists + case urlNotDeleted case urlNotExists case urlNotFileURL } diff --git a/Sources/Library/Services/FileService.swift b/Sources/Library/Services/FileService.swift index 4c164e7..d1d6397 100644 --- a/Sources/Library/Services/FileService.swift +++ b/Sources/Library/Services/FileService.swift @@ -20,6 +20,20 @@ public struct FileService: FileServicing { } } + // MARK: Functions + + public func delete(at url: URL) async throws (FileServiceError) { + guard try await exists(at: url) else { + throw FileServiceError.urlNotExists + } + + do { + try fileManager.removeItem(at: url) + } catch { + throw FileServiceError.urlNotDeleted + } + } + public func exists(at url: URL) async throws (FileServiceError) -> Bool { guard url.isFileURL else { throw FileServiceError.urlNotFileURL diff --git a/Tests/Library/Services/FileServiceTests.swift b/Tests/Library/Services/FileServiceTests.swift index 7dde431..9193264 100644 --- a/Tests/Library/Services/FileServiceTests.swift +++ b/Tests/Library/Services/FileServiceTests.swift @@ -26,9 +26,34 @@ struct FileServiceTests { #expect(url == .someExistingFolder) #expect(url.isFileURL == true) } + @Test(arguments: [URL.someNewFolder, .someNewFile]) + func delete(with url: URL) async throws { + // GIVEN + if try await !service.exists(at: url) { + try await service.createFolder(at: url) + } + + // WHEN + try await service.delete(at: url) + + // THEN + let result = try await service.exists(at: url) + + #expect(result == false) + } + + @Test(arguments: zip([URL.someNonExistingFolder, .someNonExistingFile, .someRandomURL], + [FileServiceError.urlNotExists, .urlNotExists, .urlNotFileURL])) + func deleteThrows( + with url: URL, + expects error: FileServiceError + ) async throws { // GIVEN // WHEN // THEN + await #expect(throws: error) { + try await service.delete(at: url) + } } @Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNonExistingFolder, .someNonExistingFile], @@ -62,7 +87,9 @@ struct FileServiceTests { private extension URL { static let someExistingFolder = URL(at: "/private/tmp") - static let someExistingFile = URL(at: "/etc/null") + static let someExistingFile = URL(at: "/etc/ssh/ssh_config") + static let someNewFolder = URL(at: "/private/tmp/folder") + static let someNewFile = URL(at: "/private/tmp/file.ext") static let someNonExistingFolder = URL(at: "/some/random/folder") static let someNonExistingFile = URL(at: "/some/random/file.ext") static let someRandomURL = URL(string: "https://some.random.url")!