pointfreeco / swift-snapshot-testing

📸 Delightful Swift snapshot testing.
https://www.pointfree.co/episodes/ep41-a-tour-of-snapshot-testing
MIT License
3.68k stars 556 forks source link

Add `diffToolBuilder` for building complex diff command messages #840

Closed js closed 1 week ago

js commented 3 months ago

This PR adds SnapshotTesting.diffToolBuilder closure for building complex diff tool command messages or clickable urls in Xcode. 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.

For instance, to open Differati from a clickable link in Xcode the following diffToolBuilder closure can be provided, which URL encodes the paths (in case they contain spaces etc) and builds an url openable by the app:

SnapshotTesting.diffToolBuilder = { existing, failed in
    func encode(_ path: String) -> String {
        path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? path
    }
    return "differati://show?old=\(encode(existing.path))&new=\(encode(failed.path))"
}

Which gives us an Xcode test failure bubble like this, with a clickable url that takes us directly into the diffing app: image

Or it can be used to build a complex diff command with the path arguments aren't simply a suffix, as requested in #780:

SnapshotTesting.diffToolBuilder = { "compare \"\($0.path)\" \"\($1.path)\" png: | open -f -a Preview.app" }
andrewjmeier commented 3 months ago

Thank you for doing this!!!