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
|
import Foundation
|
||||||
|
|
||||||
protocol FileServicing {
|
public protocol FileServicing {
|
||||||
|
|
||||||
// MARK: Properties
|
// MARK: Computed
|
||||||
|
|
||||||
var currentFolder: URL { get async }
|
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
|
import Foundation
|
||||||
|
|
||||||
struct FileService: FileServicing {
|
public struct FileService: FileServicing {
|
||||||
|
|
||||||
// MARK: Properties
|
// MARK: Properties
|
||||||
|
|
||||||
@ -8,19 +8,41 @@ struct FileService: FileServicing {
|
|||||||
|
|
||||||
// MARK: Initialisers
|
// MARK: Initialisers
|
||||||
|
|
||||||
init(fileManager: FileManager = .default) {
|
public init(fileManager: FileManager = .default) {
|
||||||
self.fileManager = fileManager
|
self.fileManager = fileManager
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Computed
|
// MARK: Computed
|
||||||
|
|
||||||
var currentFolder: URL {
|
public var currentFolder: URL {
|
||||||
get async {
|
get async {
|
||||||
if #available(macOS 13.0, *) {
|
.init(at: fileManager.currentDirectoryPath)
|
||||||
.init(filePath: fileManager.currentDirectoryPath)
|
}
|
||||||
} else {
|
}
|
||||||
.init(fileURLWithPath: 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
|
import Testing
|
||||||
|
|
||||||
@testable import ColibriLibrary
|
@testable import ColibriLibrary
|
||||||
|
|
||||||
struct FileServiceTests {
|
struct FileServiceTests {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
private let service: FileServicing
|
||||||
|
|
||||||
|
// MARK: Initialisers
|
||||||
|
|
||||||
|
init() {
|
||||||
|
self.service = FileService()
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: Properties tests
|
// MARK: Properties tests
|
||||||
|
|
||||||
@Test("Test the file service provides a current folder URL")
|
@Test func currentFolder() async {
|
||||||
func currentFolder() async {
|
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let service = FileService()
|
|
||||||
|
|
||||||
// WHEN
|
// WHEN
|
||||||
let url = await service.currentFolder
|
let url = await service.currentFolder
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
#expect(url.path() == "/private/tmp")
|
#expect(url == .someExistingFolder)
|
||||||
#expect(url.isFileURL == true)
|
#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