// ProjectScaffolderBuildTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable import LyrebirdCLI /// Slow integration suite: scaffolds each non-Xcode SPM template into a temp /// directory, wires it to *this* Lyrebird checkout via `--lyrebird-path`, and /// runs a real `swift build`, asserting exit 0. /// /// The plain `ProjectScaffolderTests` suite only checks generated source via /// *string substring* matches — it never compiles the templates against the /// live Lyrebird API. That gap let several templates drift out of sync with the /// API they pin: the `score` template's `Part(clef:)` / `event.performedDuration` /// usage built against current `main` but not against the older published tag /// the generated project resolved; `basic`/`demo` emitted an unbound `\(name)`; /// `ui-demo`'s `AppDelegate.init` predated `SynthesizerBase`'s `@MainActor`. /// This suite closes the gap by actually building the emitted sources against /// *this* checkout — the same source the scaffolder now pins by default. /// /// **Gated** behind the `LYREBIRD_RUN_BUILD_TESTS` env var because each case /// does a full SPM dependency resolution (network) + a from-scratch compile of /// the Lyrebird package — minutes per template, not milliseconds. Off by /// default so the normal `swift test` run stays fast and offline. /// /// Run with: /// ``` /// LYREBIRD_RUN_BUILD_TESTS=1 swift test --no-parallel --filter ProjectScaffolderBuild /// ``` @Suite( "ProjectScaffolderBuild", .enabled(if: ProcessInfo.processInfo.environment["LYREBIRD_RUN_BUILD_TESTS"] != nil, "set LYREBIRD_RUN_BUILD_TESTS=1 to run the slow scaffold-and-build integration suite"), // Swift Testing runs parametrized cases in parallel by default (the // `swift test --no-parallel` flag only governs the XCTest path). Five // concurrent `swift build` processes saturate the machine and contend, so // serialize: each template builds one at a time. .serialized ) struct ProjectScaffolderBuildTests { /// Absolute path to the root of this Lyrebird checkout — the directory /// holding the top-level `Package.swift`. Derived from this source file's /// location: `/Tests/LyrebirdCLITests/ProjectScaffolderBuildTests.swift`. static var lyrebirdCheckoutRoot: String { URL(fileURLWithPath: #filePath) .deletingLastPathComponent() // LyrebirdCLITests/ .deletingLastPathComponent() // Tests/ .deletingLastPathComponent() // / .path } /// Every SPM template that emits a `Package.swift` + `main.swift`, keyed by /// its canonical raw value. The xcode-bearing templates /// (macos-ui-xcode/ios-ui/auv3) are excluded — they require `xcodebuild`, /// code signing, and an app/extension host, not a plain `swift build`. /// /// Raw strings (not `ProjectTemplate` cases) are the parametrized arguments /// because `@Test(arguments:)` requires `Sendable` and the public /// `ProjectTemplate` enum does not declare the conformance. static let spmTemplateRawValues: [String] = ["quickstart", "basic", "demo", "nrt", "score", "ui-demo"] @Test(arguments: spmTemplateRawValues) func templateBuildsAgainstLiveLyrebird(_ rawValue: String) throws { let template: ProjectTemplate = try #require( ProjectTemplate(string: rawValue), "unknown template raw value \(rawValue)" ) #expect(template.shipsXcodeProject == false, "this suite only covers SPM templates; \(template.rawValue) ships an .xcodeproj") let fm: FileManager = FileManager.default let tmp: String = fm.temporaryDirectory .appendingPathComponent("LyrebirdBuildTest_\(template.rawValue)_\(UUID().uuidString)") .path try fm.createDirectory(atPath: tmp, withIntermediateDirectories: true, attributes: nil) defer { try? fm.removeItem(atPath: tmp) } // Wire the generated Package.swift to THIS checkout so the build // compiles against the current API rather than the published tag. let projectName: String = "BuildProbe" try ProjectScaffolder.scaffold( name: projectName, template: template, in: tmp, lyrebirdPath: Self.lyrebirdCheckoutRoot ) let projectRoot: String = (tmp as NSString).appendingPathComponent(projectName) let result: (exitCode: Int32, output: String) = try Self.runSwiftBuild(in: projectRoot) #expect(result.exitCode == 0, """ `swift build` failed for template \(template.rawValue) (exit \(result.exitCode)): \(result.output) """) } // MARK: - swift build runner /// Run `swift build` in `directory`, capturing merged stdout+stderr. private static func runSwiftBuild(in directory: String) throws -> (exitCode: Int32, output: String) { let process: Process = Process() process.executableURL = URL(fileURLWithPath: "/usr/bin/env") process.arguments = ["swift", "build"] process.currentDirectoryURL = URL(fileURLWithPath: directory) let pipe: Pipe = Pipe() process.standardOutput = pipe process.standardError = pipe try process.run() // Drain the pipe before waitUntilExit to avoid a deadlock when the // child fills the 64 KB pipe buffer (a full compile is verbose). let data: Data = pipe.fileHandleForReading.readDataToEndOfFile() process.waitUntilExit() let output: String = String(data: data, encoding: .utf8) ?? "" return (process.terminationStatus, output) } }