Skip to content

Commit 296460a

Browse files
authored
Show previous diff message when recording (#147)
* Show previous diff message when recording * Update AssertSnapshot.swift * Update SnapshotTestCase, too * Fix inconsistencies
1 parent 38c2d6f commit 296460a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Sources/SnapshotTesting/AssertSnapshot.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,15 @@ public func verifySnapshot<Value, Format>(
119119
}
120120

121121
guard !recording, fileManager.fileExists(atPath: snapshotFileUrl.path) else {
122+
let diffMessage = (try? Data(contentsOf: snapshotFileUrl))
123+
.flatMap { data in snapshotting.diffing.diff(snapshotting.diffing.fromData(data), diffable) }
124+
.map { diff, _ in diff.trimmingCharacters(in: .whitespacesAndNewlines) }
125+
?? "Recorded snapshot: …"
126+
122127
try snapshotting.diffing.toData(diffable).write(to: snapshotFileUrl)
123128
return recording
124129
? """
125-
Record mode is on. Recorded snapshot: …
130+
Record mode is on. \(diffMessage)
126131
127132
open "\(snapshotFileUrl.path)"
128133

Sources/SnapshotTesting/SnapshotTestCase.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,21 @@ open class SnapshotTestCase: XCTestCase {
7070
self.wait(for: [tookSnapshot], timeout: timeout)
7171
#endif
7272

73-
guard let diffing = optionalDiffable else {
73+
guard let diffable = optionalDiffable else {
7474
XCTFail("Couldn't snapshot value", file: file, line: line)
7575
return
7676
}
7777

7878
guard !recording, fileManager.fileExists(atPath: snapshotFileUrl.path) else {
79-
try snapshotting.diffing.toData(diffing).write(to: snapshotFileUrl)
79+
let diffMessage = (try? Data(contentsOf: snapshotFileUrl))
80+
.flatMap { data in snapshotting.diffing.diff(snapshotting.diffing.fromData(data), diffable) }
81+
.map { diff, _ in diff.trimmingCharacters(in: .whitespacesAndNewlines) }
82+
?? "Recorded snapshot: …"
83+
84+
try snapshotting.diffing.toData(diffable).write(to: snapshotFileUrl)
8085
let message = recording
8186
? """
82-
Record mode is on. Recorded snapshot: …
87+
Record mode is on. \(diffMessage)
8388
8489
open "\(snapshotFileUrl.path)"
8590
@@ -100,7 +105,7 @@ open class SnapshotTestCase: XCTestCase {
100105
let data = try Data(contentsOf: snapshotFileUrl)
101106
let reference = snapshotting.diffing.fromData(data)
102107

103-
guard let (failure, attachments) = snapshotting.diffing.diff(reference, diffing) else {
108+
guard let (failure, attachments) = snapshotting.diffing.diff(reference, diffable) else {
104109
return
105110
}
106111

@@ -110,7 +115,7 @@ open class SnapshotTestCase: XCTestCase {
110115
let artifactsSubUrl = artifactsUrl.appendingPathComponent(fileName)
111116
try fileManager.createDirectory(at: artifactsSubUrl, withIntermediateDirectories: true)
112117
let failedSnapshotFileUrl = artifactsSubUrl.appendingPathComponent(snapshotFileUrl.lastPathComponent)
113-
try snapshotting.diffing.toData(diffing).write(to: failedSnapshotFileUrl)
118+
try snapshotting.diffing.toData(diffable).write(to: failedSnapshotFileUrl)
114119

115120
if !attachments.isEmpty {
116121
#if !os(Linux)

0 commit comments

Comments
 (0)