Implemented the "exists(at: )" function for the FileService service in the module target.
This commit is contained in:
parent
739fe0c8de
commit
7d0ad3461a
@ -1,9 +1,23 @@
|
||||
import Foundation
|
||||
|
||||
protocol FileServicing {
|
||||
public protocol FileServicing {
|
||||
|
||||
// MARK: Properties
|
||||
// MARK: Computed
|
||||
|
||||
var currentFolder: URL { get async }
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func exists(at url: URL) async throws (FileServiceError) -> Bool
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
public enum FileServiceError: Error, Equatable {
|
||||
case folderNotCreated
|
||||
case folderNotDeleted
|
||||
case urlAlreadyExists
|
||||
case urlNotExists
|
||||
case urlNotFileURL
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import Foundation
|
||||
|
||||
struct FileService: FileServicing {
|
||||
public struct FileService: FileServicing {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@ -8,20 +8,42 @@ struct FileService: FileServicing {
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(fileManager: FileManager = .default) {
|
||||
public init(fileManager: FileManager = .default) {
|
||||
self.fileManager = fileManager
|
||||
}
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
var currentFolder: URL {
|
||||
public var currentFolder: URL {
|
||||
get async {
|
||||
if #available(macOS 13.0, *) {
|
||||
.init(filePath: fileManager.currentDirectoryPath)
|
||||
} else {
|
||||
.init(fileURLWithPath: fileManager.currentDirectoryPath)
|
||||
.init(at: fileManager.currentDirectoryPath)
|
||||
}
|
||||
}
|
||||
|
||||
public func exists(at url: URL) async throws (FileServiceError) -> Bool {
|
||||
guard url.isFileURL else {
|
||||
throw FileServiceError.urlNotFileURL
|
||||
}
|
||||
|
||||
let filePath = getPath(for: url)
|
||||
|
||||
return fileManager.fileExists(atPath: filePath)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension FileService {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func getPath(for url: URL) -> String {
|
||||
if #available(macOS 13.0, *) {
|
||||
return url.path()
|
||||
} else {
|
||||
return url.path
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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")!
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user