Skip to content

Commit 66624e7

Browse files
authored
Use red or green background for whitespace-only diffs (#5476)
1 parent bd7b484 commit 66624e7

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

lib/ex_unit/examples/difference.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ defmodule Difference do
2525
assert string1 == string2
2626
end
2727

28+
test "whitespace" do
29+
list1 = [%{a: "abc "}, %{a: "def"}, %{c: "gh"}]
30+
list2 = [%{a: "abc"}, %{a: " def"}, %{c: "hi"}]
31+
assert list1 == list2
32+
end
33+
2834
test "large strings" do
2935
string1 = "short"
3036
string2 = "really long string that should not emit diff"

lib/ex_unit/lib/ex_unit/cli_formatter.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,13 @@ defmodule ExUnit.CLIFormatter do
179179
# Color styles
180180

181181
defp colorize(escape, string, %{colors: colors}) do
182-
[escape | string]
183-
|> IO.ANSI.format(colors[:enabled])
184-
|> IO.iodata_to_binary
182+
if colors[:enabled] do
183+
[escape, string, :reset]
184+
|> IO.ANSI.format_fragment(true)
185+
|> IO.iodata_to_binary
186+
else
187+
string
188+
end
185189
end
186190

187191
defp success(msg, config) do
@@ -211,9 +215,15 @@ defmodule ExUnit.CLIFormatter do
211215
defp formatter(:diff_delete, msg, config),
212216
do: colorize(:red, msg, config)
213217

218+
defp formatter(:diff_delete_whitespace, msg, config),
219+
do: colorize(IO.ANSI.color_background(2, 0, 0), msg, config)
220+
214221
defp formatter(:diff_insert, msg, config),
215222
do: colorize(:green, msg, config)
216223

224+
defp formatter(:diff_insert_whitespace, msg, config),
225+
do: colorize(IO.ANSI.color_background(0, 2, 0), msg, config)
226+
217227
defp formatter(_, msg, _config),
218228
do: msg
219229

lib/ex_unit/lib/ex_unit/formatter.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,17 @@ defmodule ExUnit.Formatter do
252252
end
253253

254254
defp colorize_diff({:del, content}, formatter, {left, right}) do
255-
{[left | formatter.(:diff_delete, content)], right}
255+
format = colorize_format(content, :diff_delete, :diff_delete_whitespace)
256+
{[left | formatter.(format, content)], right}
256257
end
257258

258259
defp colorize_diff({:ins, content}, formatter, {left, right}) do
259-
{left, [right | formatter.(:diff_insert, content)]}
260+
format = colorize_format(content, :diff_insert, :diff_insert_whitespace)
261+
{left, [right | formatter.(format, content)]}
262+
end
263+
264+
defp colorize_format(content, normal, whitespace) do
265+
if String.trim_leading(content) == "", do: whitespace, else: normal
260266
end
261267

262268
defp edit_script(left, right) do

0 commit comments

Comments
 (0)