From e9208d7db14f5d0876bfa4d5f4b430efed4724bc Mon Sep 17 00:00:00 2001 From: Declan McCue Date: Fri, 2 Oct 2026 09:26:49 -0400 Subject: [PATCH] Skip tests instead of failing when in debug configurations, or when 'XC_PROJECT_FORMAT_TEST_PROJECT_SOURCE_DIR' is unavailable. --- Package.swift | 2 +- Sources/Tests/SampleData.swift | 11 ++++++++++- Sources/Tests/TestPerformance.swift | 16 ++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Package.swift b/Package.swift index b6c141e..2b0635f 100644 --- a/Package.swift +++ b/Package.swift @@ -48,7 +48,7 @@ let package = Package( ) for target in package.targets { - target.swiftSettings = [ + target.swiftSettings = (target.swiftSettings ?? []) + [ .enableUpcomingFeature("ExistentialAny"), .enableUpcomingFeature("ImmutableWeakCaptures"), .enableUpcomingFeature("InferIsolatedConformances"), diff --git a/Sources/Tests/SampleData.swift b/Sources/Tests/SampleData.swift index 79d0320..676ea84 100644 --- a/Sources/Tests/SampleData.swift +++ b/Sources/Tests/SampleData.swift @@ -11,6 +11,12 @@ import Foundation struct SampleData : Sendable { + static let sourceDirectoryEnvironmentVariable = "XC_PROJECT_FORMAT_TEST_PROJECT_SOURCE_DIR" + + static var sourceDirectory: String? { + ProcessInfo.processInfo.environment[sourceDirectoryEnvironmentVariable] + } + static let sharedInstance = Result { try SampleData() } @@ -32,11 +38,14 @@ struct SampleData : Sendable { let records: [Record] init(sourceDirectory: String) throws { let paths = try FileManager.default.recursivelyFindFiles(matchingExtension: "xcproj", in: sourceDirectory) + guard !paths.isEmpty else { + throw NSError("No .xcproj files found in \(sourceDirectory.quoted)") + } self.records = try paths.map(Record.init(path:)) } init() throws { - try self.init(sourceDirectory: ProcessInfo.processInfo.environment["XC_PROJECT_FORMAT_TEST_PROJECT_SOURCE_DIR"].unwrap(orThrow: "Missing environment variable for \("XC_PROJECT_FORMAT_TEST_PROJECT_SOURCE_DIR".quoted)")) + try self.init(sourceDirectory: Self.sourceDirectory.unwrap(orThrow: "Missing environment variable for \(Self.sourceDirectoryEnvironmentVariable.quoted)")) } } diff --git a/Sources/Tests/TestPerformance.swift b/Sources/Tests/TestPerformance.swift index 20d939e..5670649 100644 --- a/Sources/Tests/TestPerformance.swift +++ b/Sources/Tests/TestPerformance.swift @@ -16,9 +16,18 @@ extension TimeInterval { } } +#if ENABLE_PERFORMANCE_TESTS +private let arePerformanceTestsEnabled = true +#else +private let arePerformanceTestsEnabled = false +#endif + +@Suite( + .enabled(if: arePerformanceTestsEnabled, "Performance tests are only enabled in release builds."), + .enabled(if: SampleData.sourceDirectory != nil, "Set \(SampleData.sourceDirectoryEnvironmentVariable) to a directory of sample projects to run performance tests.") +) struct TestPerformance { func measureTransformingSampleProjectData(testName: String = #function, body: (SampleData.Record) throws -> Result) throws { - #if ENABLE_PERFORMANCE_TESTS let sampleData = try SampleData.sharedInstance.get() let testDurations = try Array(repetitions: 100) { try measureDuration { @@ -38,11 +47,6 @@ struct TestPerformance { print(" mean: \(mean?.formattedSeconds() ?? "N/A")") print(" median: \(median?.formattedSeconds() ?? "N/A")") print(" total: \(total.formattedSeconds())") - #else - #if compiler(>=6.3) - try Test.cancel("Performance tests are only enabled in release builds.") - #endif - #endif } @Test func testAnyJSONSerializationInstantiationPerformance() throws {