import Foundation
struct ScriptLaneResult: Codable, Equatable, Sendable {
let name: String
let seconds: Double
let status: Int32
let detail: String
}
enum ScriptLaneTools {
static let rootFilter = #"^(NameGuardTests|ParcelDevTests)\."#
static func run(_ args: [String]) throws -> Int32 {
guard args.count == 2 else { throw DevError("Usage: run-script-tests REPOSITORY OUTPUT_DIRECTORY") }
let root = URL(fileURLWithPath: args[0]).standardizedFileURL
let output = URL(fileURLWithPath: args[1]).standardizedFileURL
let scripts = try FileManager.default.contentsOfDirectory(
at: root.appendingPathComponent("Scripts/tests"), includingPropertiesForKeys: nil
).filter { $0.pathExtension == "sh" }.sorted { $0.lastPathComponent < $1.lastPathComponent }
guard !scripts.isEmpty else { throw DevError("No shell fixtures found") }
try FileManager.default.createDirectory(at: output, withIntermediateDirectories: true)
var environment = ProcessInfo.processInfo.environment
for name in [
"PARCEL_E2E", "PARCEL_E2E_PREFLIGHT_ONLY", "PARCEL_NATIVE_HOST", "PARCEL_BENCH", "PARCEL_BENCH_SMOKE",
"PARCEL_REALITY", "PARCEL_REALITY_VOICEOVER", "PARCEL_REALITY_SCREEN_CAPTURE",
"PARCEL_TEST_SKIP_BUILD", "PARCEL_TEST_XUNIT_OUTPUT", "PARCEL_SCRIPT_RESULTS",
"PARCEL_SCRIPT_ROOT_TESTS_DONE",
] {
environment.removeValue(forKey: name)
}
var results: [ScriptLaneResult] = []
let clock = ContinuousClock()
func execute(_ name: String, _ executable: String, _ arguments: [String]) throws -> ScriptLaneResult {
let start = clock.now
let status: Int32
let detail: String
do {
let result = try DevProcess.run(executable, arguments, directory: root, environment: environment)
status = result.status
detail = result.output + result.errorOutput
} catch {
status = 1
detail = String(describing: error)
}
let duration = start.duration(to: clock.now).components
let seconds = Double(duration.seconds) + Double(duration.attoseconds) / 1e18
try Data(detail.utf8).write(to: output.appendingPathComponent(name + ".log"), options: .atomic)
return .init(name: name, seconds: seconds, status: status, detail: status == 0 ? "" : detail)
}
func record(_ result: ScriptLaneResult) throws {
let line =
"\(result.status == 0 ? "PASS" : "FAIL") \(result.name) (\(String(format: "%.3f", result.seconds)) s)\n"
try FileHandle.standardOutput.write(contentsOf: Data(line.utf8))
results.append(result)
}
let discovery = try execute("discovery", "swift", ["test", "list", "--skip-build", "--filter", rootFilter])
try record(discovery)
let discoveryText = try String(contentsOf: output.appendingPathComponent("discovery.log"), encoding: .utf8)
let identifiers = discoveryText.split(separator: "\n").map(String.init)
let missing = ["NameGuardTests.", "ParcelDevTests."].filter { prefix in
!identifiers.contains(where: { $0.hasPrefix(prefix) && $0.contains("/") })
}
let report = output.appendingPathComponent("junit-scripts.xml")
// Old reports must never make a failed or empty invocation look successful.
for file in [report, output.appendingPathComponent("junit-scripts-swift-testing.xml")] {
if FileManager.default.fileExists(atPath: file.path) { try FileManager.default.removeItem(at: file) }
}
if discovery.status == 0 && missing.isEmpty {
var result = try execute(
"root-swift", "swift",
["test", "--skip-build", "--disable-xctest", "--filter", rootFilter, "--xunit-output", report.path])
if result.status == 0 {
do {
let count = try executedTests(in: report, requiringTargets: ["NameGuardTests", "ParcelDevTests"])
guard count > 0 else { throw DevError("No root Swift tests executed") }
} catch {
result = .init(
name: result.name, seconds: result.seconds, status: 1, detail: String(describing: error))
}
}
try record(result)
} else {
try record(
.init(
name: "root-swift", seconds: 0, status: 1, detail: "Discovery failed or missing targets: \(missing)"
))
}
environment["PARCEL_SCRIPT_ROOT_TESTS_DONE"] = "1"
for script in scripts { try record(try execute(script.lastPathComponent, "/bin/bash", [script.path])) }
try write(results, to: output)
return results.contains(where: { $0.status != 0 }) ? 1 : 0
}
static func write(_ results: [ScriptLaneResult], to output: URL) throws {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
try encoder.encode(results).write(to: output.appendingPathComponent("script-results.json"), options: .atomic)
let shell = results.filter { $0.name.hasSuffix(".sh") }
let cases = shell.map { result in
let failure =
result.status == 0 ? "" : "\(xml(result.detail))"
return
"\(failure)"
}.joined(separator: "\n")
let junit =
"\n\n\(cases)\n\n"
try Data(junit.utf8).write(to: output.appendingPathComponent("junit-shell.xml"), options: .atomic)
var summary = "## Script lane\n\n| Phase / fixture | Result | Seconds |\n|---|---|---:|\n"
for result in results {
summary +=
"| \(result.name.replacingOccurrences(of: "|", with: "\\|")) | \(result.status == 0 ? "pass" : "fail (\(result.status))") | \(String(format: "%.3f", result.seconds)) |\n"
}
try Data(summary.utf8).write(to: output.appendingPathComponent("script-summary.md"), options: .atomic)
}
static func xml(_ value: String) -> String {
String(
value.unicodeScalars.filter { scalar in
scalar.value == 9 || scalar.value == 10 || scalar.value == 13
|| (32...0xD7FF).contains(scalar.value) || (0xE000...0xFFFD).contains(scalar.value)
|| (0x10000...0x10FFFF).contains(scalar.value)
}
).replacingOccurrences(of: "&", with: "&")
.replacingOccurrences(of: "<", with: "<")
.replacingOccurrences(of: ">", with: ">")
.replacingOccurrences(of: "\"", with: """)
.replacingOccurrences(of: "'", with: "'")
}
static func executedTests(in file: URL, requiringTargets targets: Set = []) throws -> Int {
let parser = XMLParser(data: try Data(contentsOf: file))
let counter = TestCounter()
parser.delegate = counter
parser.shouldResolveExternalEntities = false
guard parser.parse(), ["testsuite", "testsuites"].contains(counter.root ?? ""), !counter.failed else {
throw DevError("Invalid or failed test report: \(file.path)")
}
guard targets.isSubset(of: counter.targets) else {
throw DevError("Missing executed tooling targets: \(targets.subtracting(counter.targets).sorted())")
}
return counter.executed
}
}
private final class TestCounter: NSObject, XMLParserDelegate {
var root: String?
var executed = 0
var skipped = false
var failed = false
var currentTarget: String?
var targets = Set()
func parser(
_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?,
qualifiedName qName: String?, attributes attributeDict: [String: String]
) {
if root == nil { root = elementName }
if elementName == "testcase" {
skipped = false
currentTarget = attributeDict["classname"]?.split(separator: ".").first.map(String.init)
}
if elementName == "skipped" { skipped = true }
if ["failure", "error"].contains(elementName) { failed = true }
}
func parser(
_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?
) {
if elementName == "testcase" && !skipped {
executed += 1
if let currentTarget { targets.insert(currentTarget) }
}
}
}