Skip to content

Commit b24051d

Browse files
committed
Fix #79639 - Preserving the html report options in parallel worker mode
1 parent 0de3ca4 commit b24051d

File tree

1 file changed

+137
-44
lines changed

1 file changed

+137
-44
lines changed

run-tests.php

Lines changed: 137 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,25 @@ function main()
137137
global $DETAILED, $PHP_FAILED_TESTS, $SHOW_ONLY_GROUPS, $argc, $argv, $cfg,
138138
$cfgfiles, $cfgtypes, $conf_passed, $end_time, $environment,
139139
$exts_skipped, $exts_tested, $exts_to_test, $failed_tests_file,
140-
$html_file, $html_output, $ignored_by_ext, $ini_overwrites, $is_switch,
140+
$ignored_by_ext, $ini_overwrites, $is_switch,
141141
$just_save_results, $log_format, $matches, $no_clean, $no_file_cache,
142142
$optionals, $output_file, $pass_option_n, $pass_options,
143143
$pattern_match, $php, $php_cgi, $phpdbg, $preload, $redir_tests,
144144
$repeat, $result_tests_file, $slow_min_ms, $start_time, $switch,
145145
$temp_source, $temp_target, $temp_urlbase, $test_cnt, $test_dirs,
146146
$test_files, $test_idx, $test_list, $test_results, $testfile,
147-
$user_tests, $valgrind, $sum_results, $shuffle, $file_cache;
147+
$user_tests, $valgrind, $sum_results, $shuffle, $file_cache, $outputHandler;
148148
// Parallel testing
149149
global $workers, $workerID;
150150

151151
define('IS_WINDOWS', substr(PHP_OS, 0, 3) == "WIN");
152152

153+
$outputHandler = new OutputHandler();
154+
153155
$workerID = 0;
154-
if (getenv("TEST_PHP_WORKER")) {
156+
if (is_worker_mode()) {
155157
$workerID = intval(getenv("TEST_PHP_WORKER"));
156-
run_worker();
158+
run_worker($workerID);
157159
return;
158160
}
159161

@@ -378,8 +380,6 @@ function main()
378380

379381
$just_save_results = false;
380382
$valgrind = null;
381-
$html_output = false;
382-
$html_file = null;
383383
$temp_source = null;
384384
$temp_target = null;
385385
$temp_urlbase = null;
@@ -607,8 +607,9 @@ function main()
607607
}
608608
break;
609609
case '--html':
610-
$html_file = fopen($argv[++$i], 'wt');
611-
$html_output = is_resource($html_file);
610+
$html_output_filename = $argv[++$i];
611+
$outputHandler->setHtmlOutputFilename($html_output_filename);
612+
$outputHandler->clearHtmlFile();
612613
break;
613614
case '--version':
614615
echo '$Id$' . "\n";
@@ -691,7 +692,7 @@ function main()
691692
usort($test_files, "test_sort");
692693
$start_time = time();
693694

694-
if (!$html_output) {
695+
if (!$outputHandler->isHtmlEnabled()) {
695696
echo "Running selected tests.\n";
696697
} else {
697698
show_start($start_time);
@@ -701,7 +702,7 @@ function main()
701702
run_all_tests($test_files, $environment);
702703
$end_time = time();
703704

704-
if ($html_output) {
705+
if ($outputHandler->isHtmlEnabled()) {
705706
show_end($end_time);
706707
}
707708

@@ -719,14 +720,14 @@ function main()
719720
}
720721

721722
compute_summary();
722-
if ($html_output) {
723-
fwrite($html_file, "<hr/>\n" . get_summary(false, true));
723+
if ($outputHandler->isHtmlEnabled()) {
724+
$outputHandler->writeHtml("<hr/>\n" . get_summary(false, true));
724725
}
725726
echo "=====================================================================";
726727
echo get_summary(false, false);
727728

728-
if ($html_output) {
729-
fclose($html_file);
729+
if ($outputHandler->isHtmlEnabled()) {
730+
$outputHandler->closeHtmlHandle();;
730731
}
731732

732733
if ($output_file != '' && $just_save_results) {
@@ -792,8 +793,8 @@ function main()
792793
show_end($end_time);
793794
show_summary();
794795

795-
if ($html_output) {
796-
fclose($html_file);
796+
if ($outputHandler->isHtmlEnabled()) {
797+
$outputHandler->closeHtmlHandle();
797798
}
798799

799800
save_or_mail_results();
@@ -1282,7 +1283,7 @@ function system_with_timeout($commandline, $env = null, $stdin = null, $captureS
12821283

12831284
function run_all_tests($test_files, $env, $redir_tested = null)
12841285
{
1285-
global $test_results, $failed_tests_file, $result_tests_file, $php, $test_idx, $file_cache;
1286+
global $test_results, $failed_tests_file, $result_tests_file, $php, $test_idx, $file_cache, $outputHandler;
12861287
// Parallel testing
12871288
global $PHP_FAILED_TESTS, $workers, $workerID, $workerSock;
12881289

@@ -1351,7 +1352,8 @@ function run_all_tests($test_files, $env, $redir_tested = null)
13511352
/** The heart of parallel testing. */
13521353
function run_all_tests_parallel($test_files, $env, $redir_tested)
13531354
{
1354-
global $workers, $test_idx, $test_cnt, $test_results, $failed_tests_file, $result_tests_file, $PHP_FAILED_TESTS, $shuffle, $SHOW_ONLY_GROUPS, $valgrind;
1355+
global $workers, $test_idx, $test_cnt, $test_results, $failed_tests_file, $result_tests_file, $PHP_FAILED_TESTS,
1356+
$shuffle, $SHOW_ONLY_GROUPS, $valgrind, $outputHandler;
13551357

13561358
// The PHP binary running run-tests.php, and run-tests.php itself
13571359
// This PHP executable is *not* necessarily the same as the tested version
@@ -1412,7 +1414,7 @@ function run_all_tests_parallel($test_files, $env, $redir_tested)
14121414
// Don't start more workers than test files.
14131415
$workers = max(1, min($workers, count($test_files)));
14141416

1415-
echo "Spawning workers ";
1417+
echo "Spawning workers ... ";
14161418

14171419
// We use sockets rather than STDIN/STDOUT for comms because on Windows,
14181420
// those can't be non-blocking for some reason.
@@ -1455,10 +1457,18 @@ function run_all_tests_parallel($test_files, $env, $redir_tested)
14551457
error("Failed to accept connection from worker $i");
14561458
}
14571459

1460+
$htmlOutputHandlerSettings = [];
1461+
if($outputHandler->isHtmlEnabled()) {
1462+
$htmlOutputHandlerSettings['filename'] = $outputHandler->getHtmlOutputFilename();
1463+
}
1464+
14581465
$greeting = base64_encode(serialize([
14591466
"type" => "hello",
14601467
"workerID" => $i,
14611468
"GLOBALS" => $GLOBALS,
1469+
'output_handler' => [
1470+
'html' => $htmlOutputHandlerSettings
1471+
],
14621472
"constants" => [
14631473
"INIT_DIR" => INIT_DIR,
14641474
"TEST_PHP_SRCDIR" => TEST_PHP_SRCDIR,
@@ -1688,9 +1698,9 @@ function kill_children(array $children)
16881698
}
16891699
}
16901700

1691-
function run_worker()
1701+
function run_worker($workerID)
16921702
{
1693-
global $workerID, $workerSock;
1703+
global $workerSock, $outputHandler;
16941704

16951705
$sockUri = getenv("TEST_PHP_URI");
16961706

@@ -1721,6 +1731,10 @@ function run_worker()
17211731
define($const, $value);
17221732
}
17231733

1734+
if(isset($greeting["output_handler"]['html']['filename'])) {
1735+
$outputHandler->setHtmlOutputFilename($greeting["output_handler"]['html']['filename']);
1736+
}
1737+
17241738
send_message($workerSock, [
17251739
"type" => "hello_reply",
17261740
"workerID" => $workerID
@@ -1775,14 +1789,15 @@ function show_file_block($file, $block, $section = null)
17751789
//
17761790
function run_test($php, $file, $env)
17771791
{
1778-
global $log_format, $ini_overwrites, $PHP_FAILED_TESTS;
1792+
global $outputHandler, $log_format, $ini_overwrites, $PHP_FAILED_TESTS;
17791793
global $pass_options, $DETAILED, $IN_REDIRECT, $test_cnt, $test_idx;
17801794
global $valgrind, $temp_source, $temp_target, $cfg, $environment;
17811795
global $no_clean;
17821796
global $SHOW_ONLY_GROUPS;
17831797
global $no_file_cache;
17841798
global $slow_min_ms;
17851799
global $preload, $file_cache;
1800+
17861801
// Parallel testing
17871802
global $workerID;
17881803
$temp_filenames = null;
@@ -3187,45 +3202,44 @@ function get_summary($show_ext_summary, $show_html)
31873202

31883203
function show_start($start_time)
31893204
{
3190-
global $html_output, $html_file;
3205+
global $outputHandler;
31913206

3192-
if ($html_output) {
3193-
fwrite($html_file, "<h2>Time Start: " . date('Y-m-d H:i:s', $start_time) . "</h2>\n");
3194-
fwrite($html_file, "<table>\n");
3207+
if ($outputHandler->isHtmlEnabled()) {
3208+
$outputHandler->writeHtml("<h2>Time Start: " . date('Y-m-d H:i:s', $start_time) . "</h2>\n<table>\n");
31953209
}
31963210

31973211
echo "TIME START " . date('Y-m-d H:i:s', $start_time) . "\n=====================================================================\n";
31983212
}
31993213

32003214
function show_end($end_time)
32013215
{
3202-
global $html_output, $html_file;
3216+
global $outputHandler;
32033217

3204-
if ($html_output) {
3205-
fwrite($html_file, "</table>\n");
3206-
fwrite($html_file, "<h2>Time End: " . date('Y-m-d H:i:s', $end_time) . "</h2>\n");
3218+
if ($outputHandler->isHtmlEnabled()) {
3219+
$outputHandler->writeHtml("</table>\n");
3220+
$outputHandler->writeHtml("<h2>Time End: " . date('Y-m-d H:i:s', $end_time) . "</h2>\n");
32073221
}
32083222

32093223
echo "=====================================================================\nTIME END " . date('Y-m-d H:i:s', $end_time) . "\n";
32103224
}
32113225

32123226
function show_summary()
32133227
{
3214-
global $html_output, $html_file;
3228+
global $outputHandler;
32153229

3216-
if ($html_output) {
3217-
fwrite($html_file, "<hr/>\n" . get_summary(true, true));
3230+
if ($outputHandler->isHtmlEnabled()) {
3231+
$outputHandler->writeHtml("<hr/>\n" . get_summary(true, true));
32183232
}
32193233

32203234
echo get_summary(true, false);
32213235
}
32223236

32233237
function show_redirect_start($tests, $tested, $tested_file)
32243238
{
3225-
global $html_output, $html_file, $line_length, $SHOW_ONLY_GROUPS;
3239+
global $outputHandler, $line_length, $SHOW_ONLY_GROUPS;
32263240

3227-
if ($html_output) {
3228-
fwrite($html_file, "<tr><td colspan='3'>---&gt; $tests ($tested [$tested_file]) begin</td></tr>\n");
3241+
if ($outputHandler->isHtmlEnabled()) {
3242+
$outputHandler->writeHtml("<tr><td colspan='3'>---&gt; $tests ($tested [$tested_file]) begin</td></tr>\n");
32293243
}
32303244

32313245
if (!$SHOW_ONLY_GROUPS || in_array('REDIRECT', $SHOW_ONLY_GROUPS)) {
@@ -3237,10 +3251,10 @@ function show_redirect_start($tests, $tested, $tested_file)
32373251

32383252
function show_redirect_ends($tests, $tested, $tested_file)
32393253
{
3240-
global $html_output, $html_file, $line_length, $SHOW_ONLY_GROUPS;
3254+
global $outputHandler, $line_length, $SHOW_ONLY_GROUPS;
32413255

3242-
if ($html_output) {
3243-
fwrite($html_file, "<tr><td colspan='3'>---&gt; $tests ($tested [$tested_file]) done</td></tr>\n");
3256+
if ($outputHandler->isHtmlEnabled()) {
3257+
$outputHandler->writeHtml("<tr><td colspan='3'>---&gt; $tests ($tested [$tested_file]) done</td></tr>\n");
32443258
}
32453259

32463260
if (!$SHOW_ONLY_GROUPS || in_array('REDIRECT', $SHOW_ONLY_GROUPS)) {
@@ -3282,15 +3296,14 @@ function parse_conflicts(string $text): array
32823296

32833297
function show_result($result, $tested, $tested_file, $extra = '', $temp_filenames = null)
32843298
{
3285-
global $html_output, $html_file, $temp_target, $temp_urlbase, $line_length, $SHOW_ONLY_GROUPS;
3286-
3299+
global $temp_target, $temp_urlbase, $line_length, $SHOW_ONLY_GROUPS, $outputHandler;
32873300
if (!$SHOW_ONLY_GROUPS || in_array($result, $SHOW_ONLY_GROUPS)) {
32883301
echo "$result $tested [$tested_file] $extra\n";
32893302
} elseif (!$SHOW_ONLY_GROUPS) {
32903303
clear_show_test();
32913304
}
32923305

3293-
if ($html_output) {
3306+
if ($outputHandler->isHtmlEnabled()) {
32943307
if (isset($temp_filenames['file']) && file_exists($temp_filenames['file'])) {
32953308
$url = str_replace($temp_target, $temp_urlbase, $temp_filenames['file']);
32963309
$tested = "<a href='$url'>$tested</a>";
@@ -3321,8 +3334,7 @@ function show_result($result, $tested, $tested_file, $extra = '', $temp_filename
33213334
$mem = "&nbsp;";
33223335
}
33233336

3324-
fwrite(
3325-
$html_file,
3337+
$outputHandler->writeHtml(
33263338
"<tr>" .
33273339
"<td>$result</td>" .
33283340
"<td>$tested</td>" .
@@ -3692,6 +3704,7 @@ public function wrapCommand($cmd, $memcheck_filename, $check_all)
36923704
}
36933705
}
36943706

3707+
36953708
function init_output_buffers()
36963709
{
36973710
// Delete as much output buffers as possible.
@@ -3703,6 +3716,11 @@ function init_output_buffers()
37033716
}
37043717
}
37053718

3719+
function is_worker_mode()
3720+
{
3721+
return getenv("TEST_PHP_WORKER");
3722+
}
3723+
37063724
function check_proc_open_function_exists()
37073725
{
37083726
if (!function_exists('proc_open')) {
@@ -3719,4 +3737,79 @@ function check_proc_open_function_exists()
37193737
}
37203738
}
37213739

3740+
class OutputHandler
3741+
{
3742+
3743+
/**
3744+
* @var null|string
3745+
*/
3746+
private $htmlOutputFilename = null;
3747+
3748+
/**
3749+
* @var null|resource
3750+
*/
3751+
private $htmlHandle = null;
3752+
3753+
/**
3754+
* @param string $outputFilename
3755+
*/
3756+
public function setHtmlOutputFilename($outputFilename)
3757+
{
3758+
$this->htmlOutputFilename = $outputFilename;
3759+
}
3760+
3761+
public function clearHtmlFile()
3762+
{
3763+
ftruncate($this->getHtmlFileHandle(), 0);
3764+
}
3765+
3766+
/**
3767+
* @return string
3768+
*/
3769+
public function getHtmlOutputFilename()
3770+
{
3771+
if($this->htmlOutputFilename === null) {
3772+
throw new \Exception('No output filename has been set.');
3773+
}
3774+
3775+
return $this->htmlOutputFilename;
3776+
}
3777+
3778+
/**
3779+
* @return false|resource
3780+
*/
3781+
public function getHtmlFileHandle()
3782+
{
3783+
if($this->htmlHandle !== null) {
3784+
return $this->htmlHandle;
3785+
}
3786+
3787+
$this->fileHandle = fopen($this->getHtmlOutputFilename(), 'at+');
3788+
3789+
return $this->fileHandle;
3790+
}
3791+
3792+
/**
3793+
* @return bool
3794+
*/
3795+
public function isHtmlEnabled()
3796+
{
3797+
return !empty($this->htmlOutputFilename) && is_resource($this->getHtmlFileHandle());
3798+
}
3799+
3800+
public function closeHtmlHandle()
3801+
{
3802+
fclose($this->getHtmlFileHandle());
3803+
}
3804+
3805+
/**
3806+
* @param $content
3807+
*/
3808+
public function writeHtml($content)
3809+
{
3810+
fwrite($this->getHtmlFileHandle(), $content);
3811+
}
3812+
3813+
}
3814+
37223815
main();

0 commit comments

Comments
 (0)