Implemented the "exists(at: )" function for the FileService service in the module target.

This commit is contained in:
2025-01-11 03:24:22 +01:00
parent 739fe0c8de
commit 7d0ad3461a
3 changed files with 98 additions and 15 deletions
+52 -5
View File
@@ -1,22 +1,69 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct FileServiceTests {
// MARK: Properties
private let service: FileServicing
// MARK: Initialisers
init() {
self.service = FileService()
}
// MARK: Properties tests
@Test("Test the file service provides a current folder URL")
func currentFolder() async {
@Test func currentFolder() async {
// GIVEN
let service = FileService()
// WHEN
let url = await service.currentFolder
// THEN
#expect(url.path() == "/private/tmp")
#expect(url == .someExistingFolder)
#expect(url.isFileURL == true)
}
// GIVEN
// WHEN
// THEN
}
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNonExistingFolder, .someNonExistingFile],
[true, true, false, false]))
func exists(
with url: URL,
expects outcome: Bool
) async throws {
// GIVEN
// WHEN
let result = try await service.exists(at: url)
// THEN
#expect(result == outcome)
}
@Test func existsThrows() async throws {
// GIVEN
let url = URL.someRandomURL
// WHEN
// THEN
await #expect(throws: FileServiceError.urlNotFileURL) {
try await service.exists(at: url)
}
}
}
// MARK: - URL+Constants
private extension URL {
static let someExistingFolder = URL(at: "/private/tmp")
static let someExistingFile = URL(at: "/etc/null")
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")!
}