Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocAutomationTests/AutomationTestSupport.swift3// Description: Fixtures for the automation tests: sample documents, temporary folders, the DOCX corpus and the built tool.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel9import DocContainer10@testable import DocAutomation1112/// A temporary folder removed when the value goes away.13final class TempFolder {14 let url: URL15 init() {16 url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-automation-\(UUID().uuidString)", isDirectory: true)17 try! FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)18 }19 deinit { try? FileManager.default.removeItem(at: url) }20 func file(_ name: String) -> URL { url.appendingPathComponent(name) }21 func subfolder(_ name: String) -> URL { url.appendingPathComponent(name, isDirectory: true) }22}2324enum Sample {25 static func para(_ text: String, style: String = StyleID.normal) -> Block { .paragraph(Paragraph(text, style: style)) }2627 /// A three-section report with a title, headings and body text.28 static func report(title: String = "Quarterly Report") -> DocumentModel {29 let filler = "The quick brown fox jumps over the lazy dog while the committee reviews the figures of the quarter and prepares its recommendations. "30 return DocumentModel(meta: DocumentMetadata(title: title, author: "Ada Lovelace", language: "en"), pageSetup: PageSetup(paper: .letter), body: [31 para(title, style: StyleID.title),32 para("Introduction", style: StyleID.heading(1)),33 para(String(repeating: filler, count: 3)),34 para("Zebra Findings", style: StyleID.heading(1)),35 para(String(repeating: filler, count: 3)),36 para("Regional detail", style: StyleID.heading(2)),37 para(String(repeating: filler, count: 2)),38 para("Conclusion", style: StyleID.heading(1)),39 para(String(repeating: filler, count: 2))])40 }4142 static let markdown = """43 # Field Notes4445 ## Method4647 We measured the *response time* of every station twice.4849 ## Results5051 The median was 42 ms. See the table.5253 | Station | Median |54 |---|---|55 | North | 40 |56 | South | 44 |57 """5859 /// Writes `doc` in the format of the extension and returns the URL.60 @discardableResult61 static func write(_ doc: DocumentModel, to url: URL) throws -> URL {62 _ = try DocumentConverter.export(doc, to: url)63 return url64 }6566 static var corpus: URL {67 URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("DocDOCXTests/Corpus", isDirectory: true)68 }6970 /// The repository root (three levels above the package's Tests folder).71 static var repositoryRoot: URL {72 URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()73 }74}7576/// Runs the built `orixdocs` executable, which sits next to the test bundle in the build folder.77enum Tool {78 static var url: URL? {79 // The executable is built next to the test bundle.80 var candidates = [Bundle(for: TempFolder.self).bundleURL.deletingLastPathComponent()]81 candidates += Bundle.allBundles.filter { $0.bundlePath.hasSuffix(".xctest") }.map { $0.bundleURL.deletingLastPathComponent() }82 for folder in candidates {83 let candidate = folder.appendingPathComponent("orixdocs")84 if FileManager.default.isExecutableFile(atPath: candidate.path) { return candidate }85 }86 return nil87 }8889 struct Output { var status: Int32; var stdout: String; var stderr: String }9091 static func run(_ arguments: [String], at tool: URL) throws -> Output {92 let process = Process()93 process.executableURL = tool94 process.arguments = arguments95 // The tool's history and saved workflows go to a scratch folder, never to the user's Application Support.96 var environment = ProcessInfo.processInfo.environment97 environment["ORIX_AUTOMATION_HOME"] = FileManager.default.temporaryDirectory.appendingPathComponent("orix-automation-home-tests").path98 process.environment = environment99 let outPipe = Pipe(), errPipe = Pipe()100 process.standardOutput = outPipe101 process.standardError = errPipe102 process.standardInput = FileHandle.nullDevice103 try process.run()104 // Read before waiting: a full pipe would block the tool.105 let out = outPipe.fileHandleForReading.readDataToEndOfFile()106 let err = errPipe.fileHandleForReading.readDataToEndOfFile()107 process.waitUntilExit()108 return Output(status: process.terminationStatus, stdout: String(decoding: out, as: UTF8.self), stderr: String(decoding: err, as: UTF8.self))109 }110}111112/// A thread-safe list for values collected from progress callbacks.113final class Collector<T: Sendable>: @unchecked Sendable {114 private let lock = NSLock()115 private var items: [T] = []116 func add(_ item: T) { lock.lock(); items.append(item); lock.unlock() }117 var values: [T] { lock.lock(); defer { lock.unlock() }; return items }118}119