testify icon indicating copy to clipboard operation
testify copied to clipboard

assert: add option to colorize diffs produced by assert.Equal

Open mitioshi opened this issue 2 years ago • 4 comments

This PR add an option to colorize diffs produced by assert.Equal

Changes

In this PR, the library used for diffing was replaced to diffmatchpatch which is actively maintained and enables us to access structured diffs. Since testify is frequently used in CI/CD pipelines or piped to non-tty buffers, I made colorized diffs controlled by the TESTIFY_COLORED_DIFF environment variable. This way testify will not break any existing pipelines while letting the user control this behavior(e.g. via setting it in the go test template like this image)

Motivation

When running tests in IDE/text editor, it's difficult to analyze the diffs produced by assert.Equal. This could be improved by making diff colorized. difflib is no longer maintained and cannot be extended to modify the diff structure, so it might be better to switch to another library: https://github.com/sergi/go-diff

The resulting diff looks like this after the suggested changes image

The relevant issue: https://github.com/stretchr/testify/issues/1479

mitioshi avatar Oct 05 '23 17:10 mitioshi

Related: #1467.

dolmen avatar Oct 10 '23 15:10 dolmen

Hello @mitioshi any plan to continue your work?

Some suggestion: Maybe reverse the red and green color since the green one usually means the good one (expected value)

Cheers

hendrywiranto avatar Feb 22 '24 01:02 hendrywiranto

Hello @mitioshi any plan to continue your work?

Some suggestion: Maybe reverse the red and green color since the green one usually means the good one (expected value)

Cheers

Aw, I didn't see the initial comments and thought this PR had been overlooked. Yes, I'll look into the suggested changes this week

mitioshi avatar Feb 22 '24 02:02 mitioshi

@dolmen I submitted a new PR for the library change as you suggested -> https://github.com/stretchr/testify/pull/1546. Could you please review it, so we can proceed with the current PR ?

mitioshi avatar Feb 22 '24 07:02 mitioshi