Nimble icon indicating copy to clipboard operation
Nimble copied to clipboard

Newlines in the extended failure message are unconditionally stripped

Open agurtovoy opened this issue 3 years ago • 1 comments

  • [x] I have read CONTRIBUTING and have done my best to follow them.

What did you do?

First of all, thanks for the awesome library!

We have written a custom Nimble <-> swift-snapshot-testing integration that essentially looks like this:

public func matchSnapshot<T, Format>(named name: String? = nil, as format: Snapshotting<T, Format>,
                                     file: StaticString = #file, line: UInt = #line) -> Predicate<T> {
    return Predicate { (actualExpression: Expression<T>) throws -> PredicateResult in
        let msg = ExpectationMessage.expectedTo(name != nil ? "match snapshot '\(name!)'" : "match snapshot")
        if let actualValue = try actualExpression.evaluate() {
            if let failure = verifySnapshot(matching: actualValue, as: format, named: name,
                                           file: file, testName: QuickSpec.current.shortName, line: line) {
                return PredicateResult(
                    status: .fail,
                    message: msg.appended(details: failure) 
                    //                    ^^^^^^^^^^^^^^^^^
                    //                    expected appended `details` to preserve newlines
                )
            }
                
            return PredicateResult(status: .matches, message: msg)
        } else {
            return PredicateResult(
                status: .fail,
                message: msg.appendedBeNilHint()
            )
        }
    }
}

When not nil, failure is a multi-line string diff between the actual value and the snapshot.

What did you expect to happen?

We expected that Nimble would preserve the newlines in the extended failure message.

What actually happened instead?

Instead, newlines in the extended failure message appear to be unconditionally stripped, which renders the snapshot diffs unusable:

https://github.com/Quick/Nimble/blob/0bf627cd68085345ac52c165ba02d1f73c584eed/Sources/Nimble/FailureMessage.swift#L59

This also seems to violate the documented intent of appended(details: String):

For a more comprehensive message that spans multiple lines, use appended(details: String) instead

Environment

List the software versions you're using:

  • Quick: 4.0.0
  • Nimble: 9.1.0
  • Xcode Version: 13.2 (13C90)
  • Swift Version: Xcode Default

Please also mention which package manager you used and its version. Delete the other package managers in this list:

  • Swift Package Manager: 5.5.0

agurtovoy avatar Jan 23 '22 01:01 agurtovoy

Going to prioritize this for v11 (assuming this is something we can actually fix and not, like, an Xcode/xctest limitation).

younata avatar Apr 19 '22 22:04 younata