Spec: show diff between expected and actual values when should eq fails#10006
Spec: show diff between expected and actual values when should eq fails#10006makenowjust wants to merge 7 commits intocrystal-lang:masterfrom
should eq fails#10006Conversation
src/spec/expectations.cr
Outdated
|
|
||
| begin | ||
| # Invoke `diff` command and fix up its output. | ||
| result = `#{diff_command} -u #{expected_file.path} #{actual_file.path}` |
There was a problem hiding this comment.
Please use Process.run/run? for better error and stream handling, path escapes etc.
|
This looks great! I'm not sure how it behaves with long values and huge differences. Maybe the diff should be hidden when it's too long? I don't think it's particularly useful when it spans over a page or so. @oprypin It would be really helpful if you could share your concerns instead of just giving a thumbs down. |
|
|
|
I just think that this is a maintenance liability. Running specs will now require a |
src/spec/expectations.cr
Outdated
| diff_command = Spec.diff_command | ||
| return unless diff_command && (expected.includes?('\n') || actual.includes?('\n')) | ||
|
|
||
| expected_file = File.tempfile("expected") { |f| f.puts expected } |
There was a problem hiding this comment.
If this will be merged, there should be some investigation towards limiting Crystal's tmp file footprint to a particular subdirectory. Maybe there is already a spec-global top-level directory being used somewhere? I know in Crystal's own specs there is but that's not it.
|
@oprypin This does not require |
|
The spec for this requires |
Co-authored-by: Caspian Baska <caspianbaska@gmail.com>
|
Added
Hmm, container env may lack |
|
We can use |
|
What should I do? |
|
I think it would be nice to have a |
|
|
|
@asterite That's the opposite of what you said here #9948 (comment) |
I guess and believe the latter part is impossible... (#9948 (comment)) Repeatedly, my opinion is the best idea is to use system |
|
@oprypin Yes, I changed my mind :-) In any case, if others think that relying on |
Fixed crystal-lang#9948 Close crystal-lang#10006 This commit adds `Diff` module. It has three public methods: - `Diff.diff` computes a diff between two collections. - `Diff.unified_diff` computes a diff between two strings on lines, and returns the patch string as unified diff format. - `Diff.show_unified_diff` shows the given patch and original data as unified diff format. This implementation is less than 150 LOC 🎉 And, this commit also updates `should eq` failure message by using diff.
|
I have a diffing library that can be used. https://github.com/dsisnero/similar.cr/tree/main, ir is a port of a rust library |
|
Nice! I recall I added one to minitest.cr assertions, adapted from crystal-diff by @makenowjust. It's incredibly tiny. |
Fixed #9948
Screenshot:
This PR adds only
diffoutput to theAssertionFailedmessage.We can consider the following enhancements:
#pretty_inspectoutput stable onHashkeys andSetvalues fordiff.(by adding
stableoption to#pretty_inspect?)diffoutput for human readability.wdiffcommand.Thank you.