restores Helder in all its glory

This commit is contained in:
2026-09-28 11:34:49 +02:00
parent c2b38b853c
commit 1d29d0952d
280 changed files with 29205 additions and 7 deletions
@@ -0,0 +1,38 @@
import Foundation
/// A small case-sensitive APFS disk image, attached for one test and detached on deinit.
final class CaseSensitiveVolume {
let root: URL
private let image: URL
init() throws {
let base = FileManager.default.temporaryDirectory
.appendingPathComponent("helder-cs-\(UUID().uuidString)")
.resolvingSymlinksInPath()
try FileManager.default.createDirectory(at: base, withIntermediateDirectories: true)
image = base.appendingPathComponent("volume.dmg")
root = base.appendingPathComponent("mount")
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
try Self.hdiutil(["create", "-quiet", "-size", "8m", "-fs", "Case-sensitive APFS", "-volname", "HelderCS", image.path])
try Self.hdiutil(["attach", "-quiet", "-nobrowse", "-mountpoint", root.path, image.path])
}
deinit {
try? Self.hdiutil(["detach", "-quiet", "-force", root.path])
try? FileManager.default.removeItem(at: image.deletingLastPathComponent())
}
private static func hdiutil(_ arguments: [String]) throws {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/hdiutil")
process.arguments = arguments
process.standardInput = FileHandle.nullDevice
process.standardOutput = FileHandle.nullDevice
process.standardError = FileHandle.nullDevice
try process.run()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw NSError(domain: "hdiutil", code: Int(process.terminationStatus), userInfo: [NSLocalizedDescriptionKey: arguments.joined(separator: " ")])
}
}
}
@@ -0,0 +1,106 @@
import Foundation
import HelderCore
import HelderServices
/// A config service that always returns the same config.
struct FixedConfig: ConfigServicing {
var config = HelderConfig.defaults
func resolve() async -> HelderConfig { config }
func current() async -> HelderConfig { config }
}
/// A search service without ripgrep, which sends FileService down its walk.
struct NoRipgrepSearch: SearchServicing {
func isAvailable() async -> Bool { false }
func searchContent(_ query: String) async -> [ContentGroup] { [] }
func listFiles(under directory: String?) async -> [String] { [] }
}
/// One process call a `ScriptedRunner` saw.
struct RecordedCall: Sendable, Equatable {
var executable: String
var arguments: [String]
var environment: [String: String]?
}
/// Records every call and answers from a script; for tests of argument construction and error paths.
final class ScriptedRunner: ProcessRunning, @unchecked Sendable {
private let lock = NSLock()
private var recorded: [RecordedCall] = []
private let respond: @Sendable (RecordedCall) async throws -> ProcessResult
init(_ respond: @escaping @Sendable (RecordedCall) async throws -> ProcessResult) {
self.respond = respond
}
var calls: [RecordedCall] {
lock.withLock { recorded }
}
func run(_ executable: String, arguments: [String], in directory: URL?, environment: [String: String]?) async throws -> ProcessResult {
let call = RecordedCall(executable: executable, arguments: arguments, environment: environment)
lock.withLock { recorded.append(call) }
return try await respond(call)
}
}
enum Ripgrep {
/// `$RG_BINARY`, or the ripgrep in `vendor/ripgrep`; nil when neither exists.
static var binary: URL? {
if let env = ProcessInfo.processInfo.environment["RG_BINARY"], FileManager.default.isExecutableFile(atPath: env) {
return URL(fileURLWithPath: env)
}
let packageRoot = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
let bundled = packageRoot.appendingPathComponent("vendor/ripgrep/rg")
return FileManager.default.isExecutableFile(atPath: bundled.path) ? bundled : nil
}
static var isPresent: Bool { binary != nil }
/// A locator that finds only that ripgrep.
static var locator: ExecutableLocator {
ExecutableLocator(directories: binary.map { [$0.deletingLastPathComponent().path] } ?? [])
}
/// A locator that finds no ripgrep, whatever is installed.
static let absent = ExecutableLocator(directories: [])
}
/// Polls until `condition` holds or `timeout` passes; returns whether it held.
func eventually(timeout: TimeInterval = 5, _ condition: () -> Bool) async -> Bool {
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if condition() { return true }
try? await Task.sleep(nanoseconds: 20_000_000)
}
return condition()
}
/// Polls an async condition until it holds or `timeout` passes; returns whether it held.
func eventuallyAsync(timeout: TimeInterval = 5, _ condition: () async -> Bool) async -> Bool {
let deadline = Date().addingTimeInterval(timeout)
while Date() < deadline {
if await condition() { return true }
try? await Task.sleep(nanoseconds: 5_000_000)
}
return await condition()
}
/// Holds every waiter until `open()`; waiters arriving after that pass straight through.
actor Gate {
private var isOpen = false
private var waiters: [CheckedContinuation<Void, Never>] = []
func wait() async {
if isOpen { return }
await withCheckedContinuation { waiters.append($0) }
}
func open() {
isOpen = true
waiters.forEach { $0.resume() }
waiters = []
}
}
@@ -0,0 +1,44 @@
import Foundation
/// A throwaway folder, removed on deinit.
final class TempDirectory {
let root: URL
init() throws {
root = FileManager.default.temporaryDirectory
.appendingPathComponent("helder-test-\(UUID().uuidString)")
.resolvingSymlinksInPath()
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
}
deinit {
try? FileManager.default.removeItem(at: root)
}
func url(_ path: String) -> URL {
root.appendingPathComponent(path)
}
/// Writes a file relative to the root, creating folders as needed.
func write(_ path: String, _ contents: String) throws {
try write(path, Data(contents.utf8))
}
func write(_ path: String, _ data: Data) throws {
let url = url(path)
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
try data.write(to: url)
}
func mkdir(_ path: String) throws {
try FileManager.default.createDirectory(at: url(path), withIntermediateDirectories: true)
}
func read(_ path: String) throws -> String {
try String(contentsOf: url(path), encoding: .utf8)
}
func exists(_ path: String) -> Bool {
FileManager.default.fileExists(atPath: url(path).path)
}
}
@@ -0,0 +1,74 @@
import Foundation
import HelderServices
/// A throwaway git repository in a temp folder, removed on deinit. For integration tests.
final class TempGitRepo {
let root: URL
private let git: URL
/// Creates the folder, runs `git init -b main`, and sets a local user so commits work anywhere.
/// A bare repository serves as a push remote.
init(bare: Bool = false) throws {
guard let git = ExecutableLocator.standard.locate("git") else {
throw ProcessRunnerError.executableNotFound("git")
}
self.git = git
root = FileManager.default.temporaryDirectory
.appendingPathComponent("helder-test-\(UUID().uuidString)")
.resolvingSymlinksInPath()
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
if bare {
try run("init", "-q", "--bare", "-b", "main")
return
}
try run("init", "-q", "-b", "main")
try run("config", "user.name", "Helder Tests")
try run("config", "user.email", "tests@helder.invalid")
try run("config", "commit.gpgsign", "false")
}
deinit {
try? FileManager.default.removeItem(at: root)
}
/// Writes a file relative to the root, creating folders as needed.
func write(_ path: String, _ contents: String) throws {
let url = root.appendingPathComponent(path)
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
try Data(contents.utf8).write(to: url)
}
func read(_ path: String) throws -> String {
try String(contentsOf: root.appendingPathComponent(path), encoding: .utf8)
}
/// Writes the files, stages everything and commits.
func commit(_ files: [String: String] = [:], message: String = "commit") throws {
for (path, contents) in files { try write(path, contents) }
try run("add", "-A")
try run("commit", "-q", "--allow-empty", "-m", message)
}
/// Runs git synchronously in the repo and returns stdout; throws on a non-zero exit.
@discardableResult
func run(_ arguments: String...) throws -> String {
let process = Process()
process.executableURL = git
process.arguments = arguments
process.currentDirectoryURL = root
process.standardInput = FileHandle.nullDevice
let out = Pipe()
let err = Pipe()
process.standardOutput = out
process.standardError = err
try process.run()
let data = out.fileHandleForReading.readDataToEndOfFile()
let errData = err.fileHandleForReading.readDataToEndOfFile()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw GitError.commandFailed(arguments: arguments, exitCode: process.terminationStatus,
stderr: String(decoding: errData, as: UTF8.self))
}
return String(decoding: data, as: UTF8.self)
}
}