Skip to content

Issue context clean up #158

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 3 commits into from
May 2, 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
68 changes: 40 additions & 28 deletions Sources/IssueReporting/ReportIssue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,33 @@ 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 {
observer.withLock { $0 += 1 }
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
)
}
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -109,21 +121,21 @@ 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 {
for reporter in IssueReporters.current {
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
)
}
}
Expand All @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions Tests/IssueReportingTests/SwiftTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
12 changes: 12 additions & 0 deletions Tests/IssueReportingTests/XCTestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down