feat: Adds capturing file client, for tests. Updates bump version tests
This commit is contained in:
@@ -106,6 +106,16 @@ public extension CliClient.SharedOptions {
|
||||
try await operation()
|
||||
}
|
||||
}
|
||||
|
||||
func write(_ string: String, to url: URL) async throws {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
if !dryRun {
|
||||
try await fileClient.write(string: string, to: url)
|
||||
} else {
|
||||
logger.debug("Skipping, due to dry-run being passed.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension CliClient.SharedOptions {
|
||||
@@ -131,12 +141,69 @@ private extension CliClient.SharedOptions {
|
||||
|
||||
let fileContents = Template.build(currentVersion)
|
||||
|
||||
try await fileClient.write(string: fileContents, to: fileUrl)
|
||||
try await write(fileContents, to: fileUrl)
|
||||
|
||||
return fileUrl.cleanFilePath
|
||||
}
|
||||
}
|
||||
|
||||
private func getVersionString() async throws -> (String, Bool) {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.gitVersionClient) var gitVersionClient
|
||||
@Dependency(\.logger) var logger
|
||||
let targetUrl = fileUrl
|
||||
|
||||
guard fileClient.fileExists(targetUrl) else {
|
||||
// Get the latest tag, not requiring an exact tag set on the commit.
|
||||
// This will return a tag, that may have some more data on the patch
|
||||
// portion of the tag, such as: 0.1.1-4-g59bc977
|
||||
let version = try await gitVersionClient.currentVersion(in: gitDirectory, exactMatch: false)
|
||||
// TODO: Not sure what to do for the uses optional value here??
|
||||
return (version, false)
|
||||
}
|
||||
|
||||
let contents = try await fileClient.read(fileUrl)
|
||||
let versionLine = contents.split(separator: "\n")
|
||||
.first { $0.hasPrefix("let VERSION:") }
|
||||
|
||||
guard let versionLine else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
logger.debug("Version line: \(versionLine)")
|
||||
|
||||
let isOptional = versionLine.contains("String?")
|
||||
logger.debug("Uses optional: \(isOptional)")
|
||||
|
||||
let versionString = versionLine.split(separator: "let VERSION: \(isOptional ? "String?" : "String") = ").last
|
||||
guard let versionString else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
return (String(versionString), isOptional)
|
||||
}
|
||||
|
||||
// swiftlint:disable large_tuple
|
||||
private func getVersionParts(_ version: String, _ bump: CliClient.BumpOption) throws -> (Int, Int, Int) {
|
||||
@Dependency(\.logger) var logger
|
||||
let parts = String(version).split(separator: ".")
|
||||
logger.debug("Version parts: \(parts)")
|
||||
|
||||
// TODO: Better error.
|
||||
guard parts.count == 3 else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
|
||||
var major = Int(String(parts[0].replacingOccurrences(of: "\"", with: ""))) ?? 0
|
||||
var minor = Int(String(parts[1])) ?? 0
|
||||
|
||||
// Handle cases where patch version has extra data / not an exact match tag.
|
||||
// Such as: 0.1.1-4-g59bc977
|
||||
var patch = Int(String(parts[2].split(separator: "-").first ?? "0")) ?? 0
|
||||
bump.bump(major: &major, minor: &minor, patch: &patch)
|
||||
return (major, minor, patch)
|
||||
}
|
||||
|
||||
// swiftlint:enable large_tuple
|
||||
|
||||
func bump(_ type: CliClient.BumpOption) async throws -> String {
|
||||
try await run {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@@ -146,45 +213,13 @@ private extension CliClient.SharedOptions {
|
||||
|
||||
logger.debug("Bump target url: \(targetUrl.cleanFilePath)")
|
||||
|
||||
let contents = try await fileClient.read(fileUrl)
|
||||
let versionLine = contents.split(separator: "\n")
|
||||
.first { $0.hasPrefix("let VERSION:") }
|
||||
|
||||
guard let versionLine else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
|
||||
let isOptional = versionLine.contains("String?")
|
||||
let versionString = versionLine.split(separator: "let VERSION: \(isOptional ? "String?" : "String") = ").last
|
||||
guard let versionString else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
|
||||
let parts = String(versionString).split(separator: ".")
|
||||
logger.debug("Version parts: \(parts)")
|
||||
|
||||
// TODO: Better error.
|
||||
guard parts.count == 3 else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
|
||||
var major = Int(String(parts[0])) ?? 0
|
||||
var minor = Int(String(parts[1])) ?? 0
|
||||
var patch = Int(String(parts[2])) ?? 0
|
||||
|
||||
type.bump(major: &major, minor: &minor, patch: &patch)
|
||||
|
||||
let (versionString, usesOptional) = try await getVersionString()
|
||||
let (major, minor, patch) = try getVersionParts(versionString, type)
|
||||
let version = "\(major).\(minor).\(patch)"
|
||||
logger.debug("Bumped version: \(version)")
|
||||
|
||||
let template = isOptional ? Template.optional(version) : Template.build(version)
|
||||
|
||||
if !dryRun {
|
||||
try await fileClient.write(string: template, to: targetUrl)
|
||||
} else {
|
||||
logger.debug("Skipping, due to dry-run being passed.")
|
||||
}
|
||||
|
||||
let template = usesOptional ? Template.optional(version) : Template.build(version)
|
||||
try await write(template, to: targetUrl)
|
||||
return targetUrl.cleanFilePath
|
||||
}
|
||||
}
|
||||
@@ -203,12 +238,7 @@ private extension CliClient.SharedOptions {
|
||||
}
|
||||
|
||||
let template = Template.optional(version)
|
||||
|
||||
if !dryRun {
|
||||
try await fileClient.write(string: template, to: targetUrl)
|
||||
} else {
|
||||
logger.debug("Skipping, due to dry-run being passed.")
|
||||
}
|
||||
try await write(template, to: targetUrl)
|
||||
return targetUrl.cleanFilePath
|
||||
}
|
||||
}
|
||||
@@ -242,20 +272,20 @@ public extension CliClient.BumpOption {
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public struct Template {
|
||||
public struct Template: Sendable {
|
||||
let type: TemplateType
|
||||
let version: String?
|
||||
|
||||
enum TemplateType: String {
|
||||
enum TemplateType: String, Sendable {
|
||||
case optionalString = "String?"
|
||||
case string = "String"
|
||||
}
|
||||
|
||||
var value: String {
|
||||
let versionString = version != nil ? "\"\(version!)\"" : "nil"
|
||||
return """
|
||||
// Do not set this variable, it is set during the build process.
|
||||
let VERSION: \(type.rawValue) = \(version ?? "nil")
|
||||
|
||||
let VERSION: \(type.rawValue) = \(versionString)
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -268,16 +298,6 @@ public struct Template {
|
||||
}
|
||||
}
|
||||
|
||||
private let optionalTemplate = """
|
||||
// Do not set this variable, it is set during the build process.
|
||||
let VERSION: String? = nil
|
||||
"""
|
||||
|
||||
private let buildTemplate = """
|
||||
// Do not set this variable, it is set during the build process.
|
||||
let VERSION: String = nil
|
||||
"""
|
||||
|
||||
enum CliClientError: Error {
|
||||
case gitDirectoryNotFound
|
||||
case fileExists(path: String)
|
||||
|
||||
@@ -6,7 +6,7 @@ import Foundation
|
||||
#endif
|
||||
import XCTestDynamicOverlay
|
||||
|
||||
// TODO: Need a capturing version on write for tests.
|
||||
// TODO: This can be an internal dependency.
|
||||
|
||||
public extension DependencyValues {
|
||||
|
||||
@@ -84,15 +84,27 @@ extension FileClient: DependencyKey {
|
||||
write: { try $0.write(to: $1, options: .atomic) }
|
||||
)
|
||||
|
||||
@_spi(Internal)
|
||||
public static func capturing(
|
||||
_ captured: CapturingWrite
|
||||
) -> Self {
|
||||
.init(
|
||||
fileExists: { _ in true },
|
||||
read: { _ in "" },
|
||||
write: { await captured.set($0, $1) }
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private actor CapturingWrite {
|
||||
var data: Data?
|
||||
var url: URL?
|
||||
@_spi(Internal)
|
||||
public actor CapturingWrite: Sendable {
|
||||
public private(set) var data: Data?
|
||||
public private(set) var url: URL?
|
||||
|
||||
init() {}
|
||||
public init() {}
|
||||
|
||||
func set(data: Data, url: URL) {
|
||||
func set(_ data: Data, _ url: URL) {
|
||||
self.data = data
|
||||
self.url = url
|
||||
}
|
||||
|
||||
@@ -5,7 +5,17 @@ import Foundation
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import ShellClient
|
||||
import XCTestDynamicOverlay
|
||||
|
||||
// TODO: This can be an internal dependency.
|
||||
public extension DependencyValues {
|
||||
|
||||
/// A ``GitVersionClient`` that can retrieve the current version from a
|
||||
/// git directory.
|
||||
var gitVersionClient: GitVersionClient {
|
||||
get { self[GitVersionClient.self] }
|
||||
set { self[GitVersionClient.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
/// A client that can retrieve the current version from a git directory.
|
||||
/// It will use the current `tag`, or if the current git tree does not
|
||||
@@ -21,15 +31,15 @@ public struct GitVersionClient: Sendable {
|
||||
|
||||
/// The closure to run that returns the current version from a given
|
||||
/// git directory.
|
||||
public var currentVersion: @Sendable (String?) async throws -> String
|
||||
public var currentVersion: @Sendable (String?, Bool) async throws -> String
|
||||
|
||||
/// Get the current version from the `git tag` in the given directory.
|
||||
/// If a directory is not passed in, then we will use the current working directory.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - gitDirectory: The directory to run the command in.
|
||||
public func currentVersion(in gitDirectory: String? = nil) async throws -> String {
|
||||
try await currentVersion(gitDirectory)
|
||||
public func currentVersion(in gitDirectory: String? = nil, exactMatch: Bool = true) async throws -> String {
|
||||
try await currentVersion(gitDirectory, exactMatch)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,36 +50,12 @@ extension GitVersionClient: TestDependencyKey {
|
||||
|
||||
/// The ``GitVersionClient`` used in release builds.
|
||||
public static var liveValue: GitVersionClient {
|
||||
.init(currentVersion: { gitDirectory in
|
||||
try await GitVersion(workingDirectory: gitDirectory).currentVersion()
|
||||
.init(currentVersion: { gitDirectory, exactMatch in
|
||||
try await GitVersion(workingDirectory: gitDirectory).currentVersion(exactMatch)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public extension DependencyValues {
|
||||
|
||||
/// A ``GitVersionClient`` that can retrieve the current version from a
|
||||
/// git directory.
|
||||
var gitVersionClient: GitVersionClient {
|
||||
get { self[GitVersionClient.self] }
|
||||
set { self[GitVersionClient.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
public extension ShellCommand {
|
||||
static func gitCurrentSha(gitDirectory: String? = nil) -> Self {
|
||||
GitVersion(workingDirectory: gitDirectory).command(for: .commit)
|
||||
}
|
||||
|
||||
static func gitCurrentBranch(gitDirectory: String? = nil) -> Self {
|
||||
GitVersion(workingDirectory: gitDirectory).command(for: .branch)
|
||||
}
|
||||
|
||||
static func gitCurrentTag(gitDirectory: String? = nil) -> Self {
|
||||
GitVersion(workingDirectory: gitDirectory).command(for: .describe)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private struct GitVersion {
|
||||
@@ -78,11 +64,11 @@ private struct GitVersion {
|
||||
|
||||
let workingDirectory: String?
|
||||
|
||||
func currentVersion() async throws -> String {
|
||||
func currentVersion(_ exactMatch: Bool) async throws -> String {
|
||||
logger.debug("\("Fetching current version".bold)")
|
||||
do {
|
||||
logger.debug("Checking for tag.")
|
||||
return try await run(command: command(for: .describe))
|
||||
return try await run(command: command(for: .describe(exactMatch: exactMatch)))
|
||||
} catch {
|
||||
logger.debug("\("No tag found, deferring to branch & git sha".red)")
|
||||
let branch = try await run(command: command(for: .branch))
|
||||
@@ -99,9 +85,7 @@ private struct GitVersion {
|
||||
argument.arguments.map(\.rawValue)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private extension GitVersion {
|
||||
func run(command: ShellCommand) async throws -> String {
|
||||
try await shell.background(command, trimmingCharactersIn: .whitespacesAndNewlines)
|
||||
}
|
||||
@@ -109,7 +93,7 @@ private extension GitVersion {
|
||||
enum VersionArgs {
|
||||
case branch
|
||||
case commit
|
||||
case describe
|
||||
case describe(exactMatch: Bool)
|
||||
|
||||
var arguments: [Args] {
|
||||
switch self {
|
||||
@@ -117,8 +101,12 @@ private extension GitVersion {
|
||||
return [.git, .symbolicRef, .quiet, .short, .head]
|
||||
case .commit:
|
||||
return [.git, .revParse, .short, .head]
|
||||
case .describe:
|
||||
return [.git, .describe, .tags, .exactMatch]
|
||||
case let .describe(exactMatch):
|
||||
var args = [Args.git, .describe, .tags]
|
||||
if exactMatch {
|
||||
args.append(.exactMatch)
|
||||
}
|
||||
return args
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,9 +30,10 @@ struct CliClientTests {
|
||||
arguments: TestArguments.bumpCases
|
||||
)
|
||||
func bump(type: CliClient.BumpOption, optional: Bool) async throws {
|
||||
let template = optional ? Template.optional("1.0.0") : Template.build("1.0.0")
|
||||
let template = optional ? Template.optional("1.0.0-4-g59bc977") : Template.build("1.0.0")
|
||||
try await run {
|
||||
$0.fileClient.read = { _ in template }
|
||||
$0.fileClient.fileExists = { _ in true }
|
||||
$0.fileClient.read = { @Sendable _ in template }
|
||||
} operation: {
|
||||
let client = CliClient.liveValue
|
||||
let output = try await client.bump(
|
||||
@@ -45,8 +46,17 @@ struct CliClientTests {
|
||||
verbose: true
|
||||
)
|
||||
)
|
||||
|
||||
#expect(output == "/baz/Sources/bar/foo")
|
||||
} assert: { string, _ in
|
||||
let typeString = optional ? "String?" : "String"
|
||||
switch type {
|
||||
case .major:
|
||||
#expect(string.contains("let VERSION: \(typeString) = \"2.0.0\""))
|
||||
case .minor:
|
||||
#expect(string.contains("let VERSION: \(typeString) = \"1.1.0\""))
|
||||
case .patch:
|
||||
#expect(string.contains("let VERSION: \(typeString) = \"1.0.1\""))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,17 +98,31 @@ struct CliClientTests {
|
||||
|
||||
func run(
|
||||
setupDependencies: @escaping (inout DependencyValues) -> Void = { _ in },
|
||||
operation: @Sendable @escaping () async throws -> Void
|
||||
operation: @Sendable @escaping () async throws -> Void,
|
||||
assert: @escaping (String, URL) -> Void = { _, _ in }
|
||||
) async throws {
|
||||
let captured = CapturingWrite()
|
||||
|
||||
try await withDependencies {
|
||||
$0.logger.logLevel = .debug
|
||||
$0.fileClient = .noop
|
||||
$0.fileClient = .capturing(captured)
|
||||
$0.fileClient.fileExists = { _ in false }
|
||||
$0.gitVersionClient = .init { _ in "1.0.0" }
|
||||
$0.gitVersionClient = .init { _, _ in "1.0.0" }
|
||||
setupDependencies(&$0)
|
||||
} operation: {
|
||||
try await operation()
|
||||
}
|
||||
let data = await captured.data
|
||||
let url = await captured.url
|
||||
|
||||
guard let data,
|
||||
let string = String(bytes: data, encoding: .utf8),
|
||||
let url
|
||||
else {
|
||||
throw TestError()
|
||||
}
|
||||
|
||||
assert(string, url)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,3 +133,5 @@ enum TestArguments {
|
||||
$0.append(($1, false))
|
||||
}
|
||||
}
|
||||
|
||||
struct TestError: Error {}
|
||||
|
||||
Reference in New Issue
Block a user