diff --git a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift index bc78be83e..9249b27fe 100644 --- a/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift +++ b/Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift @@ -23,14 +23,16 @@ import Foundation /// - expected: An optional closure that returns a previously generated snapshot. When omitted, /// the library will automatically write a snapshot into your test file at the call sight of /// the assertion. - /// - file: The file where the assertion occurs. The default is the filename of the test case - /// where you call this function. + /// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in + /// which this function was called. + /// - file: The file in which failure occurred. Defaults to the file path of the test case in + /// which this function was called. /// - function: The function where the assertion occurs. The default is the name of the test /// method where you call this function. - /// - line: The line where the assertion occurs. The default is the line number where you call - /// this function. - /// - column: The column where the assertion occurs. The default is the line column you call - /// this function. + /// - line: The line number on which failure occurred. Defaults to the line number on which this + /// function was called. + /// - column: The column on which failure occurred. Defaults to the column on which this + /// function was called. public func assertInlineSnapshot( of value: @autoclosure () throws -> Value?, as snapshotting: Snapshotting, @@ -39,7 +41,8 @@ import Foundation timeout: TimeInterval = 5, syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(), matches expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -70,15 +73,29 @@ import Foundation loaded. If a timeout is unavoidable, consider setting the "timeout" parameter of "assertInlineSnapshot" to a higher value. """, - file: file, - line: line + fileID: fileID, + filePath: filePath, + line: line, + column: column ) return case .incorrectOrder, .interrupted, .invertedFulfillment: - recordIssue("Couldn't snapshot value", file: file, line: line) + recordIssue( + "Couldn't snapshot value", + fileID: fileID, + filePath: filePath, + line: line, + column: column + ) return @unknown default: - recordIssue("Couldn't snapshot value", file: file, line: line) + recordIssue( + "Couldn't snapshot value", + fileID: fileID, + filePath: filePath, + line: line, + column: column + ) return } } @@ -88,7 +105,7 @@ import Foundation record != .missing || expected != nil else { // NB: Write snapshot state before calling `XCTFail` in case `continueAfterFailure = false` - inlineSnapshotState[File(path: file), default: []].append( + inlineSnapshotState[File(path: filePath), default: []].append( InlineSnapshot( expected: expected, actual: actual, @@ -119,8 +136,10 @@ import Foundation Re-run "\(function)" to assert against the newly-recorded snapshot. """, - file: file, - line: line + fileID: fileID, + filePath: filePath, + line: line, + column: column ) return } @@ -131,8 +150,10 @@ import Foundation """ No expected value to assert against. """, - file: file, - line: line + fileID: fileID, + filePath: filePath, + line: line, + column: column ) return } @@ -147,25 +168,34 @@ import Foundation \(difference.indenting(by: 2)) """, - file: file, + fileID: fileID, + file: filePath, line: line, column: column ) } catch { - recordIssue("Threw error: \(error)", file: file, line: line) + recordIssue( + "Threw error: \(error)", + fileID: fileID, + filePath: filePath, + line: line, + column: column + ) } } } #else @available(*, unavailable, message: "'assertInlineSnapshot' requires 'swift-syntax' >= 509.0.0") public func assertInlineSnapshot( - of value: @autoclosure () throws -> Value, + of value: @autoclosure () throws -> Value?, as snapshotting: Snapshotting, message: @autoclosure () -> String = "", + record isRecording: Bool? = nil, timeout: TimeInterval = 5, syntaxDescriptor: InlineSnapshotSyntaxDescriptor = InlineSnapshotSyntaxDescriptor(), matches expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -242,20 +272,23 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable { /// /// - Parameters: /// - message: An optional description of the assertion, for inclusion in test results. - /// - file: The file where the assertion occurs. The default is the filename of the test case - /// where you call `assertInlineSnapshot`. - /// - line: The line where the assertion occurs. The default is the line number where you call - /// `assertInlineSnapshot`. - /// - column: The column where the assertion occurs. The default is the column where you call - /// `assertInlineSnapshot`. + /// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case + /// in which this function was called. + /// - file: The file in which failure occurred. Defaults to the file path of the test case in + /// which this function was called. + /// - line: The line number on which failure occurred. Defaults to the line number on which + /// this function was called. + /// - column: The column on which failure occurred. Defaults to the column on which this + /// function was called. public func fail( _ message: @autoclosure () -> String = "", - file: StaticString, + fileID: StaticString, + file filePath: StaticString, line: UInt, column: UInt ) { var trailingClosureLine: Int? - if let testSource = try? testSource(file: File(path: file)) { + if let testSource = try? testSource(file: File(path: filePath)) { let visitor = SnapshotVisitor( functionCallLine: Int(line), functionCallColumn: Int(column), @@ -267,8 +300,10 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable { } recordIssue( message(), - file: file, - line: trailingClosureLine.map(UInt.init) ?? line + fileID: fileID, + filePath: filePath, + line: trailingClosureLine.map(UInt.init) ?? line, + column: column ) } @@ -279,7 +314,8 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable { @available(*, unavailable, message: "'assertInlineSnapshot' requires 'swift-syntax' >= 509.0.0") public func fail( _ message: @autoclosure () -> String = "", - file: StaticString, + fileID: StaticString, + file filePath: StaticString, line: UInt, column: UInt ) { diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift index 737f607bb..b2d473d38 100644 --- a/Sources/SnapshotTesting/AssertSnapshot.swift +++ b/Sources/SnapshotTesting/AssertSnapshot.swift @@ -45,21 +45,27 @@ public var _record: SnapshotTestingConfiguration.Record = { /// - name: An optional description of the snapshot. /// - recording: Whether or not to record a new reference. /// - timeout: The amount of time a snapshot must be generated in. -/// - file: The file in which failure occurred. Defaults to the file name of the test case in +/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in +/// which this function was called. +/// - file: The file in which failure occurred. Defaults to the file path of the test case in /// which this function was called. /// - testName: The name of the test in which failure occurred. Defaults to the function name of /// the test case in which this function was called. /// - line: The line number on which failure occurred. Defaults to the line number on which this /// function was called. +/// - column: The column on which failure occurred. Defaults to the column on which this function +/// was called. public func assertSnapshot( of value: @autoclosure () throws -> Value, as snapshotting: Snapshotting, named name: String? = nil, record recording: Bool? = nil, timeout: TimeInterval = 5, - file: StaticString = #file, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, testName: String = #function, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) { let failure = verifySnapshot( of: try value(), @@ -67,12 +73,20 @@ public func assertSnapshot( named: name, record: recording, timeout: timeout, - file: file, + fileID: fileID, + file: filePath, testName: testName, - line: line + line: line, + column: column ) guard let message = failure else { return } - recordIssue(message, file: file, line: line) + recordIssue( + message, + fileID: fileID, + filePath: filePath, + line: line, + column: column + ) } /// Asserts that a given value matches references on disk. @@ -83,20 +97,26 @@ public func assertSnapshot( /// comparing values. /// - recording: Whether or not to record a new reference. /// - timeout: The amount of time a snapshot must be generated in. -/// - file: The file in which failure occurred. Defaults to the file name of the test case in +/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in +/// which this function was called. +/// - file: The file in which failure occurred. Defaults to the file path of the test case in /// which this function was called. /// - testName: The name of the test in which failure occurred. Defaults to the function name of /// the test case in which this function was called. /// - line: The line number on which failure occurred. Defaults to the line number on which this /// function was called. +/// - column: The column on which failure occurred. Defaults to the column on which this function +/// was called. public func assertSnapshots( of value: @autoclosure () throws -> Value, as strategies: [String: Snapshotting], record recording: Bool? = nil, timeout: TimeInterval = 5, - file: StaticString = #file, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, testName: String = #function, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) { try? strategies.forEach { name, strategy in assertSnapshot( @@ -105,9 +125,11 @@ public func assertSnapshots( named: name, record: recording, timeout: timeout, - file: file, + fileID: fileID, + file: filePath, testName: testName, - line: line + line: line, + column: column ) } } @@ -119,20 +141,26 @@ public func assertSnapshots( /// - strategies: An array of strategies for serializing, deserializing, and comparing values. /// - recording: Whether or not to record a new reference. /// - timeout: The amount of time a snapshot must be generated in. -/// - file: The file in which failure occurred. Defaults to the file name of the test case in +/// - fileID: The file ID in which failure occurred. Defaults to the file ID of the test case in +/// which this function was called. +/// - file: The file in which failure occurred. Defaults to the file path of the test case in /// which this function was called. /// - testName: The name of the test in which failure occurred. Defaults to the function name of /// the test case in which this function was called. /// - line: The line number on which failure occurred. Defaults to the line number on which this /// function was called. +/// - column: The column on which failure occurred. Defaults to the column on which this function +/// was called. public func assertSnapshots( of value: @autoclosure () throws -> Value, as strategies: [Snapshotting], record recording: Bool? = nil, timeout: TimeInterval = 5, - file: StaticString = #file, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, testName: String = #function, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) { try? strategies.forEach { strategy in assertSnapshot( @@ -140,9 +168,11 @@ public func assertSnapshots( as: strategy, record: recording, timeout: timeout, - file: file, + fileID: fileID, + file: filePath, testName: testName, - line: line + line: line, + column: column ) } } @@ -205,9 +235,11 @@ public func verifySnapshot( record recording: Bool? = nil, snapshotDirectory: String? = nil, timeout: TimeInterval = 5, - file: StaticString = #file, + fileID: StaticString = #fileID, + file filePath: StaticString = #file, testName: String = #function, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) -> String? { CleanCounterBetweenTestCases.registerIfNeeded() @@ -217,7 +249,7 @@ public func verifySnapshot( ?? _record return withSnapshotTesting(record: record) { () -> String? in do { - let fileUrl = URL(fileURLWithPath: "\(file)", isDirectory: false) + let fileUrl = URL(fileURLWithPath: "\(filePath)", isDirectory: false) let fileName = fileUrl.deletingPathExtension().lastPathComponent let snapshotDirectoryUrl = diff --git a/Sources/SnapshotTesting/Internal/Deprecations.swift b/Sources/SnapshotTesting/Internal/Deprecations.swift index debb08dac..ce93f2f7f 100644 --- a/Sources/SnapshotTesting/Internal/Deprecations.swift +++ b/Sources/SnapshotTesting/Internal/Deprecations.swift @@ -14,9 +14,11 @@ public func _assertInlineSnapshot( record recording: Bool = false, timeout: TimeInterval = 5, with reference: String, - file: StaticString = #file, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, testName: String = #function, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) { let failure = _verifyInlineSnapshot( @@ -25,12 +27,14 @@ public func _assertInlineSnapshot( record: recording, timeout: timeout, with: reference, - file: file, + fileID: fileID, + file: filePath, testName: testName, - line: line + line: line, + column: column ) guard let message = failure else { return } - recordIssue(message, file: file, line: line) + recordIssue(message, fileID: fileID, filePath: filePath, line: line, column: column) } @available( @@ -44,9 +48,11 @@ public func _verifyInlineSnapshot( record recording: Bool = false, timeout: TimeInterval = 5, with reference: String, - file: StaticString = #file, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, testName: String = #function, - line: UInt = #line + line: UInt = #line, + column: UInt = #column ) -> String? { @@ -94,7 +100,7 @@ public func _verifyInlineSnapshot( // If that diff failed, we either record or fail. if recording || trimmedReference.isEmpty { - let fileName = "\(file)" + let fileName = "\(filePath)" let sourceCodeFilePath = URL(fileURLWithPath: fileName, isDirectory: false) let sourceCode = try String(contentsOf: sourceCodeFilePath) var newRecordings = recordings diff --git a/Sources/SnapshotTesting/Internal/RecordIssue.swift b/Sources/SnapshotTesting/Internal/RecordIssue.swift index 76f0a4c86..01c700df8 100644 --- a/Sources/SnapshotTesting/Internal/RecordIssue.swift +++ b/Sources/SnapshotTesting/Internal/RecordIssue.swift @@ -7,20 +7,26 @@ import XCTest @_spi(Internals) public func recordIssue( _ message: @autoclosure () -> String, - file: StaticString = #filePath, - line: UInt = #line + fileID: StaticString, + filePath: StaticString, + line: UInt, + column: UInt ) { #if canImport(Testing) if Test.current != nil { Issue.record( Comment(rawValue: message()), - filePath: file.description, - line: Int(line) + sourceLocation: SourceLocation( + fileID: fileID.description, + filePath: filePath.description, + line: Int(line), + column: Int(column) + ) ) } else { - XCTFail(message(), file: file, line: line) + XCTFail(message(), file: filePath, line: line) } #else - XCTFail(message(), file: file, line: line) + XCTFail(message(), file: filePath, line: line) #endif } diff --git a/Tests/InlineSnapshotTestingTests/AssertInlineSnapshotSwiftTests.swift b/Tests/InlineSnapshotTestingTests/AssertInlineSnapshotSwiftTests.swift index 448925cd5..0a193452c 100644 --- a/Tests/InlineSnapshotTestingTests/AssertInlineSnapshotSwiftTests.swift +++ b/Tests/InlineSnapshotTestingTests/AssertInlineSnapshotSwiftTests.swift @@ -102,7 +102,8 @@ @Test func argumentlessInlineSnapshot() { func assertArgumentlessInlineSnapshot( expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -115,7 +116,8 @@ trailingClosureOffset: 1 ), matches: expected, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -135,7 +137,8 @@ of url: () -> String, head: (() -> String)? = nil, body: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -152,7 +155,8 @@ trailingClosureOffset: 1 ), matches: head, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -178,7 +182,8 @@ trailingClosureOffset: 2 ), matches: body, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -215,7 +220,8 @@ func assertAsyncThrowingInlineSnapshot( of value: () -> String, is expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -228,7 +234,8 @@ trailingClosureOffset: 1 ), matches: expected, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -284,7 +291,8 @@ private func assertCustomInlineSnapshot( of value: () -> String, is expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -297,7 +305,8 @@ trailingClosureOffset: 1 ), matches: expected, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column diff --git a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift index 70bace93c..42198acbf 100644 --- a/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift +++ b/Tests/InlineSnapshotTestingTests/InlineSnapshotTestingTests.swift @@ -102,7 +102,8 @@ final class InlineSnapshotTestingTests: XCTestCase { func testArgumentlessInlineSnapshot() { func assertArgumentlessInlineSnapshot( expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -115,7 +116,8 @@ final class InlineSnapshotTestingTests: XCTestCase { trailingClosureOffset: 1 ), matches: expected, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -135,7 +137,8 @@ final class InlineSnapshotTestingTests: XCTestCase { of url: () -> String, head: (() -> String)? = nil, body: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -152,7 +155,8 @@ final class InlineSnapshotTestingTests: XCTestCase { trailingClosureOffset: 1 ), matches: head, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -178,7 +182,8 @@ final class InlineSnapshotTestingTests: XCTestCase { trailingClosureOffset: 2 ), matches: body, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -215,7 +220,8 @@ final class InlineSnapshotTestingTests: XCTestCase { func assertAsyncThrowingInlineSnapshot( of value: () -> String, is expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -228,7 +234,8 @@ final class InlineSnapshotTestingTests: XCTestCase { trailingClosureOffset: 1 ), matches: expected, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column @@ -284,7 +291,8 @@ final class InlineSnapshotTestingTests: XCTestCase { private func assertCustomInlineSnapshot( of value: () -> String, is expected: (() -> String)? = nil, - file: StaticString = #filePath, + fileID: StaticString = #fileID, + file filePath: StaticString = #filePath, function: StaticString = #function, line: UInt = #line, column: UInt = #column @@ -297,7 +305,8 @@ private func assertCustomInlineSnapshot( trailingClosureOffset: 1 ), matches: expected, - file: file, + fileID: fileID, + file: filePath, function: function, line: line, column: column