TypedDevs / bashunit

A simple testing library for bash scripts. Test your bash scripts in the fastest and simplest way.
https://bashunit.typeddevs.com
MIT License
324 stars 28 forks source link

Add diff to failed assertions #22

Open khru opened 1 year ago

khru commented 1 year ago

Example of tools we could use:

https://github.com/dandavison/delta

antonio-gg-dev commented 1 year ago

I've done a mini investigation and I want to leave the results here to advance this task a bit, first of all, I've tested 3 tools, diff, wdiff, and colordiff.

I've done all the tests with the following files:

file1:

Hello World
This is the file
And its gorgerous

But this is different
This one is equal

file2:

Hello
This is a file
And its awesome
I like trains
This one is equal

diff comes installed by default (at least on Linux Mint and I think on Ubuntu as well, to be confirmed on other OS) It's a very simple command that returns a line with a code indicating on which lines the changes have occurred, followed by the lines of the first file (prefixed with <) that differ from the lines of the second file (prefixed with >), so this command:

diff file1 file2

Outputs this:

1,5c1,4
< Hello World
< This is the file
< And its gorgerous
< 
< But this is different
---
> Hello
> This is a file
> And its awesome
> I like trains

`colordiff` is a command that needs to be installed, apparently it can be done with apt and brew among others, and this command exclusively colors the diffs, so the output of this command is exactly the same, but in color:
```bash
colordiff file1 file2

Outputs this: image

wdiff needs to be installed too, apparently it can be done with apt and brew among others, focuses on not duplicating the differences and what it does is surround with certain symbols the differences between both files. The following command:

wdiff -n file1 file2

Outputs this:

Hello [-World-]
This is [-the-] {+a+} file
And its [-gorgerous-]
[--]
[-But this is different-] {+awesome+}
{+I like trains+}
This one is equal

And here comes the cherry on top, colordiff is also compatible with wdiff, and if you pass the return of the wdiff with a pipe:

wdiff -n file1 file2 | colordiff

It is able to color it and achieve this wonder: image

If we decide to use these tools, it would be interesting to consider the idea that as the user has more commands available, more functionalities are enabled for them, that is, use diff by default or use wdiff instead if they have it installed, and use colordiff whenever they have it installed together with diff or wdiff.

Another interesting option would be for the user to be able to configure in the .env which command they want to use to print their diffs.

khru commented 1 year ago

Question here, does these two diffs work on Mac as well?

antonio-gg-dev commented 1 year ago

I've also discovered that very similar results can be obtained using git (which is very likely that our users have installed).

git diff --no-index file1 file2

image

git diff --no-index --word-diff file1 file2

image

Chemaclass commented 4 months ago

Can we close this, @antonio-gg-dev @khru ?