Skip to content

Commit 58489bd

Browse files
committed
Make diff section contents red(-)/green(+) in run-tests.php
This uses green for lines with `+` and red for lines with `-`. Colors(Red and Green) would make the failure causes stand out visually when scrolling through errors. Closes GH-5965
1 parent e79adf6 commit 58489bd

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

run-tests.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,7 @@ function count_array_diff(
28772877

28782878
function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): array
28792879
{
2880+
global $colorize;
28802881
$idx1 = 0;
28812882
$cnt1 = @count($ar1);
28822883
$idx2 = 0;
@@ -2885,6 +2886,25 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
28852886
$old1 = [];
28862887
$old2 = [];
28872888

2889+
$format_expected_line = function (int $line_number, string $contents) use ($colorize): string {
2890+
$output = sprintf("%03d- ", $line_number) . $contents;
2891+
if ($colorize) {
2892+
// Reuse the colors used for `-` in other diff tools.
2893+
// Here, red should be interpreted as "removed", and not "bad".
2894+
return "\e[1;31m{$output}\e[0m";
2895+
}
2896+
return $output;
2897+
};
2898+
$format_actual_line = function (int $line_number, string $contents) use ($colorize): string {
2899+
$output = sprintf("%03d+ ", $line_number) . $contents;
2900+
if ($colorize) {
2901+
// Reuse the colors used for `+` in other diff tools.
2902+
// Here, green should be interpreted as "added", and not "good".
2903+
return "\e[1;32m{$output}\e[0m";
2904+
}
2905+
return $output;
2906+
};
2907+
28882908
while ($idx1 < $cnt1 && $idx2 < $cnt2) {
28892909
if (comp_line($ar1[$idx1], $ar2[$idx2], $is_reg)) {
28902910
$idx1++;
@@ -2895,12 +2915,12 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
28952915
$c2 = @count_array_diff($ar1, $ar2, $is_reg, $w, $idx1, $idx2 + 1, $cnt1, $cnt2, 10);
28962916

28972917
if ($c1 > $c2) {
2898-
$old1[$idx1] = sprintf("%03d- ", $idx1 + 1) . $w[$idx1++];
2918+
$old1[$idx1] = $format_expected_line($idx1 + 1, $w[$idx1++]);
28992919
} elseif ($c2 > 0) {
2900-
$old2[$idx2] = sprintf("%03d+ ", $idx2 + 1) . $ar2[$idx2++];
2920+
$old2[$idx2] = $format_actual_line($idx2 + 1, $ar2[$idx2++]);
29012921
} else {
2902-
$old1[$idx1] = sprintf("%03d- ", $idx1 + 1) . $w[$idx1++];
2903-
$old2[$idx2] = sprintf("%03d+ ", $idx2 + 1) . $ar2[$idx2++];
2922+
$old1[$idx1] = $format_expected_line($idx1 + 1, $w[$idx1++]);
2923+
$old2[$idx2] = $format_actual_line($idx2 + 1, $ar2[$idx2++]);
29042924
}
29052925
}
29062926
}
@@ -2933,11 +2953,11 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
29332953
}
29342954

29352955
while ($idx1 < $cnt1) {
2936-
$diff[] = sprintf("%03d- ", $idx1 + 1) . $w[$idx1++];
2956+
$diff[] = $format_expected_line($idx1 + 1, $w[$idx1++]);
29372957
}
29382958

29392959
while ($idx2 < $cnt2) {
2940-
$diff[] = sprintf("%03d+ ", $idx2 + 1) . $ar2[$idx2++];
2960+
$diff[] = $format_actual_line($idx2 + 1, $ar2[$idx2++]);
29412961
}
29422962

29432963
return $diff;

0 commit comments

Comments
 (0)