SPB Git

Repository Public

DiscoveryAndRecordsTests.swift 16.7 KB 246 lines swift

1import Foundation2import Testing3import SheetCore4@testable import SheetPython56@Suite struct DiscoveryTests {7    @Test(.enabled(Interp.noPython) { await Interp.any != nil })8    func discoversInterpretersWithVersionsAndPackages() async throws {9        let all = await Interp.all10        print("DISCOVERED INTERPRETERS:")11        for i in all { print("  \(i.path) → \(i.executable)  \(i.version)  \(i.source.rawValue)  venv=\(i.isVirtualEnv)  packages=\(i.packages.keys.sorted().joined(separator: ","))") }12        #expect(!all.isEmpty)13        for i in all {14            #expect(i.isSupported && i.versionTriple.0 == 3 && i.versionTriple.1 >= 9)15            #expect(FileManager.default.isExecutableFile(atPath: i.executable) && !i.prefix.isEmpty)16        }17        #expect(Set(all.map { $0.executable + "|" + $0.prefix }).count == all.count, "no duplicates")18        if let brew = all.first(where: { $0.path == "/opt/homebrew/bin/python3" }) {19            #expect(brew.source == .homebrew && !brew.isVirtualEnv)20            #expect(brew.sitePackages.contains { $0.contains("site-packages") })21        }22        if let sys = all.first(where: { $0.path == "/usr/bin/python3" }) {23            #expect(sys.source == .system && sys.executable != "/usr/bin/python3", "the xcrun shim is resolved to the real interpreter")24        }25    }2627    @Test func probeRejectsWhatIsNotPython() async {28        #expect(await PythonDiscovery.probe(path: "/nonexistent/python3") == nil)29        #expect(await PythonDiscovery.probe(path: "/bin/ls") == nil)30        #expect(await PythonDiscovery.probe(path: "/etc/hosts") == nil)31        #expect(await PythonDiscovery.probe(path: "/bin/sleep") == nil, "a hanging executable is abandoned by the probe timeout… or exits on bad arguments")32    }3334    @Test(.enabled(Interp.noPython) { await Interp.any != nil })35    func userChosenPathIsProbed() async throws {36        let py = try #require(await Interp.any)37        let found = await PythonDiscovery.discover(extraPaths: [py.executable, "/bin/ls"])38        #expect(found.contains { $0.executable == py.executable })39        #expect(!found.contains { $0.path == "/bin/ls" })40    }4142    @Test func versionParsing() {43        #expect(PythonInterpreter(path: "/x", version: "3.14.4").versionTriple == (3, 14, 4))44        #expect(PythonInterpreter(path: "/x", version: "3.13.0rc1").versionTriple == (3, 13, 0))45        #expect(!PythonInterpreter(path: "/x", version: "3.8.10").isSupported && !PythonInterpreter(path: "/x", version: "2.7.18").isSupported)46        #expect(PythonInterpreter(path: "/x", version: "3.9.6").isSupported)47    }4849    @Test(.enabled(Interp.noPython) { await Interp.any != nil })50    func restrictedModeWorksWithEveryDiscoveredInterpreter() async throws {51        for py in await Interp.all {52            let r = try await runPython("import sys, json, os\nprint(json.dumps(list(sys.version_info[:2])))", on: py)53            #expect(r.succeeded, "\(py.path): \(r.stderr)")54            #expect(json(r.stdout.trimmingCharacters(in: .newlines)) as? [Int] == [py.versionTriple.0, py.versionTriple.1])55        }56    }5758    @Test(.enabled(Interp.noPython) { await Interp.any != nil })59    func packageListingHasProvenance() async throws {60        let py = try #require(await Interp.any)61        let list = try await PythonEnvironmentManager(interpreter: py).listPackages()62        #expect(!list.isEmpty)63        #expect(list.allSatisfy { !$0.name.isEmpty && $0.location.hasPrefix("/") })64        #expect(list.map { $0.name.lowercased() } == list.map { $0.name.lowercased() }.sorted())65        if py.source == .homebrew, let numpy = list.first(where: { $0.name == "numpy" }) {66            #expect(numpy.provenance == .homebrew && numpy.version == py.packages["numpy"] && numpy.location.contains("site-packages"))67        }68        print("PACKAGES: \(list.count) distributions; provenance counts: \(Dictionary(grouping: list, by: \.provenance).mapValues(\.count))")69    }7071    @Test func requirementValidation() {72        for ok in ["pandas", "scikit-learn", "pandas==2.2.3", "numpy>=1.26,<3", "requests[socks]", "ruamel.yaml", "a"] { #expect(PythonEnvironmentManager.isValidRequirement(ok), "\(ok)") }73        for bad in ["", "-r requirements.txt", "--index-url=http://evil", "pandas; rm -rf ~", "git+https://x/y.git", "./local", "/abs/path", "a b", "pkg @ https://x", "$(id)", "pandas\n-e ."] {74            #expect(!PythonEnvironmentManager.isValidRequirement(bad), "\(bad)")75        }76        #expect(PythonEnvironmentManager.installCommand(package: "pandas").suffix(2) == ["--", "pandas"])77    }7879    @Test(.enabled(Interp.noPython) { await Interp.any != nil })80    func managedVenvIsCreatedOffline() async throws {81        Env.prepare()82        let base = try #require(await Interp.any)83        await #expect(throws: PythonEnvironmentError.noManagedEnvironment) { try await PythonEnvironmentManager.install(package: "pandas") }84        await #expect(throws: PythonEnvironmentError.invalidPackageName("--upgrade pip")) { try await PythonEnvironmentManager.install(package: "--upgrade pip") }85        let venv = try await PythonEnvironmentManager(interpreter: base).createVenv()86        #expect(PythonEnvironmentManager.managedVenvExists)87        #expect(venv.isVirtualEnv && venv.source == .venv && venv.version == base.version)88        #expect(venv.prefix.hasPrefix(PythonSandbox.canonical(Env.support.path)), "created under the (redirected) support directory, not in the real one")89        #expect(venv.packages["numpy"] == base.packages["numpy"], "--system-site-packages keeps the base packages visible")90        #expect(FileManager.default.fileExists(atPath: PythonPaths.managedVenv.appendingPathComponent("pyvenv.cfg").path))91        // the venv runs scripts in restricted mode (its prefix is under the support directory, which the profile re-allows)92        let r = try await runPython("import sys, orix\nprint(sys.prefix != sys.base_prefix)\n" + (base.has("numpy") ? "import numpy\n" : ""), on: venv)93        #expect(r.succeeded && r.stdout == "True\n", "\(r.stderr)")94        let pkgs = try await PythonEnvironmentManager(interpreter: venv).listPackages()95        #expect(pkgs.contains { $0.name == "pip" && $0.provenance == .managedVenv })96        #expect(await PythonDiscovery.discover().contains { $0.isVirtualEnv && $0.path == PythonEnvironmentManager.managedPython.path })97        try PythonEnvironmentManager.removeVenv()98        #expect(!PythonEnvironmentManager.managedVenvExists)99    }100}101102@Suite(.serialized) struct RecordsTests {103    @Test func trustIsKeyedByExactCodeWorkbookAndMode() {104        Env.prepare()105        let wb = UUID().uuidString106        let code = "print('hello')"107        let h = PythonHash.sha256(code)108        #expect(h == "ed5a1d8a4a1b6ee3a7e1d09d1d1a1ecc5c9e8e0fb4c7ee9a0c5dc0ab3b6d0b3e" || h.count == 64)109        #expect(PythonHash.sha256("abc") == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")110        #expect(!PythonTrust.isTrusted(codeHash: h, workbookID: wb))111        PythonTrust.trust(codeHash: h, workbookID: wb, restricted: true, scriptName: "s")112        #expect(PythonTrust.isTrusted(codeHash: h, workbookID: wb))113        #expect(!PythonTrust.isTrusted(codeHash: h, workbookID: wb, restricted: false), "consent given for restricted mode does not cover an unrestricted run")114        #expect(!PythonTrust.isTrusted(codeHash: PythonHash.sha256(code + " "), workbookID: wb), "one extra space = another script")115        #expect(!PythonTrust.isTrusted(codeHash: h, workbookID: UUID().uuidString), "another workbook asks again")116        PythonTrust.trust(codeHash: h, workbookID: wb, restricted: false)117        #expect(PythonTrust.isTrusted(codeHash: h, workbookID: wb, restricted: false) && PythonTrust.isTrusted(codeHash: h, workbookID: wb, restricted: true))118        #expect(PythonTrust.records().filter { $0.workbookID == wb }.count == 1)119        let perms = (try? FileManager.default.attributesOfItem(atPath: PythonPaths.trustStore.path))?[.posixPermissions] as? Int120        #expect(perms == 0o600)121        PythonTrust.revoke(codeHash: h, workbookID: wb)122        #expect(!PythonTrust.isTrusted(codeHash: h, workbookID: wb))123        PythonTrust.trust(codeHash: h, workbookID: wb)124        PythonTrust.revokeAll()125        #expect(!PythonTrust.isTrusted(codeHash: h, workbookID: wb) && PythonTrust.records().isEmpty)126    }127128    @Test(.enabled(Interp.noPython) { await Interp.any != nil })129    func everyRunIsAudited() async throws {130        let py = try #require(await Interp.any)131        let code = "import orix\norix.active_sheet().range('A1').write(1)\nprint('audited \(UUID().uuidString)')"132        let ok = try await runPython(code, on: py, configure: { $0.workbookName = "Budget 2026.orixsheet"; $0.scriptName = "résumé.py" })133        let e = try #require(auditEntry(ok))134        #expect(e.workbook == "Budget 2026.orixsheet" && e.script == "résumé.py" && e.codeHash == PythonHash.sha256(code))135        #expect(e.interpreter == py.path && e.interpreterVersion == py.version && e.restricted && e.outcome == .ok && e.exitStatus == 0 && e.writes == 1 && !e.networkAllowed)136        #expect(e.duration > 0 && abs(e.timestamp.timeIntervalSinceNow) < 60)137        let bad = try await runPython("raise RuntimeError('boom \(UUID().uuidString)')", on: py, restricted: false)138        let b = try #require(auditEntry(bad))139        #expect(b.outcome == .error && b.exitStatus == 1 && !b.restricted && b.networkAllowed && b.detail.hasPrefix("RuntimeError: boom"))140        // the file is JSON Lines, one object per line, private to the user141        let text = try String(contentsOf: PythonPaths.auditLog, encoding: .utf8)142        for line in text.split(separator: "\n") { #expect(json(String(line)) is [String: Any]) }143        #expect(((try FileManager.default.attributesOfItem(atPath: PythonPaths.auditLog.path))[.posixPermissions] as? Int) == 0o600)144        #expect(PythonAuditLog.recent(limit: 1).count == 1)145        let recent = PythonAuditLog.recent(limit: 50)146        #expect(recent == recent.sorted { $0.timestamp > $1.timestamp }, "most recent first")147    }148149    @Test func scriptsLiveInWorkbookAttachments() throws {150        let wb = Workbook(sheetNames: ["S"])151        #expect(PythonScripts.load(from: wb).isEmpty)152        var a = PythonScript(name: "Summarise", code: "import orix\nprint('é')")153        let b = PythonScript(name: "analysis", code: "x = 1")154        PythonScripts.save(a, to: wb); PythonScripts.save(b, to: wb)155        wb.attachments["python/garbage.json"] = Data("not json".utf8)156        wb.attachments["workflow/x.json"] = Data("{}".utf8)157        #expect(wb.attachments[PythonScripts.key(a.id)] != nil)158        #expect(PythonScripts.load(from: wb).map(\.name) == ["analysis", "Summarise"])159        a.code = "print(2)"; a.modified = Date(timeIntervalSince1970: 1_800_000_000)160        PythonScripts.save(a, to: wb)161        let loaded = try #require(PythonScripts.load(from: wb).first { $0.id == a.id })162        #expect(loaded == a && loaded.codeHash == PythonHash.sha256("print(2)"))163        PythonScripts.remove(b.id, from: wb)164        #expect(PythonScripts.load(from: wb).count == 1 && wb.attachments["workflow/x.json"] != nil)165        let id = PythonScripts.workbookID(wb)166        #expect(UUID(uuidString: id) != nil && PythonScripts.workbookID(wb) == id)167        #expect(PythonScripts.load(from: wb).count == 1, "the workbook id is not mistaken for a script")168        let fresh = Workbook(sheetNames: ["S"])169        _ = PythonScripts.workbookID(fresh, create: false)170        #expect(fresh.attachments.isEmpty)171    }172173    @Test func wireFormat() throws {174        var out = [UInt8]()175        for c in [PythonCell.null, .number(1), .number(-2.5), .number(1e300), .number(.nan), .string("a\"b\\c\n\u{01}é"), .bool(true), .error("#N/A")] { Wire.appendCell(c, to: &out); out.append(0x2C) }176        #expect(String(decoding: out, as: UTF8.self) == ##"null,1.0,-2.5,1e+300,null,"a\"b\\c\n\u0001é",true,{"error":"#N/A"},"##)177        let line = Wire.rangeAnswer(id: 7, PythonRangeData(sheet: "S \"1\"", a1: "A1:B1", rows: [[.number(3), .string("x")]], dateColumns: [0], date1904: false))178        let obj = try #require(json(String(decoding: line, as: UTF8.self)) as? [String: Any])179        #expect(obj["id"] as? Int == 7 && obj["ok"] as? Bool == true && line.last == 0x0A)180        let result = try #require(obj["result"] as? [String: Any])181        #expect(result["sheet"] as? String == "S \"1\"" && result["date_columns"] as? [Int] == [0] && (result["rows"] as? [[Any]])?.count == 1)182        #expect(Wire.parseA1("A1")! == (0, 0) && Wire.parseA1("$XFD$1048576")! == (1_048_575, 16_383) && Wire.parseA1("AA10")! == (9, 26))183        for bad in ["", "A", "1", "A0", "A1:B2", "A-1", "ABCD1", "A12345678", "é1"] { #expect(Wire.parseA1(bad) == nil, "\(bad)") }184        #expect(throws: PythonHostError.self) { try Wire.rows([[1, 2], [3]]) }185        #expect(throws: PythonHostError.self) { try Wire.rows([[["nested"]]]) }186        #expect(try Wire.rows([[1, "a", true, NSNull(), ["error": "#REF!"]]]) == [[.number(1), .string("a"), .bool(true), .null, .error("#REF!")]])187        let cells: [PythonCell] = [.null, .number(1.5), .string("s"), .bool(false), .error("#NUM!")]188        #expect(try JSONDecoder().decode([PythonCell].self, from: JSONEncoder().encode(cells)) == cells)189        var chunker = UTF8Chunker()190        let bytes = Array("é🚀".utf8)191        #expect(chunker.feed(Array(bytes[0..<1])) == "" && chunker.feed(Array(bytes[1..<4])) == "é" && chunker.feed(Array(bytes[4...])) == "🚀" && chunker.flush() == "")192    }193194    @Test func staleRunDirectoriesAreSwept() throws {195        let fm = FileManager.default196        let old = fm.temporaryDirectory.appendingPathComponent(PythonPaths.runDirectoryPrefix + "stale-\(UUID().uuidString)")197        try fm.createDirectory(at: old.appendingPathComponent("figures"), withIntermediateDirectories: true)198        try fm.setAttributes([.modificationDate: Date(timeIntervalSinceNow: -3 * 24 * 3600)], ofItemAtPath: old.path)199        #expect(PythonRunner.existingRunDirectories().contains { $0.lastPathComponent == old.lastPathComponent })200        #expect(PythonRunner.removeStaleRunDirectories(olderThan: 24 * 3600) >= 1)201        #expect(!fm.fileExists(atPath: old.path))202    }203}204205@Suite(.serialized) struct LargeDataTests {206    @Test(.enabled(Interp.noPandas) { await Interp.scientific != nil })207    func hundredThousandRowsRoundTrip() async throws {208        let py = try #require(await Interp.scientific)209        let n = 100_000210        var rows: [[PythonCell]] = [[.string("id"), .string("category"), .string("revenue"), .string("units"), .string("date"), .string("note")]]211        rows.reserveCapacity(n + 1)212        for i in 0..<n {213            rows.append([.number(Double(i)), .string("cat\(i % 23)"), .number(Double(i) * 1.37 + 0.5), i % 97 == 0 ? .null : .number(Double(i % 50)),214                         .number(45292 + Double(i % 700)), .string("note \(i) — é")])215        }216        let host = FakeHost([.init(name: "Big", rows: rows, dateColumns: [4])])217        let t0 = Date()218        let r = try await runPython("""219        import orix, time, json220        t = {}221        s = orix.active_sheet()222        t0 = time.perf_counter(); rg = s.range("A1:F100001"); values = rg.values; t["read_values_s"] = time.perf_counter() - t0223        t0 = time.perf_counter(); df = rg.to_dataframe(); t["to_dataframe_s"] = time.perf_counter() - t0224        assert df.shape == (100000, 6), df.shape225        assert str(df["date"].dtype).startswith("datetime64"), df.dtypes226        assert df["units"].isna().sum() == 1031 and df["revenue"].dtype == float227        t0 = time.perf_counter(); summary = df.groupby("category")["revenue"].sum(); t["groupby_s"] = time.perf_counter() - t0228        t0 = time.perf_counter(); s.range("H1").write(df); t["write_dataframe_s"] = time.perf_counter() - t0229        s.range("P1").write(summary)230        print(json.dumps(t))231        """, on: py, host: host)232        let total = Date().timeIntervalSince(t0)233        #expect(r.succeeded, "\(r.stderr)")234        let t = (report(r) as? [String: Double]) ?? [:]235        print(String(format: "LARGE DATA (100 000 rows × 6 columns, %@ %@, restricted): read .values %.3f s · to_dataframe %.3f s · groupby %.4f s · write DataFrame %.3f s · whole run %.3f s (interpreter start + pandas import included)",236                     py.path, py.version, t["read_values_s"] ?? -1, t["to_dataframe_s"] ?? -1, t["groupby_s"] ?? -1, t["write_dataframe_s"] ?? -1, total))237        let w = try #require(r.writes.first)238        #expect(w.rowCount == n + 1 && w.columnCount == 6)239        #expect(w.rows[0] == rows[0])240        #expect(w.rows[1] == rows[1] && w.rows[98] == rows[98] && w.rows[n] == rows[n], "values survive the round trip exactly, blanks and dates included")241        #expect(w.formats == [PythonFormatHint(r0: 1, c0: 4, r1: n, c1: 4, format: "yyyy-mm-dd")])242        #expect(r.writes[1].rowCount == 24)243        #expect(total < 30, "100k-row exchange took \(total) s")244    }245}246
Files main
Menu

Theme