bugfixes, better MusicXML support and 1.1.0

This commit is contained in:
2026-06-10 08:41:07 -07:00
parent 6d3a0930a1
commit f59fd3b55a
228 changed files with 32271 additions and 2586 deletions
@@ -0,0 +1,128 @@
// 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: `<root>/Tests/LyrebirdCLITests/ProjectScaffolderBuildTests.swift`.
static var lyrebirdCheckoutRoot: String {
URL(fileURLWithPath: #filePath)
.deletingLastPathComponent() // LyrebirdCLITests/
.deletingLastPathComponent() // Tests/
.deletingLastPathComponent() // <root>/
.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] = ["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)
}
}
@@ -140,7 +140,10 @@ struct ProjectScaffolderTests {
@Test func workspaceContentsReferencesPackage() {
let contents: String = ProjectScaffolder.workspaceContents()
#expect(contents.contains("group:Package.swift"))
// Must ref the package *directory* (`group:.`), not the Package.swift
// file — the file form loads a loose document with no schemes.
#expect(contents.contains("group:."))
#expect(!contents.contains("group:Package.swift"))
#expect(contents.contains("<Workspace"))
#expect(contents.hasPrefix("<?xml"))
}
@@ -170,8 +173,8 @@ struct ProjectScaffolderTests {
#expect(contents.contains("group:WS.xcodeproj"),
"xcode workspace must ref .xcodeproj for \(template.rawValue)")
} else {
#expect(contents.contains("group:Package.swift"),
"spm workspace must ref Package.swift for \(template.rawValue)")
#expect(contents.contains("group:."),
"spm workspace must ref the package dir for \(template.rawValue)")
}
}
}
@@ -223,6 +226,9 @@ struct ProjectScaffolderTests {
#expect(ProjectTemplate(string: "ui-demo") == .uiDemo)
#expect(ProjectTemplate(string: "uidemo") == .uiDemo)
#expect(ProjectTemplate(string: "nrt") == .nrt)
#expect(ProjectTemplate(string: "score") == .score)
#expect(ProjectTemplate(string: "score-musicxml") == .score)
#expect(ProjectTemplate(string: "SCORE") == .score)
#expect(ProjectTemplate(string: "DEMO") == .demo)
#expect(ProjectTemplate(string: "bogus") == nil)
#expect(ProjectTemplate(string: "macos-ui-xcode") == .macosUIXcode)
@@ -238,6 +244,7 @@ struct ProjectScaffolderTests {
#expect(ProjectTemplate.demo.shipsXcodeProject == false)
#expect(ProjectTemplate.uiDemo.shipsXcodeProject == false)
#expect(ProjectTemplate.nrt.shipsXcodeProject == false)
#expect(ProjectTemplate.score.shipsXcodeProject == false)
#expect(ProjectTemplate.macosUIXcode.shipsXcodeProject == true)
#expect(ProjectTemplate.iosUI.shipsXcodeProject == true)
#expect(ProjectTemplate.auv3.shipsXcodeProject == true)
@@ -433,6 +440,60 @@ struct ProjectScaffolderTests {
#expect(!manifest.contains(".package(name:"))
}
// MARK: - defaultLyrebirdCheckoutPath resolution
@Test func defaultCheckoutPathNonNilFromSourceTree() throws {
// These tests run from inside the Lyrebird source checkout, so the
// computed property must resolve to a real root holding Package.swift.
let resolved: String = try #require(ProjectScaffolder.defaultLyrebirdCheckoutPath)
let manifest: String = (resolved as NSString).appendingPathComponent("Package.swift")
#expect(FileManager.default.fileExists(atPath: manifest))
}
@Test func checkoutRootResolvesParentThreeLevelsUp() {
let tmp: String = FileManager.default.temporaryDirectory
.appendingPathComponent("LyrebirdCheckoutRoot_\(UUID().uuidString)")
.path
try? FileManager.default.createDirectory(atPath: tmp,
withIntermediateDirectories: true,
attributes: nil)
defer { try? FileManager.default.removeItem(atPath: tmp) }
let manifest: String = (tmp as NSString).appendingPathComponent("Package.swift")
try? "// swift-tools-version: 6.0\n// Lyrebird".write(toFile: manifest,
atomically: true, encoding: .utf8)
// Synthetic source path: <tmp>/Sources/LyrebirdCLI/X.swift → up 3 == <tmp>.
let synthetic: String = (tmp as NSString)
.appendingPathComponent("Sources/LyrebirdCLI/X.swift")
#expect(ProjectScaffolder.lyrebirdCheckoutRoot(forSourceFile: synthetic) == tmp)
}
@Test func checkoutRootNilWhenNoManifest() {
let tmp: String = FileManager.default.temporaryDirectory
.appendingPathComponent("LyrebirdCheckoutNil_\(UUID().uuidString)")
.path
try? FileManager.default.createDirectory(atPath: tmp,
withIntermediateDirectories: true,
attributes: nil)
defer { try? FileManager.default.removeItem(atPath: tmp) }
let synthetic: String = (tmp as NSString).appendingPathComponent("a/b/c.swift")
#expect(ProjectScaffolder.lyrebirdCheckoutRoot(forSourceFile: synthetic) == nil)
}
@Test func checkoutRootNilWhenManifestNotLyrebird() {
let tmp: String = FileManager.default.temporaryDirectory
.appendingPathComponent("LyrebirdCheckoutForeign_\(UUID().uuidString)")
.path
try? FileManager.default.createDirectory(atPath: tmp,
withIntermediateDirectories: true,
attributes: nil)
defer { try? FileManager.default.removeItem(atPath: tmp) }
let manifest: String = (tmp as NSString).appendingPathComponent("Package.swift")
try? "// some unrelated SwiftPM package".write(toFile: manifest,
atomically: true, encoding: .utf8)
let synthetic: String = (tmp as NSString).appendingPathComponent("a/b/c.swift")
#expect(ProjectScaffolder.lyrebirdCheckoutRoot(forSourceFile: synthetic) == nil)
}
@Test func macOSAppPBXProjUsesLocalPackageRefWhenPathSet() {
let pbx: String = ProjectScaffolder.macOSAppPBXProj(
name: "Foo",
@@ -606,6 +667,30 @@ struct ProjectScaffolderTests {
#expect(!manifest.contains("LyrebirdUI"))
}
// MARK: - score template
@Test func scoreMainSwiftBuildsAndExportsScore() {
let source: String = ProjectScaffolder.mainSwift(name: "MyPiece", template: .score)
#expect(source.contains("Score(bpm:"))
#expect(source.contains("toMusicXMLString"))
#expect(source.contains("fromMusicXML"))
#expect(source.contains("score.musicxml"))
}
@Test func scoreMainSwiftRendersOfflineWithNRTDriver() {
let source: String = ProjectScaffolder.mainSwift(name: "MyPiece", template: .score)
#expect(source.contains("NRTDriver"))
#expect(source.contains("score.caf"))
#expect(source.contains("engine.schedule"))
// Must not embed the Lyrebird copyright in emitted user source.
#expect(!source.contains("Joshua Parmenter"))
}
@Test func scorePackageManifestDoesNotIncludeLyrebirdUI() {
let manifest: String = ProjectScaffolder.packageManifest(name: "MyPiece", template: .score)
#expect(!manifest.contains("LyrebirdUI"))
}
// MARK: - basic template unchanged by default
@Test func defaultTemplateIsBasic() {