Skip to content

Commit 63d3b45

Browse files
Fix recordIssue for Xcode beta 3. (#869)
* Fix recordIssue for Xcode beta 3. * wip * wip * fix * wip --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent f6c51fa commit 63d3b45

File tree

6 files changed

+180
-82
lines changed

6 files changed

+180
-82
lines changed

Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift

+67-31
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ import Foundation
2323
/// - expected: An optional closure that returns a previously generated snapshot. When omitted,
2424
/// the library will automatically write a snapshot into your test file at the call sight of
2525
/// the assertion.
26-
/// - file: The file where the assertion occurs. The default is the filename of the test case
27-
/// where you call this function.
26+
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
27+
/// which this function was called.
28+
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
29+
/// which this function was called.
2830
/// - function: The function where the assertion occurs. The default is the name of the test
2931
/// method where you call this function.
30-
/// - line: The line where the assertion occurs. The default is the line number where you call
31-
/// this function.
32-
/// - column: The column where the assertion occurs. The default is the line column you call
33-
/// this function.
32+
/// - line: The line number on which failure occurred. Defaults to the line number on which this
33+
/// function was called.
34+
/// - column: The column on which failure occurred. Defaults to the column on which this
35+
/// function was called.
3436
public func assertInlineSnapshot<Value>(
3537
of value: @autoclosure () throws -> Value?,
3638
as snapshotting: Snapshotting<Value, String>,
@@ -39,7 +41,8 @@ import Foundation
3941
timeout: TimeInterval = 5,
4042
syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(),
4143
matches expected: (() -> String)? = nil,
42-
file: StaticString = #filePath,
44+
fileID: StaticString = #fileID,
45+
file filePath: StaticString = #filePath,
4346
function: StaticString = #function,
4447
line: UInt = #line,
4548
column: UInt = #column
@@ -70,15 +73,29 @@ import Foundation
7073
loaded. If a timeout is unavoidable, consider setting the "timeout" parameter of
7174
"assertInlineSnapshot" to a higher value.
7275
""",
73-
file: file,
74-
line: line
76+
fileID: fileID,
77+
filePath: filePath,
78+
line: line,
79+
column: column
7580
)
7681
return
7782
case .incorrectOrder, .interrupted, .invertedFulfillment:
78-
recordIssue("Couldn't snapshot value", file: file, line: line)
83+
recordIssue(
84+
"Couldn't snapshot value",
85+
fileID: fileID,
86+
filePath: filePath,
87+
line: line,
88+
column: column
89+
)
7990
return
8091
@unknown default:
81-
recordIssue("Couldn't snapshot value", file: file, line: line)
92+
recordIssue(
93+
"Couldn't snapshot value",
94+
fileID: fileID,
95+
filePath: filePath,
96+
line: line,
97+
column: column
98+
)
8299
return
83100
}
84101
}
@@ -88,7 +105,7 @@ import Foundation
88105
record != .missing || expected != nil
89106
else {
90107
// NB: Write snapshot state before calling `XCTFail` in case `continueAfterFailure = false`
91-
inlineSnapshotState[File(path: file), default: []].append(
108+
inlineSnapshotState[File(path: filePath), default: []].append(
92109
InlineSnapshot(
93110
expected: expected,
94111
actual: actual,
@@ -119,8 +136,10 @@ import Foundation
119136
120137
Re-run "\(function)" to assert against the newly-recorded snapshot.
121138
""",
122-
file: file,
123-
line: line
139+
fileID: fileID,
140+
filePath: filePath,
141+
line: line,
142+
column: column
124143
)
125144
return
126145
}
@@ -131,8 +150,10 @@ import Foundation
131150
"""
132151
No expected value to assert against.
133152
""",
134-
file: file,
135-
line: line
153+
fileID: fileID,
154+
filePath: filePath,
155+
line: line,
156+
column: column
136157
)
137158
return
138159
}
@@ -147,25 +168,34 @@ import Foundation
147168
148169
\(difference.indenting(by: 2))
149170
""",
150-
file: file,
171+
fileID: fileID,
172+
file: filePath,
151173
line: line,
152174
column: column
153175
)
154176
} catch {
155-
recordIssue("Threw error: \(error)", file: file, line: line)
177+
recordIssue(
178+
"Threw error: \(error)",
179+
fileID: fileID,
180+
filePath: filePath,
181+
line: line,
182+
column: column
183+
)
156184
}
157185
}
158186
}
159187
#else
160188
@available(*, unavailable, message: "'assertInlineSnapshot' requires 'swift-syntax' >= 509.0.0")
161189
public func assertInlineSnapshot<Value>(
162-
of value: @autoclosure () throws -> Value,
190+
of value: @autoclosure () throws -> Value?,
163191
as snapshotting: Snapshotting<Value, String>,
164192
message: @autoclosure () -> String = "",
193+
record isRecording: Bool? = nil,
165194
timeout: TimeInterval = 5,
166195
syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(),
167196
matches expected: (() -> String)? = nil,
168-
file: StaticString = #filePath,
197+
fileID: StaticString = #fileID,
198+
file filePath: StaticString = #filePath,
169199
function: StaticString = #function,
170200
line: UInt = #line,
171201
column: UInt = #column
@@ -242,20 +272,23 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
242272
///
243273
/// - Parameters:
244274
/// - message: An optional description of the assertion, for inclusion in test results.
245-
/// - file: The file where the assertion occurs. The default is the filename of the test case
246-
/// where you call `assertInlineSnapshot`.
247-
/// - line: The line where the assertion occurs. The default is the line number where you call
248-
/// `assertInlineSnapshot`.
249-
/// - column: The column where the assertion occurs. The default is the column where you call
250-
/// `assertInlineSnapshot`.
275+
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case
276+
/// in which this function was called.
277+
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
278+
/// which this function was called.
279+
/// - line: The line number on which failure occurred. Defaults to the line number on which
280+
/// this function was called.
281+
/// - column: The column on which failure occurred. Defaults to the column on which this
282+
/// function was called.
251283
public func fail(
252284
_ message: @autoclosure () -> String = "",
253-
file: StaticString,
285+
fileID: StaticString,
286+
file filePath: StaticString,
254287
line: UInt,
255288
column: UInt
256289
) {
257290
var trailingClosureLine: Int?
258-
if let testSource = try? testSource(file: File(path: file)) {
291+
if let testSource = try? testSource(file: File(path: filePath)) {
259292
let visitor = SnapshotVisitor(
260293
functionCallLine: Int(line),
261294
functionCallColumn: Int(column),
@@ -267,8 +300,10 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
267300
}
268301
recordIssue(
269302
message(),
270-
file: file,
271-
line: trailingClosureLine.map(UInt.init) ?? line
303+
fileID: fileID,
304+
filePath: filePath,
305+
line: trailingClosureLine.map(UInt.init) ?? line,
306+
column: column
272307
)
273308
}
274309

@@ -279,7 +314,8 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
279314
@available(*, unavailable, message: "'assertInlineSnapshot' requires 'swift-syntax' >= 509.0.0")
280315
public func fail(
281316
_ message: @autoclosure () -> String = "",
282-
file: StaticString,
317+
fileID: StaticString,
318+
file filePath: StaticString,
283319
line: UInt,
284320
column: UInt
285321
) {

Sources/SnapshotTesting/AssertSnapshot.swift

+51-19
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,48 @@ public var _record: SnapshotTestingConfiguration.Record = {
4545
/// - name: An optional description of the snapshot.
4646
/// - recording: Whether or not to record a new reference.
4747
/// - timeout: The amount of time a snapshot must be generated in.
48-
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
48+
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
49+
/// which this function was called.
50+
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
4951
/// which this function was called.
5052
/// - testName: The name of the test in which failure occurred. Defaults to the function name of
5153
/// the test case in which this function was called.
5254
/// - line: The line number on which failure occurred. Defaults to the line number on which this
5355
/// function was called.
56+
/// - column: The column on which failure occurred. Defaults to the column on which this function
57+
/// was called.
5458
public func assertSnapshot<Value, Format>(
5559
of value: @autoclosure () throws -> Value,
5660
as snapshotting: Snapshotting<Value, Format>,
5761
named name: String? = nil,
5862
record recording: Bool? = nil,
5963
timeout: TimeInterval = 5,
60-
file: StaticString = #file,
64+
fileID: StaticString = #fileID,
65+
file filePath: StaticString = #filePath,
6166
testName: String = #function,
62-
line: UInt = #line
67+
line: UInt = #line,
68+
column: UInt = #column
6369
) {
6470
let failure = verifySnapshot(
6571
of: try value(),
6672
as: snapshotting,
6773
named: name,
6874
record: recording,
6975
timeout: timeout,
70-
file: file,
76+
fileID: fileID,
77+
file: filePath,
7178
testName: testName,
72-
line: line
79+
line: line,
80+
column: column
7381
)
7482
guard let message = failure else { return }
75-
recordIssue(message, file: file, line: line)
83+
recordIssue(
84+
message,
85+
fileID: fileID,
86+
filePath: filePath,
87+
line: line,
88+
column: column
89+
)
7690
}
7791

7892
/// Asserts that a given value matches references on disk.
@@ -83,20 +97,26 @@ public func assertSnapshot<Value, Format>(
8397
/// comparing values.
8498
/// - recording: Whether or not to record a new reference.
8599
/// - timeout: The amount of time a snapshot must be generated in.
86-
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
100+
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
101+
/// which this function was called.
102+
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
87103
/// which this function was called.
88104
/// - testName: The name of the test in which failure occurred. Defaults to the function name of
89105
/// the test case in which this function was called.
90106
/// - line: The line number on which failure occurred. Defaults to the line number on which this
91107
/// function was called.
108+
/// - column: The column on which failure occurred. Defaults to the column on which this function
109+
/// was called.
92110
public func assertSnapshots<Value, Format>(
93111
of value: @autoclosure () throws -> Value,
94112
as strategies: [String: Snapshotting<Value, Format>],
95113
record recording: Bool? = nil,
96114
timeout: TimeInterval = 5,
97-
file: StaticString = #file,
115+
fileID: StaticString = #fileID,
116+
file filePath: StaticString = #filePath,
98117
testName: String = #function,
99-
line: UInt = #line
118+
line: UInt = #line,
119+
column: UInt = #column
100120
) {
101121
try? strategies.forEach { name, strategy in
102122
assertSnapshot(
@@ -105,9 +125,11 @@ public func assertSnapshots<Value, Format>(
105125
named: name,
106126
record: recording,
107127
timeout: timeout,
108-
file: file,
128+
fileID: fileID,
129+
file: filePath,
109130
testName: testName,
110-
line: line
131+
line: line,
132+
column: column
111133
)
112134
}
113135
}
@@ -119,30 +141,38 @@ public func assertSnapshots<Value, Format>(
119141
/// - strategies: An array of strategies for serializing, deserializing, and comparing values.
120142
/// - recording: Whether or not to record a new reference.
121143
/// - timeout: The amount of time a snapshot must be generated in.
122-
/// - file: The file in which failure occurred. Defaults to the file name of the test case in
144+
/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in
145+
/// which this function was called.
146+
/// - file: The file in which failure occurred. Defaults to the file path of the test case in
123147
/// which this function was called.
124148
/// - testName: The name of the test in which failure occurred. Defaults to the function name of
125149
/// the test case in which this function was called.
126150
/// - line: The line number on which failure occurred. Defaults to the line number on which this
127151
/// function was called.
152+
/// - column: The column on which failure occurred. Defaults to the column on which this function
153+
/// was called.
128154
public func assertSnapshots<Value, Format>(
129155
of value: @autoclosure () throws -> Value,
130156
as strategies: [Snapshotting<Value, Format>],
131157
record recording: Bool? = nil,
132158
timeout: TimeInterval = 5,
133-
file: StaticString = #file,
159+
fileID: StaticString = #fileID,
160+
file filePath: StaticString = #filePath,
134161
testName: String = #function,
135-
line: UInt = #line
162+
line: UInt = #line,
163+
column: UInt = #column
136164
) {
137165
try? strategies.forEach { strategy in
138166
assertSnapshot(
139167
of: try value(),
140168
as: strategy,
141169
record: recording,
142170
timeout: timeout,
143-
file: file,
171+
fileID: fileID,
172+
file: filePath,
144173
testName: testName,
145-
line: line
174+
line: line,
175+
column: column
146176
)
147177
}
148178
}
@@ -205,9 +235,11 @@ public func verifySnapshot<Value, Format>(
205235
record recording: Bool? = nil,
206236
snapshotDirectory: String? = nil,
207237
timeout: TimeInterval = 5,
208-
file: StaticString = #file,
238+
fileID: StaticString = #fileID,
239+
file filePath: StaticString = #file,
209240
testName: String = #function,
210-
line: UInt = #line
241+
line: UInt = #line,
242+
column: UInt = #column
211243
) -> String? {
212244
CleanCounterBetweenTestCases.registerIfNeeded()
213245

@@ -217,7 +249,7 @@ public func verifySnapshot<Value, Format>(
217249
?? _record
218250
return withSnapshotTesting(record: record) { () -> String? in
219251
do {
220-
let fileUrl = URL(fileURLWithPath: "\(file)", isDirectory: false)
252+
let fileUrl = URL(fileURLWithPath: "\(filePath)", isDirectory: false)
221253
let fileName = fileUrl.deletingPathExtension().lastPathComponent
222254

223255
let snapshotDirectoryUrl =

0 commit comments

Comments
 (0)