|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SWBUtil |
| 14 | +import SWBCore |
| 15 | +import SWBTaskExecution |
| 16 | +import ArgumentParser |
| 17 | + |
| 18 | +class TestEntryPointGenerationTaskAction: TaskAction { |
| 19 | + override class var toolIdentifier: String { |
| 20 | + "TestEntryPointGenerationTaskAction" |
| 21 | + } |
| 22 | + |
| 23 | + override func performTaskAction(_ task: any ExecutableTask, dynamicExecutionDelegate: any DynamicTaskExecutionDelegate, executionDelegate: any TaskExecutionDelegate, clientDelegate: any TaskExecutionClientDelegate, outputDelegate: any TaskOutputDelegate) async -> CommandResult { |
| 24 | + do { |
| 25 | + let options = try Options.parse(Array(task.commandLineAsStrings.dropFirst())) |
| 26 | + try executionDelegate.fs.write(options.output, contents: #""" |
| 27 | + #if canImport(Testing) |
| 28 | + import Testing |
| 29 | + #endif |
| 30 | +
|
| 31 | + @main |
| 32 | + @available(macOS 10.15, iOS 11, watchOS 4, tvOS 11, visionOS 1, *) |
| 33 | + @available(*, deprecated, message: "Not actually deprecated. Marked as deprecated to allow inclusion of deprecated tests (which test deprecated functionality) without warnings") |
| 34 | + struct Runner { |
| 35 | + private static func testingLibrary() -> String { |
| 36 | + var iterator = CommandLine.arguments.makeIterator() |
| 37 | + while let argument = iterator.next() { |
| 38 | + if argument == "--testing-library", let libraryName = iterator.next() { |
| 39 | + return libraryName.lowercased() |
| 40 | + } |
| 41 | + } |
| 42 | +
|
| 43 | + // Fallback if not specified: run XCTest (legacy behavior) |
| 44 | + return "xctest" |
| 45 | + } |
| 46 | + |
| 47 | + #if os(Linux) |
| 48 | + @_silgen_name("$ss13_runAsyncMainyyyyYaKcF") |
| 49 | + private static func _runAsyncMain(_ asyncFun: @Sendable @escaping () async throws -> ()) |
| 50 | +
|
| 51 | + static func main() { |
| 52 | + let testingLibrary = Self.testingLibrary() |
| 53 | + #if canImport(Testing) |
| 54 | + if testingLibrary == "swift-testing" { |
| 55 | + _runAsyncMain { |
| 56 | + await Testing.__swiftPMEntryPoint() as Never |
| 57 | + } |
| 58 | + } |
| 59 | + #endif |
| 60 | + } |
| 61 | + #else |
| 62 | + static func main() async { |
| 63 | + let testingLibrary = Self.testingLibrary() |
| 64 | + #if canImport(Testing) |
| 65 | + if testingLibrary == "swift-testing" { |
| 66 | + await Testing.__swiftPMEntryPoint() as Never |
| 67 | + } |
| 68 | + #endif |
| 69 | + } |
| 70 | + #endif |
| 71 | + } |
| 72 | + """#) |
| 73 | + return .succeeded |
| 74 | + } catch { |
| 75 | + outputDelegate.emitError("\(error)") |
| 76 | + return .failed |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +private struct Options: ParsableArguments { |
| 82 | + @Option var output: Path |
| 83 | +} |
0 commit comments