SPB Git

Repository Public

Support.swift 7.6 KB 150 lines swift

1import Foundation2import Testing3@testable import SheetPython45/// Interpreters used by the tests, discovered once. Tests are skipped (with the message below) when none exists.6enum Interp {7    static let noPython: Comment = "No Python ≥ 3.9 interpreter was found on this Mac: Python tests skipped."8    static let noPandas: Comment = "No interpreter with pandas + numpy + matplotlib was found: test skipped."9    static let noBare: Comment = "No interpreter WITHOUT pandas was found (/usr/bin/python3): test skipped."1011    private static let task = Task { () -> [PythonInterpreter] in12        Env.prepare()13        return await PythonDiscovery.discover()14    }15    static var all: [PythonInterpreter] { get async { await task.value } }16    /// Any supported interpreter (prefers the scientific one).17    static var any: PythonInterpreter? { get async { let a = await all; return a.first { $0.has("pandas") } ?? a.first } }18    static var scientific: PythonInterpreter? { get async { await all.first { $0.has("pandas") && $0.has("numpy") && $0.has("matplotlib") && !$0.isVirtualEnv } } }19    static var bareOrAny: PythonInterpreter? { get async { if let b = await bare { return b }; return await any } }20    static var bare: PythonInterpreter? { get async { await all.first { !$0.has("pandas") && !$0.has("numpy") && !$0.isVirtualEnv } } }21}2223enum Env {24    /// Redirects the audit log, the trust store, the font cache and the managed venv to a throw-away directory.25    static let support: URL = {26        let url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-sheet-tests-\(UUID().uuidString)", isDirectory: true)27        try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)28        PythonPaths.supportDirectory = url29        atexit { try? FileManager.default.removeItem(at: PythonPaths.supportDirectory) }30        return url31    }()32    static func prepare() { _ = support }33}3435/// In-memory workbook answering the script's requests.36final class FakeHost: PythonHost, @unchecked Sendable {37    struct SheetData { var name: String; var rows: [[PythonCell]]; var dateColumns: [Int] = [] }38    let lock = NSLock()39    var sheets: [SheetData]40    var active: String41    var selection = "A1:B2"42    var failReads = false43    var readDelay: TimeInterval = 044    var readCount = 04546    init(_ sheets: [SheetData] = [SheetData(name: "Data", rows: [])], active: String? = nil) { self.sheets = sheets; self.active = active ?? sheets[0].name }4748    static func cells(_ rows: [[Any?]]) -> [[PythonCell]] {49        rows.map { $0.map { v in50            switch v {51            case nil: return .null52            case let b as Bool: return .bool(b)53            case let i as Int: return .number(Double(i))54            case let d as Double: return .number(d)55            case let s as String: return s.hasPrefix("#") ? .error(s) : .string(s)56            default: return .null57            }58        } }59    }6061    func readRange(sheet: String?, a1: String) async throws -> PythonRangeData {62        if readDelay > 0 { try? await Task.sleep(nanoseconds: UInt64(readDelay * 1e9)) }63        return try lock.withLock {64            readCount += 165            if failReads { throw PythonHostError("the host refused this read") }66            guard let s = sheets.first(where: { $0.name == (sheet ?? active) }) else { throw PythonHostError("no sheet named \(sheet ?? active)") }67            let parts = a1.split(separator: ":").map(String.init)68            guard let a = Wire.parseA1(parts[0]), let b = Wire.parseA1(parts.count > 1 ? parts[1] : parts[0]) else { throw PythonHostError("invalid range \(a1)") }69            var rows: [[PythonCell]] = []70            for r in a.row...b.row {71                var row: [PythonCell] = []72                for c in a.col...b.col { row.append(r < s.rows.count && c < s.rows[r].count ? s.rows[r][c] : .null) }73                rows.append(row)74            }75            return PythonRangeData(sheet: s.name, a1: a1, rows: rows, dateColumns: s.dateColumns.filter { $0 >= a.col && $0 <= b.col }.map { $0 - a.col })76        }77    }78    func activeSheetName() async -> String { lock.withLock { active } }79    func sheetNames() async -> [String] { lock.withLock { sheets.map(\.name) } }80    func selectionA1() async -> String { lock.withLock { selection } }81    func usedRange(sheet: String?) async throws -> String {82        try lock.withLock {83            guard let s = sheets.first(where: { $0.name == (sheet ?? active) }) else { throw PythonHostError("no sheet named \(sheet ?? active)") }84            let h = max(1, s.rows.count), w = max(1, s.rows.map(\.count).max() ?? 1)85            var name = "", n = w86            while n > 0 { let (q, r) = (n - 1).quotientAndRemainder(dividingBy: 26); name = String(UnicodeScalar(UInt8(65 + r))) + name; n = q }87            return "A1:\(name)\(h)"88        }89    }90}9192final class OutputBox: @unchecked Sendable {93    private let lock = NSLock()94    private var chunks: [(PythonStream, String)] = []95    func add(_ s: PythonStream, _ t: String) { lock.withLock { chunks.append((s, t)) } }96    var stdout: String { lock.withLock { chunks.filter { $0.0 == .stdout }.map(\.1).joined() } }97    var stderr: String { lock.withLock { chunks.filter { $0.0 == .stderr }.map(\.1).joined() } }98    var count: Int { lock.withLock { chunks.count } }99}100101/// Runs a script and checks the invariant "no run directory is left behind" (figures are consumed by `body`).102@discardableResult103func runPython(_ code: String, on interpreter: PythonInterpreter, host: FakeHost = FakeHost(), restricted: Bool = true,104               configure: (inout PythonRunOptions) -> Void = { _ in }, output: OutputBox? = nil,105               inspect: (PythonRunResult) throws -> Void = { _ in }) async throws -> PythonRunResult {106    Env.prepare()107    var options = PythonRunOptions()108    options.restricted = restricted109    options.timeout = 60110    configure(&options)111    let runner = PythonRunner(interpreter: interpreter)112    var sink: (@Sendable (PythonStream, String) -> Void)?113    if let output { sink = { stream, text in output.add(stream, text) } }114    let result = try await runner.run(code: code, host: host, options: options, onOutput: sink)115    if result.figures.isEmpty {116        #expect(result.runDirectory == nil)117    } else {118        let dir = try #require(result.runDirectory)119        let left = try FileManager.default.contentsOfDirectory(atPath: dir.path)120        #expect(left == ["figures"], "only figures may survive a run")121    }122    try inspect(result)123    result.discard()124    if let dir = result.runDirectory { #expect(!FileManager.default.fileExists(atPath: dir.path)) }125    return result126}127128func json(_ text: String) -> Any? { try? JSONSerialization.jsonObject(with: Data(text.utf8), options: [.fragmentsAllowed]) }129130/// Last line of stdout parsed as JSON (scripts report their observations this way).131func report(_ result: PythonRunResult) -> [String: Any] {132    guard let line = result.stdout.split(separator: "\n").last, let obj = json(String(line)) as? [String: Any] else { return [:] }133    return obj134}135136func cellsText(_ rows: [[PythonCell]]) -> String {137    rows.map { $0.map { c -> String in138        switch c {139        case .null: return "_"140        case .number(let n): return n == n.rounded() && abs(n) < 1e15 ? String(Int(n)) : String(n)141        case .string(let s): return s142        case .bool(let b): return b ? "T" : "F"143        case .error(let e): return e144        }145    }.joined(separator: ",") }.joined(separator: ";")146}147148/// The audit entry written for a run (matched by code hash: suites run in parallel).149func auditEntry(_ r: PythonRunResult) -> PythonAuditEntry? { PythonAuditLog.recent(limit: 5_000).first { $0.codeHash == r.codeHash } }150
Files main
Menu

Theme