Skip to content

Work around test discovery crash when running on older OS versions due to unguarded use of .timeLimit #409

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
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
22 changes: 21 additions & 1 deletion Tests/TestingTests/Test.SnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,33 @@ struct Test_SnapshotTests {
private static let bug: Bug = Bug.bug(12345, "Lorem ipsum")

@available(_clockAPI, *)
@Test("timeLimit property", .timeLimit(.minutes(999_999_999)))
@Test("timeLimit property", _timeLimitIfAvailable(minutes: 999_999_999))
func timeLimit() async throws {
let test = try #require(Test.current)
let snapshot = Test.Snapshot(snapshotting: test)

#expect(snapshot.timeLimit == .seconds(60) * 999_999_999)
}

/// Create a time limit trait representing the specified number of minutes, if
/// running on an OS which supports time limits.
///
/// - Parameters:
/// - minutes: The number of minutes the returned time limit trait should
/// represent.
///
/// - Returns: A time limit trait if the API is available, otherwise a
/// disabled trait.
///
/// This is provided in order to work around a bug where traits with
/// conditional API availability are not guarded by `@available` attributes on
/// `@Test` functions (rdar://127811571).
private static func _timeLimitIfAvailable(minutes: some BinaryInteger) -> any TestTrait {
guard #available(_clockAPI, *) else {
return .disabled(".timeLimit() not available")
}
return .timeLimit(.minutes(minutes))
}
}

extension Tag {
Expand Down
10 changes: 7 additions & 3 deletions Tests/TestingTests/Traits/TimeLimitTraitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,21 @@ struct TimeLimitTraitTests {

// MARK: - Fixtures

func timeLimitIfAvailable(minutes: UInt64) -> any SuiteTrait {
private func _timeLimitIfAvailable(minutes: UInt64) -> any SuiteTrait {
// @available can't be applied to a suite type, so we can't mark the suite as
// available only on newer OSes.
// available only on newer OSes. In addition, there is a related, known bug
// where traits with conditional API availability are not guarded by
// `@available` attributes on their associated `@Test` function
// (rdar://127811571). That is not directly relevant here but is worth noting
// if this trait is ever applied to `@Test` functions in this file.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No functional change in this file, just noting this related bug

if #available(_clockAPI, *) {
.timeLimit(.minutes(minutes))
} else {
.disabled(".timeLimit() not available")
}
}

@Suite(.hidden, timeLimitIfAvailable(minutes: 10))
@Suite(.hidden, _timeLimitIfAvailable(minutes: 10))
struct TestTypeThatTimesOut {
@available(_clockAPI, *)
@Test(.hidden, arguments: 0 ..< 10)
Expand Down