This commit is contained in:
2023-03-12 23:02:11 -04:00
parent 7d69982605
commit b0559d9726
12 changed files with 472 additions and 156 deletions

View File

@@ -1,19 +1,26 @@
import Dependencies
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
import FoundationNetworking
#endif
import XCTestDynamicOverlay
/// Represents the interactions with the file system. It is able
/// to read from and write to files.
///
/// ```swift
/// @Dependency(\.fileClient) var fileClient
/// ```
///
public struct FileClient {
/// Read the file contents from the given `URL` as `Data`.
///
public private(set) var read: (URL) throws -> Data
/// Write `Data` to a file `URL`.
public private(set) var write: (Data, URL) throws -> Void
/// Create a new ``GitVersion/FileClient`` instance.
///
/// This is generally not interacted with directly, instead access as a dependency.
@@ -32,7 +39,7 @@ public struct FileClient {
self.read = read
self.write = write
}
/// Read a file at the given path.
///
/// - Parameters:
@@ -41,7 +48,7 @@ public struct FileClient {
let url = try url(for: path)
return try self.read(url)
}
/// Read the file as a string.
///
/// - Parameters:
@@ -50,7 +57,7 @@ public struct FileClient {
let data = try read(url)
return String(decoding: data, as: UTF8.self)
}
/// Read the file as a string
///
/// - Parameters:
@@ -58,10 +65,10 @@ public struct FileClient {
public func readAsString(path: String) throws -> String {
try self.readAsString(url: url(for: path))
}
/// Read the contents of a file and decode as the decodable type.
///
/// - Parameters:
/// - Parameters:
/// - decodable: The type to decode.
/// - url: The file url.
/// - decoder: The decoder to use.
@@ -73,7 +80,7 @@ public struct FileClient {
let data = try read(url)
return try decoder.decode(D.self, from: data)
}
/// Read the contents of a file and decode as the decodable type.
///
/// - Parameters:
@@ -88,7 +95,7 @@ public struct FileClient {
let data = try read(path: path)
return try decoder.decode(D.self, from: data)
}
/// Write the data to a file at the given path.
///
/// - Parameters:
@@ -98,32 +105,41 @@ public struct FileClient {
let url = try url(for: path)
try self.write(data, url)
}
public func write(string: String, to url: URL) throws {
try self.write(Data(string.utf8), url)
}
public func write(string: String, to path: String) throws {
let url = try url(for: path)
try self.write(string: string, to: url)
}
}
extension FileClient: DependencyKey {
/// A ``FileClient`` that does not do anything.
public static let noop = FileClient.init(
read: { _ in Data() },
write: { _, _ in }
)
/// An `unimplemented` ``FileClient``.
public static let testValue = FileClient(
read: unimplemented("\(Self.self).read", placeholder: Data()),
write: unimplemented("\(Self.self).write")
)
/// The live ``FileClient``
public static let liveValue = FileClient(
read: { try Data(contentsOf: $0) },
write: { try $0.write(to: $1, options: .atomic) }
)
}
extension DependencyValues {
/// Access a basic ``FileClient`` that can read / write data to the file system.
///
public var fileClient: FileClient {
@@ -134,7 +150,7 @@ extension DependencyValues {
// MARK: - Overrides
extension FileClient {
/// Override the data that get's returned when a `read` operation is called.
///
/// This is useful in a testing context.