feat: Adds file-client tests.

This commit is contained in:
2024-12-16 19:00:42 -05:00
parent 85b285347b
commit f89efc8c5e
4 changed files with 100 additions and 22 deletions

View File

@@ -91,6 +91,10 @@ let package = Package(
.product(name: "DependenciesMacros", package: "swift-dependencies") .product(name: "DependenciesMacros", package: "swift-dependencies")
] ]
), ),
.testTarget(
name: "FileClientTests",
dependencies: ["FileClient", "TestSupport"]
),
.target( .target(
name: "PandocClient", name: "PandocClient",
dependencies: [ dependencies: [

View File

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

View File

@@ -0,0 +1,67 @@
import FileClient
import Foundation
import Testing
import TestSupport
@Suite("FileClientTests")
struct FileClientTests {
@Test
func createDirectory() async throws {
try await withTemporaryDirectory { url in
let fileClient = FileClient.liveValue
let tempDir = url.appending(path: "temp")
try await fileClient.createDirectory(tempDir)
let isDirectory = try await fileClient.isDirectory(tempDir)
#expect(isDirectory)
}
}
@Test(arguments: ["vault.yml", "vault.yaml"])
func findVaultFile(fileName: String) async throws {
try await withTemporaryDirectory { url in
let fileClient = FileClient.liveValue
let vaultFilePath = url.appending(path: fileName)
try FileManager.default.createFile(atPath: vaultFilePath.cleanFilePath, contents: nil)
let output = try await fileClient.findVaultFile(url)!
#expect(output.cleanFilePath == vaultFilePath.cleanFilePath)
let nilWhenFileNotDirectory = try await fileClient.findVaultFile(vaultFilePath)
#expect(nilWhenFileNotDirectory == nil)
}
}
@Test(arguments: ["vault.yml", "vault.yaml"])
func findVaultFileNestedInSubfolder(fileName: String) async throws {
try await withTemporaryDirectory { url in
let fileClient = FileClient.liveValue
let subDir = url.appending(path: "sub")
try await fileClient.createDirectory(subDir)
let vaultFilePath = subDir.appending(path: fileName)
try FileManager.default.createFile(atPath: vaultFilePath.cleanFilePath, contents: nil)
let output = try await fileClient.findVaultFile(url)!
#expect(output.cleanFilePath == vaultFilePath.cleanFilePath)
}
}
@Test
func findVaultFileReturnsNil() async throws {
try await withTemporaryDirectory { url in
let fileClient = FileClient.liveValue
let subDir = url.appending(path: "sub")
try await fileClient.createDirectory(subDir)
let output = try await fileClient.findVaultFile(url)
#expect(output == nil)
}
}
}

View File

@@ -14,7 +14,7 @@ struct VaultClientTests: TestCase {
func decrypt(input: TestOptions) async throws { func decrypt(input: TestOptions) async throws {
try await withCapturingCommandClient("decrypt") { try await withCapturingCommandClient("decrypt") {
$0.configurationClient = .mock(input.configuration) $0.configurationClient = .mock(input.configuration)
$0.fileClient.findVaultFileInCurrentDirectory = { URL(filePath: "/vault.yml") } $0.fileClient.findVaultFile = { _ in URL(filePath: "/vault.yml") }
$0.vaultClient = .liveValue $0.vaultClient = .liveValue
} run: { } run: {
@Dependency(\.vaultClient) var vaultClient @Dependency(\.vaultClient) var vaultClient
@@ -45,7 +45,7 @@ struct VaultClientTests: TestCase {
func encrypt(input: TestOptions) async throws { func encrypt(input: TestOptions) async throws {
try await withCapturingCommandClient("decrypt") { try await withCapturingCommandClient("decrypt") {
$0.configurationClient = .mock(input.configuration) $0.configurationClient = .mock(input.configuration)
$0.fileClient.findVaultFileInCurrentDirectory = { URL(filePath: "/vault.yml") } $0.fileClient.findVaultFile = { _ in URL(filePath: "/vault.yml") }
$0.vaultClient = .liveValue $0.vaultClient = .liveValue
} run: { } run: {
@Dependency(\.vaultClient) var vaultClient @Dependency(\.vaultClient) var vaultClient