feat: Adds file-client tests.
This commit is contained in:
@@ -14,11 +14,15 @@ public struct FileClient: Sendable {
|
||||
public var copy: @Sendable (URL, URL) async throws -> Void
|
||||
public var createDirectory: @Sendable (URL) async throws -> Void
|
||||
public var fileExists: @Sendable (URL) -> Bool = { _ in true }
|
||||
public var findVaultFileInCurrentDirectory: @Sendable () async throws -> URL?
|
||||
public var findVaultFile: @Sendable (URL) async throws -> URL?
|
||||
public var homeDirectory: @Sendable () -> URL = { URL(filePath: "~/") }
|
||||
public var isDirectory: @Sendable (URL) async throws -> Bool
|
||||
public var load: @Sendable (URL) async throws -> Data
|
||||
public var write: @Sendable (Data, URL) async throws -> Void
|
||||
|
||||
public func findVaultFileInCurrentDirectory() async throws -> URL? {
|
||||
try await findVaultFile(URL(filePath: "./"))
|
||||
}
|
||||
}
|
||||
|
||||
extension FileClient: DependencyKey {
|
||||
@@ -32,8 +36,8 @@ extension FileClient: DependencyKey {
|
||||
try await manager.creatDirectory($0)
|
||||
} fileExists: { url in
|
||||
manager.fileExists(at: url)
|
||||
} findVaultFileInCurrentDirectory: {
|
||||
try await manager.findVaultFileInCurrentDirectory()
|
||||
} findVaultFile: {
|
||||
try await manager.findVaultFile(in: $0)
|
||||
} homeDirectory: {
|
||||
manager.homeDirectory()
|
||||
} isDirectory: {
|
||||
@@ -62,26 +66,27 @@ struct LiveFileClient: Sendable {
|
||||
manager.fileExists(atPath: url.cleanFilePath)
|
||||
}
|
||||
|
||||
func findVaultFileInCurrentDirectory() async throws -> URL? {
|
||||
let urls = try manager.contentsOfDirectory(
|
||||
at: URL(filePath: "./"),
|
||||
includingPropertiesForKeys: nil
|
||||
)
|
||||
func findVaultFile(in url: URL) async throws -> URL? {
|
||||
guard isDirectory(url) else { return nil }
|
||||
let urls = try manager.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
|
||||
|
||||
// Check the current directory
|
||||
if let vault = urls.firstVaultFile { return vault }
|
||||
guard let vault = urls.firstVaultFile else {
|
||||
// check subfolders, 1 layer deep.
|
||||
let subfolders = urls.filter { isDirectory($0) }
|
||||
for folder in subfolders {
|
||||
let vault = try manager.contentsOfDirectory(
|
||||
at: folder,
|
||||
includingPropertiesForKeys: nil
|
||||
)
|
||||
.firstVaultFile
|
||||
|
||||
let subfolders = urls.filter { isDirectory($0) }
|
||||
|
||||
for folder in subfolders {
|
||||
let files = try manager.contentsOfDirectory(
|
||||
at: folder,
|
||||
includingPropertiesForKeys: nil
|
||||
)
|
||||
if let vault = files.firstVaultFile { return vault }
|
||||
if let vault { return vault }
|
||||
}
|
||||
// Didn't find a file.
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
return vault
|
||||
}
|
||||
|
||||
func homeDirectory() -> URL {
|
||||
@@ -114,6 +119,8 @@ private extension Array where Element == URL {
|
||||
|
||||
public extension URL {
|
||||
var cleanFilePath: String {
|
||||
absoluteString.replacing("file://", with: "")
|
||||
absoluteString
|
||||
.replacing("file://", with: "")
|
||||
.replacing("/private", with: "")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user