diff --git a/Sources/IssueReporting/ReportIssue.swift b/Sources/IssueReporting/ReportIssue.swift index 3a2af15..b7160d9 100644 --- a/Sources/IssueReporting/ReportIssue.swift +++ b/Sources/IssueReporting/ReportIssue.swift @@ -34,6 +34,12 @@ public func reportIssue( line: UInt = #line, column: UInt = #column ) { + let (fileID, filePath, line, column) = ( + IssueContext.current?.fileID ?? fileID, + IssueContext.current?.filePath ?? filePath, + IssueContext.current?.line ?? line, + IssueContext.current?.column ?? column + ) guard let context = TestContext.current else { guard !isTesting else { return } if let observer = FailureObserver.current { @@ -41,20 +47,20 @@ public func reportIssue( for reporter in IssueReporters.current { reporter.expectIssue( message(), - fileID: IssueContext.current?.fileID ?? fileID, - filePath: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line, - column: IssueContext.current?.column ?? column + fileID: fileID, + filePath: filePath, + line: line, + column: column ) } } else { for reporter in IssueReporters.current { reporter.reportIssue( message(), - fileID: IssueContext.current?.fileID ?? fileID, - filePath: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line, - column: IssueContext.current?.column ?? column + fileID: fileID, + filePath: filePath, + line: line, + column: column ) } } @@ -65,16 +71,16 @@ public func reportIssue( 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) + fileID: "\(fileID)", + filePath: "\(filePath)", + line: Int(line), + column: Int(column) ) case .xcTest: _XCTFail( message().withAppHostWarningIfNeeded() ?? "", - file: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line + file: filePath, + line: line ) @unknown default: break } @@ -101,6 +107,12 @@ public func reportIssue( line: UInt = #line, column: UInt = #column ) { + let (fileID, filePath, line, column) = ( + IssueContext.current?.fileID ?? fileID, + IssueContext.current?.filePath ?? filePath, + IssueContext.current?.line ?? line, + IssueContext.current?.column ?? column + ) guard let context = TestContext.current else { guard !isTesting else { return } if let observer = FailureObserver.current { @@ -109,10 +121,10 @@ public func reportIssue( reporter.expectIssue( error, message(), - fileID: IssueContext.current?.fileID ?? fileID, - filePath: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line, - column: IssueContext.current?.column ?? column + fileID: fileID, + filePath: filePath, + line: line, + column: column ) } } else { @@ -120,10 +132,10 @@ public func reportIssue( reporter.reportIssue( error, message(), - fileID: IssueContext.current?.fileID ?? fileID, - filePath: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line, - column: IssueContext.current?.column ?? column + fileID: fileID, + filePath: filePath, + line: line, + column: column ) } } @@ -135,16 +147,16 @@ public func reportIssue( _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) + fileID: "\(fileID)", + filePath: "\(filePath)", + line: Int(line), + column: Int(column) ) case .xcTest: _XCTFail( "Caught error: \(error)\(message().map { ": \($0)" } ?? "")".withAppHostWarningIfNeeded(), - file: IssueContext.current?.filePath ?? filePath, - line: IssueContext.current?.line ?? line + file: filePath, + line: line ) @unknown default: break } diff --git a/Tests/IssueReportingTests/SwiftTestingTests.swift b/Tests/IssueReportingTests/SwiftTestingTests.swift index fef8993..3969ac6 100644 --- a/Tests/IssueReportingTests/SwiftTestingTests.swift +++ b/Tests/IssueReportingTests/SwiftTestingTests.swift @@ -117,6 +117,18 @@ await Task.yield() } } + + @Test func overrideIssueContext() { + withKnownIssue { + withIssueContext(fileID: #fileID, filePath: #filePath, line: #line, column: #column) { + reportIssue("Something went wrong") + } + } matching: { issue in + let expectedReportingLine = #line - 4 + return issue.sourceLocation?.line == expectedReportingLine + && issue.description == "Issue recorded: Something went wrong" + } + } } private struct Failure: Error {} diff --git a/Tests/IssueReportingTests/XCTestTests.swift b/Tests/IssueReportingTests/XCTestTests.swift index d5219a8..22d55c3 100644 --- a/Tests/IssueReportingTests/XCTestTests.swift +++ b/Tests/IssueReportingTests/XCTestTests.swift @@ -49,6 +49,18 @@ final class XCTestTests: XCTestCase { func testWithExpectedIssue_Throwing() { withExpectedIssue { throw Failure() } } + + func testOverrideIssueContext() { + XCTExpectFailure { + withIssueContext(fileID: #fileID, filePath: #filePath, line: #line, column: #column) { + reportIssue("Something went wrong") + } + } issueMatcher: { issue in + let expectedReportingLine = #line - 4 + return issue.sourceCodeContext.location?.lineNumber == expectedReportingLine + && issue.compactDescription == "failed - Something went wrong" + } + } #endif }