|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +#if canImport(Foundation) |
| 12 | +@_spi(ForToolsIntegrationOnly) public import Testing |
| 13 | +public import Foundation |
| 14 | +private import _TestingInternals |
| 15 | + |
| 16 | +@_spi(Experimental) |
| 17 | +@freestanding(expression) |
| 18 | +@discardableResult |
| 19 | +#if !SWT_NO_EXIT_TESTS |
| 20 | +@available(macOS 10.15.4, *) |
| 21 | +#else |
| 22 | +@_unavailableInEmbedded |
| 23 | +@available(*, unavailable, message: "Exit tests are not available on this platform.") |
| 24 | +#endif |
| 25 | +public macro expect( |
| 26 | + _ process: Process, |
| 27 | + exitsWith expectedExitCondition: ExitTest.Condition, |
| 28 | + observing observedValues: [any PartialKeyPath<ExitTest.Result> & Sendable] = [], |
| 29 | + _ comment: @autoclosure () -> Comment? = nil, |
| 30 | + sourceLocation: SourceLocation = #_sourceLocation |
| 31 | +) -> ExitTest.Result? = #externalMacro(module: "TestingMacros", type: "ExpectNSTaskExitsWithMacro") |
| 32 | + |
| 33 | +@_spi(Experimental) |
| 34 | +@freestanding(expression) |
| 35 | +@discardableResult |
| 36 | +#if !SWT_NO_EXIT_TESTS |
| 37 | +@available(macOS 10.15.4, *) |
| 38 | +#else |
| 39 | +@_unavailableInEmbedded |
| 40 | +@available(*, unavailable, message: "Exit tests are not available on this platform.") |
| 41 | +#endif |
| 42 | +public macro require( |
| 43 | + _ process: Process, |
| 44 | + exitsWith expectedExitCondition: ExitTest.Condition, |
| 45 | + observing observedValues: [any PartialKeyPath<ExitTest.Result> & Sendable] = [], |
| 46 | + _ comment: @autoclosure () -> Comment? = nil, |
| 47 | + sourceLocation: SourceLocation = #_sourceLocation |
| 48 | +) -> ExitTest.Result = #externalMacro(module: "TestingMacros", type: "RequireNSTaskExitsWithMacro") |
| 49 | + |
| 50 | +// MARK: - |
| 51 | + |
| 52 | +@_spi(Experimental) |
| 53 | +@discardableResult |
| 54 | +#if !SWT_NO_EXIT_TESTS |
| 55 | +@available(macOS 10.15.4, *) |
| 56 | +#else |
| 57 | +@_unavailableInEmbedded |
| 58 | +@available(*, unavailable, message: "Exit tests are not available on this platform.") |
| 59 | +#endif |
| 60 | +public func __check( |
| 61 | + _ process: Process, |
| 62 | + exitsWith expectedExitCondition: ExitTest.Condition, |
| 63 | + observing observedValues: [any PartialKeyPath<ExitTest.Result> & Sendable] = [], |
| 64 | + comments: @autoclosure () -> [Comment], |
| 65 | + isRequired: Bool, |
| 66 | + isolation: isolated (any Actor)? = #isolation, |
| 67 | + sourceLocation: SourceLocation |
| 68 | +) async -> Result<ExitTest.Result?, any Error> { |
| 69 | +#if !SWT_NO_EXIT_TESTS |
| 70 | + // The process may have already started and may already have a termination |
| 71 | + // handler set, so it's not possible for us to asynchronously wait for it. |
| 72 | + // As such, we'll have to block _some_ thread. |
| 73 | + var result: ExitTest.Result |
| 74 | + do { |
| 75 | + try await withCheckedThrowingContinuation { continuation in |
| 76 | + Thread.detachNewThread { |
| 77 | + do { |
| 78 | + // There's an obvious race condition here, but that's a limitation of |
| 79 | + // the Process/NSTask API and we'll just have to accept it. |
| 80 | + if !process.isRunning { |
| 81 | + try process.run() |
| 82 | + } |
| 83 | + process.waitUntilExit() |
| 84 | + continuation.resume() |
| 85 | + } catch { |
| 86 | + continuation.resume(throwing: error) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + let reason = process.terminationReason |
| 92 | + let exitStatus: ExitStatus = switch reason { |
| 93 | + case .exit: |
| 94 | + .exitCode(process.terminationStatus) |
| 95 | + case .uncaughtSignal: |
| 96 | + #if os(Windows) |
| 97 | + // On Windows, Foundation tries to map exit codes that look like HRESULT |
| 98 | + // values to signals, which is not the model Swift Testing uses. The |
| 99 | + // conversion is lossy, so there's not much we can do here other than treat |
| 100 | + // it as an exit code too. |
| 101 | + .exitCode(process.terminationStatus) |
| 102 | + #else |
| 103 | + .signal(process.terminationStatus) |
| 104 | + #endif |
| 105 | + @unknown default: |
| 106 | + fatalError("Unexpected termination reason '\(reason)' from process \(process). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new") |
| 107 | + } |
| 108 | + |
| 109 | + result = ExitTest.Result(exitStatus: exitStatus) |
| 110 | + func makeContent(from streamObject: Any?) -> [UInt8] { |
| 111 | + if let fileHandle = streamObject as? FileHandle { |
| 112 | + if let content = try? fileHandle.readToEnd() { |
| 113 | + return Array(content) |
| 114 | + } |
| 115 | + } else if let pipe = streamObject as? Pipe { |
| 116 | + return makeContent(from: pipe.fileHandleForReading) |
| 117 | + } |
| 118 | + |
| 119 | + return [] |
| 120 | + } |
| 121 | + if observedValues.contains(\.standardOutputContent) { |
| 122 | + result.standardOutputContent = makeContent(from: process.standardOutput) |
| 123 | + } |
| 124 | + if observedValues.contains(\.standardErrorContent) { |
| 125 | + result.standardErrorContent = makeContent(from: process.standardError) |
| 126 | + } |
| 127 | + } catch { |
| 128 | + // As with the main exit test implementation, if an error occurs while |
| 129 | + // trying to run the exit test, treat it as a system error and treat the |
| 130 | + // condition as a mismatch. |
| 131 | + let issue = Issue( |
| 132 | + kind: .system, |
| 133 | + comments: comments() + CollectionOfOne(Comment(rawValue: String(describingForTest: error))), |
| 134 | + sourceContext: SourceContext(backtrace: nil, sourceLocation: sourceLocation) |
| 135 | + ) |
| 136 | + issue.record() |
| 137 | + |
| 138 | + let exitStatus: ExitStatus = if expectedExitCondition.isApproximatelyEqual(to: .exitCode(EXIT_FAILURE)) { |
| 139 | + .exitCode(EXIT_SUCCESS) |
| 140 | + } else { |
| 141 | + .exitCode(EXIT_FAILURE) |
| 142 | + } |
| 143 | + result = ExitTest.Result(exitStatus: exitStatus) |
| 144 | + } |
| 145 | + |
| 146 | + let expression = Expression("expectedExitCondition") |
| 147 | + return __checkValue( |
| 148 | + expectedExitCondition.isApproximatelyEqual(to: result.exitStatus), |
| 149 | + expression: expression, |
| 150 | + expressionWithCapturedRuntimeValues: expression.capturingRuntimeValues(result.exitStatus), |
| 151 | + comments: comments(), |
| 152 | + isRequired: isRequired, |
| 153 | + sourceLocation: sourceLocation |
| 154 | + ).map { _ in result } |
| 155 | +#else |
| 156 | + swt_unreachable() |
| 157 | +#endif |
| 158 | +} |
| 159 | +#endif |
0 commit comments