92 lines
3.1 KiB
Swift
92 lines
3.1 KiB
Swift
// LiveCompilerTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable import LyrebirdLiveCompile
|
|
|
|
#if os(macOS)
|
|
|
|
@Suite("LiveCompiler")
|
|
struct LiveCompilerTests {
|
|
|
|
/// Marker buffer the snippet writes to via the `ctx` pointer. One element so
|
|
/// we can confirm the entry symbol was actually invoked end-to-end. Held by
|
|
/// the test through a withUnsafeMutablePointer scope so the snippet write
|
|
/// lands in this process's address space.
|
|
@Test
|
|
func compileLoadAndInvokeEntry() throws {
|
|
// Snippet writes a sentinel `Int32` into the context pointer it gets handed.
|
|
let source: String = """
|
|
@_cdecl("lyrebird_snippet_entry")
|
|
public func lyrebird_snippet_entry(_ ctx: UnsafeMutableRawPointer?) {
|
|
guard let ctx else { return }
|
|
ctx.assumingMemoryBound(to: Int32.self).pointee = 0x4C425342 // 'LBSB'
|
|
}
|
|
"""
|
|
|
|
let snippet: CompiledSnippet = try LiveCompiler.compile(source: source)
|
|
#expect(snippet.moduleName.hasPrefix("LBSnippet_"))
|
|
#expect(snippet.dylibPath.hasSuffix(".dylib"))
|
|
|
|
var marker: Int32 = 0
|
|
withUnsafeMutablePointer(to: &marker) { ptr in
|
|
snippet.entry(UnsafeMutableRawPointer(ptr))
|
|
}
|
|
#expect(marker == 0x4C425342)
|
|
}
|
|
|
|
@Test
|
|
func compileFailureCarriesStderr() {
|
|
let source: String = """
|
|
@_cdecl("lyrebird_snippet_entry")
|
|
public func lyrebird_snippet_entry(_ ctx: UnsafeMutableRawPointer?) {
|
|
let _: Int = "this is a string, not an Int"
|
|
}
|
|
"""
|
|
|
|
do {
|
|
_ = try LiveCompiler.compile(source: source)
|
|
Issue.record("expected compileFailed, got success")
|
|
} catch let LiveCompileError.compileFailed(exitCode, stderr) {
|
|
#expect(exitCode != 0)
|
|
#expect(!stderr.isEmpty)
|
|
// Don't pin exact wording — Swift compiler diagnostics rephrase between
|
|
// toolchain versions. Just confirm the error is present in stderr.
|
|
#expect(stderr.lowercased().contains("error"))
|
|
} catch {
|
|
Issue.record("expected LiveCompileError.compileFailed, got \(error)")
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func missingEntrySymbolIsDetected() {
|
|
// Snippet compiles cleanly but exports no `lyrebird_snippet_entry`.
|
|
let source: String = """
|
|
public func someUnrelatedFunction() -> Int { 42 }
|
|
"""
|
|
|
|
do {
|
|
_ = try LiveCompiler.compile(source: source)
|
|
Issue.record("expected entrySymbolMissing, got success")
|
|
} catch let LiveCompileError.entrySymbolMissing(symbol, _) {
|
|
#expect(symbol == lyrebirdSnippetEntrySymbol)
|
|
} catch {
|
|
Issue.record("expected LiveCompileError.entrySymbolMissing, got \(error)")
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func swiftcResolvesToExecutablePath() throws {
|
|
let path: String = try LiveCompiler.findSwiftc()
|
|
#expect(!path.isEmpty)
|
|
// The resolved path must point at an actual executable on disk.
|
|
#expect(FileManager.default.isExecutableFile(atPath: path))
|
|
}
|
|
}
|
|
|
|
#endif
|