Skip to content

Merge 'main' branch to 'release/6.2' #1141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,23 @@ let package = Package(
"Testing",
"_Testing_CoreGraphics",
"_Testing_Foundation",
"MemorySafeTestingTests",
],
swiftSettings: .packageSettings
swiftSettings: .packageSettings + .disableMandatoryOptimizationsSettings
),

// Use a plain `.target` instead of a `.testTarget` to avoid the unnecessary
// overhead of having a separate test target for this module. Conceptually,
// the content in this module is no different than content which would
// typically be placed in the `TestingTests` target, except this content
// needs the (module-wide) strict memory safety feature to be enabled.
.target(
name: "MemorySafeTestingTests",
dependencies: [
"Testing",
],
path: "Tests/_MemorySafeTestingTests",
swiftSettings: .packageSettings + .strictMemorySafety
),

.macro(
Expand Down Expand Up @@ -219,7 +234,7 @@ package.targets.append(contentsOf: [
"Testing",
"TestingMacros",
],
swiftSettings: .packageSettings
swiftSettings: .packageSettings + .disableMandatoryOptimizationsSettings
)
])
#endif
Expand Down Expand Up @@ -275,7 +290,10 @@ extension Array where Element == PackageDescription.SwiftSetting {
// This setting is enabled in the package, but not in the toolchain build
// (via CMake). Enabling it is dependent on acceptance of the @section
// proposal via Swift Evolution.
.enableExperimentalFeature("SymbolLinkageMarkers"),
//
// FIXME: Re-enable this once a CI blocker is resolved:
// https://github.com/swiftlang/swift-testing/issues/1138.
// .enableExperimentalFeature("SymbolLinkageMarkers"),

// This setting is no longer needed when building with a 6.2 or later
// toolchain now that SE-0458 has been accepted and implemented, but it is
Expand Down Expand Up @@ -355,6 +373,32 @@ extension Array where Element == PackageDescription.SwiftSetting {

return result
}

/// Settings necessary to enable Strict Memory Safety, introduced in
/// [SE-0458: Opt-in Strict Memory Safety Checking](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0458-strict-memory-safety.md#swiftpm-integration).
static var strictMemorySafety: Self {
#if compiler(>=6.2)
// FIXME: Adopt official `.strictMemorySafety()` condition once the minimum
// supported toolchain is 6.2.
[.unsafeFlags(["-strict-memory-safety"])]
#else
[]
#endif
}

/// Settings which disable Swift's mandatory optimizations pass.
///
/// This is intended only to work around a build failure caused by a Swift
/// compiler regression which is expected to be resolved in
/// [swiftlang/swift#82034](https://github.com/swiftlang/swift/pull/82034).
///
/// @Comment {
/// - Bug: This should be removed once the CI issue is resolved.
/// [swiftlang/swift-testin#1138](https://github.com/swiftlang/swift-testing/issues/1138).
/// }
static var disableMandatoryOptimizationsSettings: Self {
[.unsafeFlags(["-Xllvm", "-sil-disable-pass=mandatory-performance-optimizations"])]
}
}

extension Array where Element == PackageDescription.CXXSetting {
Expand Down
3 changes: 3 additions & 0 deletions Sources/Testing/ExitTests/ExitTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ extension ExitTest {
///
/// - Warning: This function is used to implement the
/// `#expect(processExitsWith:)` macro. Do not use it directly.
#if compiler(>=6.2)
@safe
#endif
public static func __store<each T>(
_ id: (UInt64, UInt64, UInt64, UInt64),
_ body: @escaping @Sendable (repeat each T) async throws -> Void,
Expand Down
3 changes: 3 additions & 0 deletions Sources/Testing/Test+Discovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ extension Test {
///
/// - Warning: This function is used to implement the `@Test` macro. Do not
/// use it directly.
#if compiler(>=6.2)
@safe
#endif
public static func __store(
_ generator: @escaping @Sendable () async -> Test,
into outValue: UnsafeMutableRawPointer,
Expand Down
30 changes: 22 additions & 8 deletions Sources/Testing/Test+Macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ public macro Test<C>(
/// - collection: A collection of values to pass to the associated test
/// function.
///
/// During testing, the associated test function is called once for each element
/// in `collection`.
/// You can prefix the expression you pass to `collection` with `try` or `await`.
/// The testing library evaluates the expression lazily only if it determines
/// that the associated test will run. During testing, the testing library calls
/// the associated test function once for each element in `collection`.
///
/// @Comment {
/// - Bug: The testing library should support variadic generics.
Expand Down Expand Up @@ -270,7 +272,10 @@ extension Test {
/// - collection1: A collection of values to pass to `testFunction`.
/// - collection2: A second collection of values to pass to `testFunction`.
///
/// During testing, the associated test function is called once for each pair of
/// You can prefix the expressions you pass to `collection1` or `collection2`
/// with `try` or `await`. The testing library evaluates the expressions lazily
/// only if it determines that the associated test will run. During testing, the
/// testing library calls the associated test function once for each pair of
/// elements in `collection1` and `collection2`.
///
/// @Comment {
Expand Down Expand Up @@ -298,7 +303,10 @@ public macro Test<C1, C2>(
/// - collection1: A collection of values to pass to `testFunction`.
/// - collection2: A second collection of values to pass to `testFunction`.
///
/// During testing, the associated test function is called once for each pair of
/// You can prefix the expressions you pass to `collection1` or `collection2`
/// with `try` or `await`. The testing library evaluates the expressions lazily
/// only if it determines that the associated test will run. During testing, the
/// testing library calls the associated test function once for each pair of
/// elements in `collection1` and `collection2`.
///
/// @Comment {
Expand All @@ -324,8 +332,11 @@ public macro Test<C1, C2>(
/// - zippedCollections: Two zipped collections of values to pass to
/// `testFunction`.
///
/// During testing, the associated test function is called once for each element
/// in `zippedCollections`.
/// You can prefix the expression you pass to `zippedCollections` with `try` or
/// `await`. The testing library evaluates the expression lazily only if it
/// determines that the associated test will run. During testing, the testing
/// library calls the associated test function once for each element in
/// `zippedCollections`.
///
/// @Comment {
/// - Bug: The testing library should support variadic generics.
Expand All @@ -352,8 +363,11 @@ public macro Test<C1, C2>(
/// - zippedCollections: Two zipped collections of values to pass to
/// `testFunction`.
///
/// During testing, the associated test function is called once for each element
/// in `zippedCollections`.
/// You can prefix the expression you pass to `zippedCollections` with `try` or
/// `await`. The testing library evaluates the expression lazily only if it
/// determines that the associated test will run. During testing, the testing
/// library calls the associated test function once for each element in
/// `zippedCollections`.
///
/// @Comment {
/// - Bug: The testing library should support variadic generics.
Expand Down
31 changes: 31 additions & 0 deletions Sources/Testing/Testing.docc/ParameterizedTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,37 @@ func makeLargeOrder(count: Int) async throws {
- Note: Very large ranges such as `0 ..< .max` may take an excessive amount of
time to test, or may never complete due to resource constraints.

### Pass the same arguments to multiple test functions

If you want to pass the same collection of arguments to two or more
parameterized test functions, you can extract the arguments to a separate
function or property and pass it to each `@Test` attribute. For example:

```swift
extension Food {
static var bestSelling: [Food] {
get async throws { /* ... */ }
}
}

@Test(arguments: try await Food.bestSelling)
func `Order entree`(food: Food) {
let foodTruck = FoodTruck()
#expect(foodTruck.order(food))
}

@Test(arguments: try await Food.bestSelling)
func `Package leftovers`(food: Food) throws {
let foodTruck = FoodTruck()
let container = try #require(foodTruck.container(fitting: food))
try container.add(food)
}
```

> Tip: You can prefix expressions passed to `arguments:` with `try` or `await`.
> The testing library evaluates them lazily only if it determines that the
> associated test will run.

### Test with more than one collection

It's possible to test more than one collection. Consider the following test
Expand Down
2 changes: 1 addition & 1 deletion Sources/Testing/Testing.docc/Traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ types that customize the behavior of your tests.
<!--
### Handling issues

- ``Trait/transformIssues(_:)``
- ``Trait/compactMapIssues(_:)``
- ``Trait/filterIssues(_:)``
-->

Expand Down
38 changes: 28 additions & 10 deletions Sources/Testing/Traits/IssueHandlingTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
/// modifying one or more of its properties, and returning the copy. You can
/// observe recorded issues by returning them unmodified. Or you can suppress an
/// issue by either filtering it using ``Trait/filterIssues(_:)`` or returning
/// `nil` from the closure passed to ``Trait/transformIssues(_:)``.
/// `nil` from the closure passed to ``Trait/compactMapIssues(_:)``.
///
/// When an instance of this trait is applied to a suite, it is recursively
/// inherited by all child suites and tests.
///
/// To add this trait to a test, use one of the following functions:
///
/// - ``Trait/transformIssues(_:)``
/// - ``Trait/compactMapIssues(_:)``
/// - ``Trait/filterIssues(_:)``
@_spi(Experimental)
public struct IssueHandlingTrait: TestTrait, SuiteTrait {
Expand Down Expand Up @@ -96,15 +96,26 @@ extension IssueHandlingTrait: TestScoping {
return
}

// Ignore system issues, as they are not expected to be caused by users.
if case .system = issue.kind {
oldConfiguration.eventHandler(event, context)
return
}

// Use the original configuration's event handler when invoking the
// transformer to avoid infinite recursion if the transformer itself
// handler closure to avoid infinite recursion if the handler itself
// records new issues. This means only issue handling traits whose scope
// is outside this one will be allowed to handle such issues.
let newIssue = Configuration.withCurrent(oldConfiguration) {
handleIssue(issue)
}

if let newIssue {
// Prohibit assigning the issue's kind to system.
if case .system = newIssue.kind {
preconditionFailure("Issue returned by issue handling closure cannot have kind 'system': \(newIssue)")
}

var event = event
event.kind = .issueRecorded(newIssue)
oldConfiguration.eventHandler(event, context)
Expand All @@ -120,31 +131,35 @@ extension Trait where Self == IssueHandlingTrait {
/// Constructs an trait that transforms issues recorded by a test.
///
/// - Parameters:
/// - transformer: The closure called for each issue recorded by the test
/// - transform: A closure called for each issue recorded by the test
/// this trait is applied to. It is passed a recorded issue, and returns
/// an optional issue to replace the passed-in one.
///
/// - Returns: An instance of ``IssueHandlingTrait`` that transforms issues.
///
/// The `transformer` closure is called synchronously each time an issue is
/// The `transform` closure is called synchronously each time an issue is
/// recorded by the test this trait is applied to. The closure is passed the
/// recorded issue, and if it returns a non-`nil` value, that will be recorded
/// instead of the original. Otherwise, if the closure returns `nil`, the
/// issue is suppressed and will not be included in the results.
///
/// The `transformer` closure may be called more than once if the test records
/// The `transform` closure may be called more than once if the test records
/// multiple issues. If more than one instance of this trait is applied to a
/// test (including via inheritance from a containing suite), the `transformer`
/// test (including via inheritance from a containing suite), the `transform`
/// closure for each instance will be called in right-to-left, innermost-to-
/// outermost order, unless `nil` is returned, which will skip invoking the
/// remaining traits' closures.
///
/// Within `transformer`, you may access the current test or test case (if any)
/// Within `transform`, you may access the current test or test case (if any)
/// using ``Test/current`` ``Test/Case/current``, respectively. You may also
/// record new issues, although they will only be handled by issue handling
/// traits which precede this trait or were inherited from a containing suite.
public static func transformIssues(_ transformer: @escaping @Sendable (Issue) -> Issue?) -> Self {
Self(handler: transformer)
///
/// - Note: `transform` will never be passed an issue for which the value of
/// ``Issue/kind`` is ``Issue/Kind/system``, and may not return such an
/// issue.
public static func compactMapIssues(_ transform: @escaping @Sendable (Issue) -> Issue?) -> Self {
Self(handler: transform)
}

/// Constructs a trait that filters issues recorded by a test.
Expand Down Expand Up @@ -174,6 +189,9 @@ extension Trait where Self == IssueHandlingTrait {
/// using ``Test/current`` ``Test/Case/current``, respectively. You may also
/// record new issues, although they will only be handled by issue handling
/// traits which precede this trait or were inherited from a containing suite.
///
/// - Note: `isIncluded` will never be passed an issue for which the value of
/// ``Issue/kind`` is ``Issue/Kind/system``.
public static func filterIssues(_ isIncluded: @escaping @Sendable (Issue) -> Bool) -> Self {
Self { issue in
isIncluded(issue) ? issue : nil
Expand Down
3 changes: 2 additions & 1 deletion Sources/TestingMacros/ConditionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,11 @@ extension ExitTestConditionMacro {
var recordDecl: DeclSyntax?
#if !SWT_NO_LEGACY_TEST_DISCOVERY
let legacyEnumName = context.makeUniqueName("__🟡$")
let unsafeKeyword: TokenSyntax? = isUnsafeKeywordSupported ? .keyword(.unsafe, trailingTrivia: .space) : nil
recordDecl = """
enum \(legacyEnumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(enumName).testContentRecord
\(unsafeKeyword)\(enumName).testContentRecord
}
}
"""
Expand Down
3 changes: 2 additions & 1 deletion Sources/TestingMacros/SuiteDeclarationMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ public struct SuiteDeclarationMacro: MemberMacro, PeerMacro, Sendable {
#if !SWT_NO_LEGACY_TEST_DISCOVERY
// Emit a type that contains a reference to the test content record.
let enumName = context.makeUniqueName("__🟡$")
let unsafeKeyword: TokenSyntax? = isUnsafeKeywordSupported ? .keyword(.unsafe, trailingTrivia: .space) : nil
result.append(
"""
@available(*, deprecated, message: "This type is an implementation detail of the testing library. Do not use it directly.")
enum \(enumName): Testing.__TestContentRecordContainer {
nonisolated static var __testContentRecord: Testing.__TestContentRecord {
\(testContentRecordName)
\(unsafeKeyword)\(testContentRecordName)
}
}
"""
Expand Down
Loading