From afa0526c2e24098d8eba6d6c6657aa0ef1b4b566 Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Thu, 6 Feb 2025 14:37:45 +0000 Subject: [PATCH 1/3] wip --- Sources/IssueReporting/ReportIssue.swift | 72 ++++++++++--------- Sources/IssueReporting/TestContext.swift | 43 +++++++++++ .../SwiftTestingTests.swift | 6 ++ Tests/IssueReportingTests/XCTestTests.swift | 6 ++ 4 files changed, 93 insertions(+), 34 deletions(-) diff --git a/Sources/IssueReporting/ReportIssue.swift b/Sources/IssueReporting/ReportIssue.swift index 3a2af15..a66fbcf 100644 --- a/Sources/IssueReporting/ReportIssue.swift +++ b/Sources/IssueReporting/ReportIssue.swift @@ -60,23 +60,25 @@ public func reportIssue( } return } - - switch context { - case .swiftTesting: - _recordIssue( - message: message(), - fileID: "\(IssueContext.current?.fileID ?? fileID)", - filePath: "\(IssueContext.current?.filePath ?? filePath)", - line: Int(IssueContext.current?.line ?? line), - column: Int(IssueContext.current?.column ?? column) - ) - case .xcTest: - _XCTFail( - message().withAppHostWarningIfNeeded() ?? "", - file: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line - ) - @unknown default: break + + if TestContext.emitsFailureOnReportIssue { + switch context { + case .swiftTesting: + _recordIssue( + message: message(), + fileID: "\(IssueContext.current?.fileID ?? fileID)", + filePath: "\(IssueContext.current?.filePath ?? filePath)", + line: Int(IssueContext.current?.line ?? line), + column: Int(IssueContext.current?.column ?? column) + ) + case .xcTest: + _XCTFail( + message().withAppHostWarningIfNeeded() ?? "", + file: IssueContext.current?.filePath ?? filePath, + line: IssueContext.current?.line ?? line + ) + @unknown default: break + } } } @@ -130,22 +132,24 @@ public func reportIssue( return } - switch context { - case .swiftTesting: - _recordError( - error: error, - message: message(), - fileID: "\(IssueContext.current?.fileID ?? fileID)", - filePath: "\(IssueContext.current?.filePath ?? filePath)", - line: Int(IssueContext.current?.line ?? line), - column: Int(IssueContext.current?.column ?? column) - ) - case .xcTest: - _XCTFail( - "Caught error: \(error)\(message().map { ": \($0)" } ?? "")".withAppHostWarningIfNeeded(), - file: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line - ) - @unknown default: break + if TestContext.emitsFailureOnReportIssue { + switch context { + case .swiftTesting: + _recordError( + error: error, + message: message(), + fileID: "\(IssueContext.current?.fileID ?? fileID)", + filePath: "\(IssueContext.current?.filePath ?? filePath)", + line: Int(IssueContext.current?.line ?? line), + column: Int(IssueContext.current?.column ?? column) + ) + case .xcTest: + _XCTFail( + "Caught error: \(error)\(message().map { ": \($0)" } ?? "")".withAppHostWarningIfNeeded(), + file: IssueContext.current?.filePath ?? filePath, + line: IssueContext.current?.line ?? line + ) + @unknown default: break + } } } diff --git a/Sources/IssueReporting/TestContext.swift b/Sources/IssueReporting/TestContext.swift index 9d8c612..e00c561 100644 --- a/Sources/IssueReporting/TestContext.swift +++ b/Sources/IssueReporting/TestContext.swift @@ -77,3 +77,46 @@ extension TestContext.Testing { self.init(test: Test(id: Test.ID(rawValue: id))) } } + +extension TestContext { + /// Indicates whether or not test failures should be emitted by default when an issue is reported. + public static var emitsFailureOnReportIssue: Bool { + get { _emitsFailureOnReportIssue.withLock { $0 } } + set { _emitsFailureOnReportIssue.withLock { $0 = newValue } } + } + + @TaskLocal fileprivate static var _emitsFailureOnReportIssue = LockIsolated(true) +} + +public func withEmitsFailureOnReportIssue( + _ value: Bool, + operation: () throws -> R +) rethrows -> R { + try TestContext.$_emitsFailureOnReportIssue.withValue(LockIsolated(value), operation: operation) +} + +#if compiler(>=6) + /// Overrides the task's issue reporters for the duration of the asynchronous operation. + /// + /// An asynchronous version of ``withIssueReporters(_:operation:)-91179``. + /// + /// - Parameters: + /// - reporters: Issue reporters to notify during the operation. + /// - isolation: The isolation associated with the operation. + /// - operation: An asynchronous operation. + public func withEmitsFailureOnReportIssue( + _ value: Bool, + isolation: isolated (any Actor)? = #isolation, + operation: () async throws -> R + ) async rethrows -> R { + try await TestContext.$_emitsFailureOnReportIssue.withValue(LockIsolated(value), operation: operation) + } +#else + @_unsafeInheritExecutor + public func withIssueReporters( + _ value: Bool, + operation: () async throws -> R + ) async rethrows -> R { + try await TestContext.$_emitsFailureOnReportIssue.withValue(LockIsolated(value), operation: operation) + } +#endif diff --git a/Tests/IssueReportingTests/SwiftTestingTests.swift b/Tests/IssueReportingTests/SwiftTestingTests.swift index fef8993..f76d36c 100644 --- a/Tests/IssueReportingTests/SwiftTestingTests.swift +++ b/Tests/IssueReportingTests/SwiftTestingTests.swift @@ -45,6 +45,12 @@ issue.description == "Caught error: Failure(): Something went wrong" } } + + @Test func reportIssue_EmitFailureDisabled() { + withEmitsFailureOnReportIssue(false) { + reportIssue() + } + } @Test func withExpectedIssue_reportIssue() { withExpectedIssue { diff --git a/Tests/IssueReportingTests/XCTestTests.swift b/Tests/IssueReportingTests/XCTestTests.swift index d5219a8..0cb9784 100644 --- a/Tests/IssueReportingTests/XCTestTests.swift +++ b/Tests/IssueReportingTests/XCTestTests.swift @@ -33,6 +33,12 @@ final class XCTestTests: XCTestCase { $0.compactDescription == "failed - Something went wrong" } } + + func testReportIssue_EmitFailureDisabled() { + withEmitsFailureOnReportIssue(false) { + reportIssue() + } + } func testWithExpectedIssue() { withExpectedIssue { From b91271daa95acd65840fb1023d7916e549209866 Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Thu, 6 Feb 2025 14:38:49 +0000 Subject: [PATCH 2/3] wip --- Sources/IssueReporting/TestContext.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/IssueReporting/TestContext.swift b/Sources/IssueReporting/TestContext.swift index e00c561..4bc79a2 100644 --- a/Sources/IssueReporting/TestContext.swift +++ b/Sources/IssueReporting/TestContext.swift @@ -113,7 +113,7 @@ public func withEmitsFailureOnReportIssue( } #else @_unsafeInheritExecutor - public func withIssueReporters( + public func withEmitsFailureOnReportIssue( _ value: Bool, operation: () async throws -> R ) async rethrows -> R { From 703b555e736cf2d6539988fccb12d1a2b85e4ec9 Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Thu, 6 Feb 2025 14:39:54 +0000 Subject: [PATCH 3/3] wip --- Sources/IssueReporting/TestContext.swift | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Sources/IssueReporting/TestContext.swift b/Sources/IssueReporting/TestContext.swift index 4bc79a2..76a2dfd 100644 --- a/Sources/IssueReporting/TestContext.swift +++ b/Sources/IssueReporting/TestContext.swift @@ -96,14 +96,6 @@ public func withEmitsFailureOnReportIssue( } #if compiler(>=6) - /// Overrides the task's issue reporters for the duration of the asynchronous operation. - /// - /// An asynchronous version of ``withIssueReporters(_:operation:)-91179``. - /// - /// - Parameters: - /// - reporters: Issue reporters to notify during the operation. - /// - isolation: The isolation associated with the operation. - /// - operation: An asynchronous operation. public func withEmitsFailureOnReportIssue( _ value: Bool, isolation: isolated (any Actor)? = #isolation,