Skip to content

Commit c90c0f9

Browse files
authored
Issue context clean up (#158)
* Allow overriding file/line/column of where issues are reported. * wip * wip
1 parent f21b6b5 commit c90c0f9

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

Sources/IssueReporting/ReportIssue.swift

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,33 @@ public func reportIssue(
3434
line: UInt = #line,
3535
column: UInt = #column
3636
) {
37+
let (fileID, filePath, line, column) = (
38+
IssueContext.current?.fileID ?? fileID,
39+
IssueContext.current?.filePath ?? filePath,
40+
IssueContext.current?.line ?? line,
41+
IssueContext.current?.column ?? column
42+
)
3743
guard let context = TestContext.current else {
3844
guard !isTesting else { return }
3945
if let observer = FailureObserver.current {
4046
observer.withLock { $0 += 1 }
4147
for reporter in IssueReporters.current {
4248
reporter.expectIssue(
4349
message(),
44-
fileID: IssueContext.current?.fileID ?? fileID,
45-
filePath: IssueContext.current?.filePath ?? filePath,
46-
line: IssueContext.current?.line ?? line,
47-
column: IssueContext.current?.column ?? column
50+
fileID: fileID,
51+
filePath: filePath,
52+
line: line,
53+
column: column
4854
)
4955
}
5056
} else {
5157
for reporter in IssueReporters.current {
5258
reporter.reportIssue(
5359
message(),
54-
fileID: IssueContext.current?.fileID ?? fileID,
55-
filePath: IssueContext.current?.filePath ?? filePath,
56-
line: IssueContext.current?.line ?? line,
57-
column: IssueContext.current?.column ?? column
60+
fileID: fileID,
61+
filePath: filePath,
62+
line: line,
63+
column: column
5864
)
5965
}
6066
}
@@ -65,16 +71,16 @@ public func reportIssue(
6571
case .swiftTesting:
6672
_recordIssue(
6773
message: message(),
68-
fileID: "\(IssueContext.current?.fileID ?? fileID)",
69-
filePath: "\(IssueContext.current?.filePath ?? filePath)",
70-
line: Int(IssueContext.current?.line ?? line),
71-
column: Int(IssueContext.current?.column ?? column)
74+
fileID: "\(fileID)",
75+
filePath: "\(filePath)",
76+
line: Int(line),
77+
column: Int(column)
7278
)
7379
case .xcTest:
7480
_XCTFail(
7581
message().withAppHostWarningIfNeeded() ?? "",
76-
file: IssueContext.current?.filePath ?? filePath,
77-
line: IssueContext.current?.line ?? line
82+
file: filePath,
83+
line: line
7884
)
7985
@unknown default: break
8086
}
@@ -101,6 +107,12 @@ public func reportIssue(
101107
line: UInt = #line,
102108
column: UInt = #column
103109
) {
110+
let (fileID, filePath, line, column) = (
111+
IssueContext.current?.fileID ?? fileID,
112+
IssueContext.current?.filePath ?? filePath,
113+
IssueContext.current?.line ?? line,
114+
IssueContext.current?.column ?? column
115+
)
104116
guard let context = TestContext.current else {
105117
guard !isTesting else { return }
106118
if let observer = FailureObserver.current {
@@ -109,21 +121,21 @@ public func reportIssue(
109121
reporter.expectIssue(
110122
error,
111123
message(),
112-
fileID: IssueContext.current?.fileID ?? fileID,
113-
filePath: IssueContext.current?.filePath ?? filePath,
114-
line: IssueContext.current?.line ?? line,
115-
column: IssueContext.current?.column ?? column
124+
fileID: fileID,
125+
filePath: filePath,
126+
line: line,
127+
column: column
116128
)
117129
}
118130
} else {
119131
for reporter in IssueReporters.current {
120132
reporter.reportIssue(
121133
error,
122134
message(),
123-
fileID: IssueContext.current?.fileID ?? fileID,
124-
filePath: IssueContext.current?.filePath ?? filePath,
125-
line: IssueContext.current?.line ?? line,
126-
column: IssueContext.current?.column ?? column
135+
fileID: fileID,
136+
filePath: filePath,
137+
line: line,
138+
column: column
127139
)
128140
}
129141
}
@@ -135,16 +147,16 @@ public func reportIssue(
135147
_recordError(
136148
error: error,
137149
message: message(),
138-
fileID: "\(IssueContext.current?.fileID ?? fileID)",
139-
filePath: "\(IssueContext.current?.filePath ?? filePath)",
140-
line: Int(IssueContext.current?.line ?? line),
141-
column: Int(IssueContext.current?.column ?? column)
150+
fileID: "\(fileID)",
151+
filePath: "\(filePath)",
152+
line: Int(line),
153+
column: Int(column)
142154
)
143155
case .xcTest:
144156
_XCTFail(
145157
"Caught error: \(error)\(message().map { ": \($0)" } ?? "")".withAppHostWarningIfNeeded(),
146-
file: IssueContext.current?.filePath ?? filePath,
147-
line: IssueContext.current?.line ?? line
158+
file: filePath,
159+
line: line
148160
)
149161
@unknown default: break
150162
}

Tests/IssueReportingTests/SwiftTestingTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@
117117
await Task.yield()
118118
}
119119
}
120+
121+
@Test func overrideIssueContext() {
122+
withKnownIssue {
123+
withIssueContext(fileID: #fileID, filePath: #filePath, line: #line, column: #column) {
124+
reportIssue("Something went wrong")
125+
}
126+
} matching: { issue in
127+
let expectedReportingLine = #line - 4
128+
return issue.sourceLocation?.line == expectedReportingLine
129+
&& issue.description == "Issue recorded: Something went wrong"
130+
}
131+
}
120132
}
121133

122134
private struct Failure: Error {}

Tests/IssueReportingTests/XCTestTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ final class XCTestTests: XCTestCase {
4949
func testWithExpectedIssue_Throwing() {
5050
withExpectedIssue { throw Failure() }
5151
}
52+
53+
func testOverrideIssueContext() {
54+
XCTExpectFailure {
55+
withIssueContext(fileID: #fileID, filePath: #filePath, line: #line, column: #column) {
56+
reportIssue("Something went wrong")
57+
}
58+
} issueMatcher: { issue in
59+
let expectedReportingLine = #line - 4
60+
return issue.sourceCodeContext.location?.lineNumber == expectedReportingLine
61+
&& issue.compactDescription == "failed - Something went wrong"
62+
}
63+
}
5264
#endif
5365
}
5466

0 commit comments

Comments
 (0)