Implemented the "delete(at: )" function for the FileService service in the module target.
This commit is contained in:
parent
7d0ad3461a
commit
58151a4e5a
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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")!
|
||||
|
Loading…
x
Reference in New Issue
Block a user