Skip to content

Commit 635114c

Browse files
authored
Add diffToolBuilder for building complex diff command messages (pointfreeco#840)
* Add SnapshotTesting.diffToolBuilder for building complex diff command messages It takes the existing and the failure snapshot as arguments, and thus complex commands which are not just prefixed with the existing diffTool can be created. * Rename diffToolBuilder to diffToolCommand, replace diffTool with custom getter/setter
1 parent f2fe5df commit 635114c

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ targets: [
210210
- **`Codable` support**. Snapshot encodable data structures into their JSON and property list
211211
representations.
212212
- **Custom diff tool integration**. Configure failure messages to print diff commands for
213-
[Kaleidoscope](https://kaleidoscope.app) (or your diff tool of choice).
213+
[Kaleidoscope](https://kaleidoscope.app) or your diff tool of choice.
214214
``` swift
215-
SnapshotTesting.diffTool = "ksdiff"
215+
SnapshotTesting.diffToolCommand = { "ksdiff \($0) \($1)" }
216216
```
217217

218218
[available-strategies]: https://swiftpackageindex.com/pointfreeco/swift-snapshot-testing/main/documentation/snapshottesting/snapshotting

Sources/SnapshotTesting/AssertSnapshot.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import XCTest
22

33
/// Enhances failure messages with a command line diff tool expression that can be copied and pasted
4-
/// into a terminal.
4+
/// into a terminal. For more complex difftool needs, see `diffToolCommand`.
55
///
66
/// ```swift
77
/// diffTool = "ksdiff"
88
/// ```
9-
public var diffTool: String? = nil
9+
public var diffTool: String? {
10+
get { diffToolCommand?("", "").trimmingCharacters(in: .whitespaces) }
11+
set {
12+
diffToolCommand = newValue.map { value in
13+
{ [value, $0, $1].joined(separator: " ") }
14+
}
15+
}
16+
}
17+
18+
/// Enhances failure messages with a diff tool expression created by the closure, such as an clickable
19+
/// URL or a complex command. The closure will receive the existing screenshot path and the failed
20+
/// screenshot path as arguments.
21+
///
22+
/// ```swift
23+
/// diffToolCommand = { "compare \"\($0)\" \"\($1)\" png: | open -f -a Preview.app" }
24+
/// ```
25+
public var diffToolCommand: ((String, String) -> String)?
1026

1127
/// Whether or not to record all new references.
1228
public var isRecording: Bool = {
@@ -333,8 +349,7 @@ public func verifySnapshot<Value, Format>(
333349
}
334350

335351
let diffMessage =
336-
diffTool
337-
.map { "\($0) \"\(snapshotFileUrl.path)\" \"\(failedSnapshotFileUrl.path)\"" }
352+
diffToolCommand?(snapshotFileUrl.path, failedSnapshotFileUrl.path)
338353
?? """
339354
@\(minus)
340355
"\(snapshotFileUrl.absoluteString)"

0 commit comments

Comments
 (0)