Implemented the "createFolder(at: )" function for the FileService service in the module target.
This commit is contained in:
parent
58151a4e5a
commit
1d080cce45
@ -8,6 +8,7 @@ public protocol FileServicing {
|
|||||||
|
|
||||||
// MARK: Functions
|
// MARK: Functions
|
||||||
|
|
||||||
|
func createFolder(at url: URL) async throws (FileServiceError)
|
||||||
func delete(at url: URL) async throws (FileServiceError)
|
func delete(at url: URL) async throws (FileServiceError)
|
||||||
func exists(at url: URL) async throws (FileServiceError) -> Bool
|
func exists(at url: URL) async throws (FileServiceError) -> Bool
|
||||||
|
|
||||||
|
@ -22,6 +22,21 @@ public struct FileService: FileServicing {
|
|||||||
|
|
||||||
// MARK: Functions
|
// MARK: Functions
|
||||||
|
|
||||||
|
public func createFolder(at url: URL) async throws (FileServiceError) {
|
||||||
|
guard try await !exists(at: url) else {
|
||||||
|
throw FileServiceError.urlAlreadyExists
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
try fileManager.createDirectory(
|
||||||
|
at: url,
|
||||||
|
withIntermediateDirectories: true
|
||||||
|
)
|
||||||
|
} catch {
|
||||||
|
throw FileServiceError.folderNotCreated
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public func delete(at url: URL) async throws (FileServiceError) {
|
public func delete(at url: URL) async throws (FileServiceError) {
|
||||||
guard try await exists(at: url) else {
|
guard try await exists(at: url) else {
|
||||||
throw FileServiceError.urlNotExists
|
throw FileServiceError.urlNotExists
|
||||||
|
@ -26,6 +26,41 @@ struct FileServiceTests {
|
|||||||
#expect(url == .someExistingFolder)
|
#expect(url == .someExistingFolder)
|
||||||
#expect(url.isFileURL == true)
|
#expect(url.isFileURL == true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
|
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||||
|
func createFolder(with url: URL) async throws {
|
||||||
|
// GIVEN
|
||||||
|
if try await service.exists(at: url) {
|
||||||
|
try await service.delete(at: url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
try await service.createFolder(at: url)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
let result = try await service.exists(at: url)
|
||||||
|
|
||||||
|
#expect(result == true)
|
||||||
|
|
||||||
|
try await service.delete(at: url)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someRandomURL],
|
||||||
|
[FileServiceError.urlAlreadyExists, .urlAlreadyExists, .urlNotFileURL]))
|
||||||
|
func createFolderThrows(
|
||||||
|
with url: URL,
|
||||||
|
expects error: FileServiceError
|
||||||
|
) async throws {
|
||||||
|
// GIVEN
|
||||||
|
// WHEN
|
||||||
|
// THEN
|
||||||
|
await #expect(throws: error) {
|
||||||
|
try await service.createFolder(at: url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||||
func delete(with url: URL) async throws {
|
func delete(with url: URL) async throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
@ -42,7 +77,7 @@ struct FileServiceTests {
|
|||||||
#expect(result == false)
|
#expect(result == false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(arguments: zip([URL.someNonExistingFolder, .someNonExistingFile, .someRandomURL],
|
@Test(arguments: zip([URL.someNewFolder, .someNewFile, .someRandomURL],
|
||||||
[FileServiceError.urlNotExists, .urlNotExists, .urlNotFileURL]))
|
[FileServiceError.urlNotExists, .urlNotExists, .urlNotFileURL]))
|
||||||
func deleteThrows(
|
func deleteThrows(
|
||||||
with url: URL,
|
with url: URL,
|
||||||
@ -56,7 +91,7 @@ struct FileServiceTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNonExistingFolder, .someNonExistingFile],
|
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNewFolder, .someNewFile],
|
||||||
[true, true, false, false]))
|
[true, true, false, false]))
|
||||||
func exists(
|
func exists(
|
||||||
with url: URL,
|
with url: URL,
|
||||||
@ -90,7 +125,5 @@ private extension URL {
|
|||||||
static let someExistingFile = URL(at: "/etc/ssh/ssh_config")
|
static let someExistingFile = URL(at: "/etc/ssh/ssh_config")
|
||||||
static let someNewFolder = URL(at: "/private/tmp/folder")
|
static let someNewFolder = URL(at: "/private/tmp/folder")
|
||||||
static let someNewFile = URL(at: "/private/tmp/file.ext")
|
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")!
|
static let someRandomURL = URL(string: "https://some.random.url")!
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user