Skip to content

Commit dec241c

Browse files
committed
Add run-tests.php --context [n] option.
Mentioned in #5965 (comment) This PR proposes 3 lines of context so the impact can be seen in tests. Other `diff` programs show around 3 lines of context. (This helps indicate exactly which position a test should be updated to add a new expected line at)
1 parent 58489bd commit dec241c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

run-tests.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ function show_usage(): void
102102
seconds. The default value is 60 seconds, or 300 seconds when
103103
testing for memory leaks.
104104
105+
--context [n]
106+
Sets the number of lines of surrounding context to print for diffs.
107+
The default value is 3.
108+
105109
--show-[all|php|skip|clean|exp|diff|out|mem]
106110
Show 'all' files, 'php' test file, 'skip' or 'clean' file. You
107111
can also use this to show the output 'out', the expected result
@@ -145,6 +149,7 @@ function main(): void
145149
$user_tests, $valgrind, $sum_results, $shuffle, $file_cache;
146150
// Parallel testing
147151
global $workers, $workerID;
152+
global $context_line_count;
148153

149154
define('IS_WINDOWS', substr(PHP_OS, 0, 3) == "WIN");
150155

@@ -396,6 +401,7 @@ function main(): void
396401
$file_cache = null;
397402
$shuffle = false;
398403
$workers = null;
404+
$context_line_count = 3;
399405

400406
$cfgtypes = ['show', 'keep'];
401407
$cfgfiles = ['skip', 'php', 'clean', 'out', 'diff', 'exp', 'mem'];
@@ -561,6 +567,13 @@ function main(): void
561567
case '--set-timeout':
562568
$environment['TEST_TIMEOUT'] = $argv[++$i];
563569
break;
570+
case '--context':
571+
$context_line_count = $argv[++$i] ?? '';
572+
if (!preg_match('/^\d+$/', $context_line_count)) {
573+
error("'$context_line_count' is not a valid number of lines of context, try e.g. --context 3 for 3 lines");
574+
}
575+
$context_line_count = intval($context_line_count, 10);
576+
break;
564577
case '--show-all':
565578
foreach ($cfgfiles as $file) {
566579
$cfg['show'][$file] = true;
@@ -2878,6 +2891,7 @@ function count_array_diff(
28782891
function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): array
28792892
{
28802893
global $colorize;
2894+
global $context_line_count;
28812895
$idx1 = 0;
28822896
$cnt1 = @count($ar1);
28832897
$idx2 = 0;
@@ -2904,8 +2918,14 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
29042918
}
29052919
return $output;
29062920
};
2921+
$format_context_line = function (int $line_number, string $contents): string {
2922+
// return sprintf("%03d ", $line_number) . $contents; // to print the line number in the expected output
2923+
return str_repeat(' ', max(strlen((string)$line_number), 3) + 2) . $contents;
2924+
};
2925+
$mapping = [];
29072926

29082927
while ($idx1 < $cnt1 && $idx2 < $cnt2) {
2928+
$mapping[$idx2] = $idx1;
29092929
if (comp_line($ar1[$idx1], $ar2[$idx2], $is_reg)) {
29102930
$idx1++;
29112931
$idx2++;
@@ -2922,43 +2942,72 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
29222942
$old1[$idx1] = $format_expected_line($idx1 + 1, $w[$idx1++]);
29232943
$old2[$idx2] = $format_actual_line($idx2 + 1, $ar2[$idx2++]);
29242944
}
2945+
$last_printed_context_line = $idx1;
29252946
}
29262947
}
2948+
$mapping[$idx2] = $idx1;
29272949

29282950
reset($old1);
29292951
$k1 = key($old1);
29302952
$l1 = -2;
29312953
reset($old2);
29322954
$k2 = key($old2);
29332955
$l2 = -2;
2956+
$old_k1 = -1;
2957+
$add_context_lines = function (int $new_k1) use (&$old_k1, &$diff, $w, $context_line_count, $format_context_line) {
2958+
$end = $new_k1 - 1;
2959+
$range_end = min($end, $old_k1 + $context_line_count);
2960+
if ($old_k1 >= 0) {
2961+
while ($old_k1 < $range_end) {
2962+
$diff[] = $format_context_line($old_k1 + 1, $w[$old_k1++]);
2963+
}
2964+
}
2965+
if ($end - $context_line_count > $old_k1) {
2966+
$old_k1 = $end - $context_line_count;
2967+
$diff[] = '--';
2968+
}
2969+
$old_k1 = max($old_k1, 0);
2970+
while ($old_k1 < $end) {
2971+
$diff[] = $format_context_line($old_k1 + 1, $w[$old_k1++]);
2972+
}
2973+
$old_k1 = $new_k1;
2974+
};
29342975

29352976
while ($k1 !== null || $k2 !== null) {
29362977
if ($k1 == $l1 + 1 || $k2 === null) {
2978+
$add_context_lines($k1);
29372979
$l1 = $k1;
29382980
$diff[] = current($old1);
2981+
$old_k1 = $k1;
29392982
$k1 = next($old1) ? key($old1) : null;
29402983
} elseif ($k2 == $l2 + 1 || $k1 === null) {
2984+
$add_context_lines($mapping[$k2]);
29412985
$l2 = $k2;
29422986
$diff[] = current($old2);
29432987
$k2 = next($old2) ? key($old2) : null;
29442988
} elseif ($k1 < $k2) {
2989+
$add_context_lines($k1);
29452990
$l1 = $k1;
29462991
$diff[] = current($old1);
29472992
$k1 = next($old1) ? key($old1) : null;
29482993
} else {
2994+
$add_context_lines($mapping[$k2]);
29492995
$l2 = $k2;
29502996
$diff[] = current($old2);
29512997
$k2 = next($old2) ? key($old2) : null;
29522998
}
29532999
}
29543000

29553001
while ($idx1 < $cnt1) {
3002+
$add_context_lines($idx1 + 1);
29563003
$diff[] = $format_expected_line($idx1 + 1, $w[$idx1++]);
29573004
}
29583005

29593006
while ($idx2 < $cnt2) {
3007+
$add_context_lines($mapping[$idx2] + 1);
29603008
$diff[] = $format_actual_line($idx2 + 1, $ar2[$idx2++]);
29613009
}
3010+
$add_context_lines(min($old_k1 + $context_line_count + 1, $cnt1 + 1));
29623011

29633012
return $diff;
29643013
}

0 commit comments

Comments
 (0)