|
| 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 | +/// A type that allows transforming or filtering the issues recorded by a test. |
| 12 | +/// |
| 13 | +/// Use this type to observe or customize the issue(s) recorded by the test this |
| 14 | +/// trait is applied to. You can transform a recorded issue by copying it, |
| 15 | +/// modifying one or more of its properties, and returning the copy. You can |
| 16 | +/// observe recorded issues by returning them unmodified. Or you can suppress an |
| 17 | +/// issue by either filtering it using ``Trait/filterIssues(_:)`` or returning |
| 18 | +/// `nil` from the closure passed to ``Trait/transformIssues(_:)``. |
| 19 | +/// |
| 20 | +/// When an instance of this trait is applied to a suite, it is recursively |
| 21 | +/// inherited by all child suites and tests. |
| 22 | +/// |
| 23 | +/// To add this trait to a test, use one of the following functions: |
| 24 | +/// |
| 25 | +/// - ``Trait/transformIssues(_:)`` |
| 26 | +/// - ``Trait/filterIssues(_:)`` |
| 27 | +@_spi(Experimental) |
| 28 | +public struct IssueHandlingTrait: TestTrait, SuiteTrait { |
| 29 | + /// A function which transforms an issue and returns an optional replacement. |
| 30 | + /// |
| 31 | + /// - Parameters: |
| 32 | + /// - issue: The issue to transform. |
| 33 | + /// |
| 34 | + /// - Returns: An issue to replace `issue`, or else `nil` if the issue should |
| 35 | + /// not be recorded. |
| 36 | + fileprivate typealias Transformer = @Sendable (_ issue: Issue) -> Issue? |
| 37 | + |
| 38 | + /// This trait's transformer function. |
| 39 | + private var _transformer: Transformer |
| 40 | + |
| 41 | + fileprivate init(transformer: @escaping Transformer) { |
| 42 | + _transformer = transformer |
| 43 | + } |
| 44 | + |
| 45 | + public var isRecursive: Bool { |
| 46 | + true |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +extension IssueHandlingTrait: TestScoping { |
| 51 | + public func scopeProvider(for test: Test, testCase: Test.Case?) -> Self? { |
| 52 | + // Provide scope for tests at both the suite and test case levels, but not |
| 53 | + // for the test function level. This avoids redundantly invoking the closure |
| 54 | + // twice, and potentially double-processing, issues recorded by test |
| 55 | + // functions. |
| 56 | + test.isSuite || testCase != nil ? self : nil |
| 57 | + } |
| 58 | + |
| 59 | + public func provideScope(for test: Test, testCase: Test.Case?, performing function: @Sendable () async throws -> Void) async throws { |
| 60 | + try await provideScope(performing: function) |
| 61 | + } |
| 62 | + |
| 63 | + /// Provide scope for a specified function. |
| 64 | + /// |
| 65 | + /// - Parameters: |
| 66 | + /// - function: The function to perform. |
| 67 | + /// |
| 68 | + /// This is a simplified version of ``provideScope(for:testCase:performing:)`` |
| 69 | + /// which doesn't accept test or test case parameters. It's included so that |
| 70 | + /// a runner can invoke this trait's closure even when there is no test case, |
| 71 | + /// such as if a trait on a test function threw an error during `prepare(for:)` |
| 72 | + /// and caused an issue to be recorded for the test function. In that scenario, |
| 73 | + /// this trait still needs to be invoked, but its `scopeProvider(for:testCase:)` |
| 74 | + /// intentionally returns `nil` (see the comment in that method), so this |
| 75 | + /// function can be called instead to ensure this trait can still handle that |
| 76 | + /// issue. |
| 77 | + func provideScope(performing function: @Sendable () async throws -> Void) async throws { |
| 78 | + guard var configuration = Configuration.current else { |
| 79 | + preconditionFailure("Configuration.current is nil when calling \(#function). Please file a bug report at https://github.com/swiftlang/swift-testing/issues/new") |
| 80 | + } |
| 81 | + |
| 82 | + configuration.eventHandler = { [oldConfiguration = configuration] event, context in |
| 83 | + guard case let .issueRecorded(issue) = event.kind else { |
| 84 | + oldConfiguration.eventHandler(event, context) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // Use the original configuration's event handler when invoking the |
| 89 | + // transformer to avoid infinite recursion if the transformer itself |
| 90 | + // records new issues. This means only issue handling traits whose scope |
| 91 | + // is outside this one will be allowed to handle such issues. |
| 92 | + let newIssue = Configuration.withCurrent(oldConfiguration) { |
| 93 | + _transformer(issue) |
| 94 | + } |
| 95 | + |
| 96 | + if let newIssue { |
| 97 | + var event = event |
| 98 | + event.kind = .issueRecorded(newIssue) |
| 99 | + oldConfiguration.eventHandler(event, context) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + try await Configuration.withCurrent(configuration, perform: function) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +@_spi(Experimental) |
| 108 | +extension Trait where Self == IssueHandlingTrait { |
| 109 | + /// Constructs an trait that transforms issues recorded by a test. |
| 110 | + /// |
| 111 | + /// - Parameters: |
| 112 | + /// - transformer: The closure called for each issue recorded by the test |
| 113 | + /// this trait is applied to. It is passed a recorded issue, and returns |
| 114 | + /// an optional issue to replace the passed-in one. |
| 115 | + /// |
| 116 | + /// The `transformer` closure is called synchronously each time an issue is |
| 117 | + /// recorded by the test this trait is applied to. The closure is passed the |
| 118 | + /// recorded issue, and if it returns a non-`nil` value, that will be recorded |
| 119 | + /// instead of the original. Otherwise, if the closure returns `nil`, the |
| 120 | + /// issue is suppressed and will not be included in the results. |
| 121 | + /// |
| 122 | + /// The `transformer` closure may be called more than once if the test records |
| 123 | + /// multiple issues. If more than one instance of this trait is applied to a |
| 124 | + /// test (including via inheritance from a containing suite), the `transformer` |
| 125 | + /// closure for each instance will be called in right-to-left, innermost-to- |
| 126 | + /// outermost order, unless `nil` is returned, which will skip invoking the |
| 127 | + /// remaining traits' closures. |
| 128 | + /// |
| 129 | + /// Within `transformer`, you may access the current test or test case (if any) |
| 130 | + /// using ``Test/current`` ``Test/Case/current``, respectively. You may also |
| 131 | + /// record new issues, although they will only be handled by issue handling |
| 132 | + /// traits which precede this trait or were inherited from a containing suite. |
| 133 | + public static func transformIssues(_ transformer: @escaping @Sendable (Issue) -> Issue?) -> Self { |
| 134 | + Self(transformer: transformer) |
| 135 | + } |
| 136 | + |
| 137 | + /// Constructs a trait that filters issues recorded by a test. |
| 138 | + /// |
| 139 | + /// - Parameters: |
| 140 | + /// - isIncluded: The predicate with which to filter issues recorded by the |
| 141 | + /// test this trait is applied to. It is passed a recorded issue, and |
| 142 | + /// should return `true` if the issue should be included, or `false` if it |
| 143 | + /// should be suppressed. |
| 144 | + /// |
| 145 | + /// The `isIncluded` closure is called synchronously each time an issue is |
| 146 | + /// recorded by the test this trait is applied to. The closure is passed the |
| 147 | + /// recorded issue, and if it returns `true`, the issue will be preserved in |
| 148 | + /// the test results. Otherwise, if the closure returns `false`, the issue |
| 149 | + /// will not be included in the test results. |
| 150 | + /// |
| 151 | + /// The `isIncluded` closure may be called more than once if the test records |
| 152 | + /// multiple issues. If more than one instance of this trait is applied to a |
| 153 | + /// test (including via inheritance from a containing suite), the `isIncluded` |
| 154 | + /// closure for each instance will be called in right-to-left, innermost-to- |
| 155 | + /// outermost order, unless `false` is returned, which will skip invoking the |
| 156 | + /// remaining traits' closures. |
| 157 | + /// |
| 158 | + /// Within `isIncluded`, you may access the current test or test case (if any) |
| 159 | + /// using ``Test/current`` ``Test/Case/current``, respectively. You may also |
| 160 | + /// record new issues, although they will only be handled by issue handling |
| 161 | + /// traits which precede this trait or were inherited from a containing suite. |
| 162 | + public static func filterIssues(_ isIncluded: @escaping @Sendable (Issue) -> Bool) -> Self { |
| 163 | + Self { issue in |
| 164 | + isIncluded(issue) ? issue : nil |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments