Skip to content

Commit d0cbb72

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

File tree

2 files changed

+139
-44
lines changed

2 files changed

+139
-44
lines changed

run-tests.php

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ function show_usage()
122122
HELP;
123123
}
124124

125+
include('tests-util/OutputHandler.php');
126+
125127
/**
126128
* One function to rule them all, one function to find them, one function to
127129
* bring them all and in the darkness bind them.
@@ -137,23 +139,25 @@ function main()
137139
global $DETAILED, $PHP_FAILED_TESTS, $SHOW_ONLY_GROUPS, $argc, $argv, $cfg,
138140
$cfgfiles, $cfgtypes, $conf_passed, $end_time, $environment,
139141
$exts_skipped, $exts_tested, $exts_to_test, $failed_tests_file,
140-
$html_file, $html_output, $ignored_by_ext, $ini_overwrites, $is_switch,
142+
$ignored_by_ext, $ini_overwrites, $is_switch,
141143
$just_save_results, $log_format, $matches, $no_clean, $no_file_cache,
142144
$optionals, $output_file, $pass_option_n, $pass_options,
143145
$pattern_match, $php, $php_cgi, $phpdbg, $preload, $redir_tests,
144146
$repeat, $result_tests_file, $slow_min_ms, $start_time, $switch,
145147
$temp_source, $temp_target, $temp_urlbase, $test_cnt, $test_dirs,
146148
$test_files, $test_idx, $test_list, $test_results, $testfile,
147-
$user_tests, $valgrind, $sum_results, $shuffle, $file_cache;
149+
$user_tests, $valgrind, $sum_results, $shuffle, $file_cache, $outputHandler;
148150
// Parallel testing
149151
global $workers, $workerID;
150152

151153
define('IS_WINDOWS', substr(PHP_OS, 0, 3) == "WIN");
152154

155+
$outputHandler = new OutputHandler();
156+
153157
$workerID = 0;
154-
if (getenv("TEST_PHP_WORKER")) {
158+
if (is_worker_mode()) {
155159
$workerID = intval(getenv("TEST_PHP_WORKER"));
156-
run_worker();
160+
run_worker($workerID);
157161
return;
158162
}
159163

@@ -378,8 +382,6 @@ function main()
378382

379383
$just_save_results = false;
380384
$valgrind = null;
381-
$html_output = false;
382-
$html_file = null;
383385
$temp_source = null;
384386
$temp_target = null;
385387
$temp_urlbase = null;
@@ -607,8 +609,9 @@ function main()
607609
}
608610
break;
609611
case '--html':
610-
$html_file = fopen($argv[++$i], 'wt');
611-
$html_output = is_resource($html_file);
612+
$html_output_filename = $argv[++$i];
613+
$outputHandler->setHtmlOutputFilename($html_output_filename);
614+
$outputHandler->clearHtmlFile();
612615
break;
613616
case '--version':
614617
echo '$Id$' . "\n";
@@ -691,7 +694,7 @@ function main()
691694
usort($test_files, "test_sort");
692695
$start_time = time();
693696

694-
if (!$html_output) {
697+
if (!$outputHandler->isHtmlEnabled()) {
695698
echo "Running selected tests.\n";
696699
} else {
697700
show_start($start_time);
@@ -701,7 +704,7 @@ function main()
701704
run_all_tests($test_files, $environment);
702705
$end_time = time();
703706

704-
if ($html_output) {
707+
if ($outputHandler->isHtmlEnabled()) {
705708
show_end($end_time);
706709
}
707710

@@ -719,14 +722,14 @@ function main()
719722
}
720723

721724
compute_summary();
722-
if ($html_output) {
723-
fwrite($html_file, "<hr/>\n" . get_summary(false, true));
725+
if ($outputHandler->isHtmlEnabled()) {
726+
$outputHandler->writeHtml("<hr/>\n" . get_summary(false, true));
724727
}
725728
echo "=====================================================================";
726729
echo get_summary(false, false);
727730

728-
if ($html_output) {
729-
fclose($html_file);
731+
if ($outputHandler->isHtmlEnabled()) {
732+
$outputHandler->closeHtmlHandle();;
730733
}
731734

732735
if ($output_file != '' && $just_save_results) {
@@ -792,8 +795,8 @@ function main()
792795
show_end($end_time);
793796
show_summary();
794797

795-
if ($html_output) {
796-
fclose($html_file);
798+
if ($outputHandler->isHtmlEnabled()) {
799+
$outputHandler->closeHtmlHandle();
797800
}
798801

799802
save_or_mail_results();
@@ -1282,7 +1285,7 @@ function system_with_timeout($commandline, $env = null, $stdin = null, $captureS
12821285

12831286
function run_all_tests($test_files, $env, $redir_tested = null)
12841287
{
1285-
global $test_results, $failed_tests_file, $result_tests_file, $php, $test_idx, $file_cache;
1288+
global $test_results, $failed_tests_file, $result_tests_file, $php, $test_idx, $file_cache, $outputHandler;
12861289
// Parallel testing
12871290
global $PHP_FAILED_TESTS, $workers, $workerID, $workerSock;
12881291

@@ -1351,7 +1354,8 @@ function run_all_tests($test_files, $env, $redir_tested = null)
13511354
/** The heart of parallel testing. */
13521355
function run_all_tests_parallel($test_files, $env, $redir_tested)
13531356
{
1354-
global $workers, $test_idx, $test_cnt, $test_results, $failed_tests_file, $result_tests_file, $PHP_FAILED_TESTS, $shuffle, $SHOW_ONLY_GROUPS, $valgrind;
1357+
global $workers, $test_idx, $test_cnt, $test_results, $failed_tests_file, $result_tests_file, $PHP_FAILED_TESTS,
1358+
$shuffle, $SHOW_ONLY_GROUPS, $valgrind, $outputHandler;
13551359

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

1415-
echo "Spawning workers ";
1419+
echo "Spawning workers ... ";
14161420

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

1462+
$htmlOutputHandlerSettings = [];
1463+
if($outputHandler->isHtmlEnabled()) {
1464+
$htmlOutputHandlerSettings['filename'] = $outputHandler->getHtmlOutputFilename();
1465+
}
1466+
14581467
$greeting = base64_encode(serialize([
14591468
"type" => "hello",
14601469
"workerID" => $i,
14611470
"GLOBALS" => $GLOBALS,
1471+
'output_handler' => [
1472+
'html' => $htmlOutputHandlerSettings
1473+
],
14621474
"constants" => [
14631475
"INIT_DIR" => INIT_DIR,
14641476
"TEST_PHP_SRCDIR" => TEST_PHP_SRCDIR,
@@ -1688,9 +1700,9 @@ function kill_children(array $children)
16881700
}
16891701
}
16901702

1691-
function run_worker()
1703+
function run_worker($workerID)
16921704
{
1693-
global $workerID, $workerSock;
1705+
global $workerSock, $outputHandler;
16941706

16951707
$sockUri = getenv("TEST_PHP_URI");
16961708

@@ -1721,6 +1733,10 @@ function run_worker()
17211733
define($const, $value);
17221734
}
17231735

1736+
if(isset($greeting["output_handler"]['html']['filename'])) {
1737+
$outputHandler->setHtmlOutputFilename($greeting["output_handler"]['html']['filename']);
1738+
}
1739+
17241740
send_message($workerSock, [
17251741
"type" => "hello_reply",
17261742
"workerID" => $workerID
@@ -1775,14 +1791,15 @@ function show_file_block($file, $block, $section = null)
17751791
//
17761792
function run_test($php, $file, $env)
17771793
{
1778-
global $log_format, $ini_overwrites, $PHP_FAILED_TESTS;
1794+
global $outputHandler, $log_format, $ini_overwrites, $PHP_FAILED_TESTS;
17791795
global $pass_options, $DETAILED, $IN_REDIRECT, $test_cnt, $test_idx;
17801796
global $valgrind, $temp_source, $temp_target, $cfg, $environment;
17811797
global $no_clean;
17821798
global $SHOW_ONLY_GROUPS;
17831799
global $no_file_cache;
17841800
global $slow_min_ms;
17851801
global $preload, $file_cache;
1802+
17861803
// Parallel testing
17871804
global $workerID;
17881805
$temp_filenames = null;
@@ -3187,45 +3204,44 @@ function get_summary($show_ext_summary, $show_html)
31873204

31883205
function show_start($start_time)
31893206
{
3190-
global $html_output, $html_file;
3207+
global $outputHandler;
31913208

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");
3209+
if ($outputHandler->isHtmlEnabled()) {
3210+
$outputHandler->writeHtml("<h2>Time Start: " . date('Y-m-d H:i:s', $start_time) . "</h2>\n<table>\n");
31953211
}
31963212

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

32003216
function show_end($end_time)
32013217
{
3202-
global $html_output, $html_file;
3218+
global $outputHandler;
32033219

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");
3220+
if ($outputHandler->isHtmlEnabled()) {
3221+
$outputHandler->writeHtml("</table>\n");
3222+
$outputHandler->writeHtml("<h2>Time End: " . date('Y-m-d H:i:s', $end_time) . "</h2>\n");
32073223
}
32083224

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

32123228
function show_summary()
32133229
{
3214-
global $html_output, $html_file;
3230+
global $outputHandler;
32153231

3216-
if ($html_output) {
3217-
fwrite($html_file, "<hr/>\n" . get_summary(true, true));
3232+
if ($outputHandler->isHtmlEnabled()) {
3233+
$outputHandler->writeHtml("<hr/>\n" . get_summary(true, true));
32183234
}
32193235

32203236
echo get_summary(true, false);
32213237
}
32223238

32233239
function show_redirect_start($tests, $tested, $tested_file)
32243240
{
3225-
global $html_output, $html_file, $line_length, $SHOW_ONLY_GROUPS;
3241+
global $outputHandler, $line_length, $SHOW_ONLY_GROUPS;
32263242

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

32313247
if (!$SHOW_ONLY_GROUPS || in_array('REDIRECT', $SHOW_ONLY_GROUPS)) {
@@ -3237,10 +3253,10 @@ function show_redirect_start($tests, $tested, $tested_file)
32373253

32383254
function show_redirect_ends($tests, $tested, $tested_file)
32393255
{
3240-
global $html_output, $html_file, $line_length, $SHOW_ONLY_GROUPS;
3256+
global $outputHandler, $line_length, $SHOW_ONLY_GROUPS;
32413257

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

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

32833299
function show_result($result, $tested, $tested_file, $extra = '', $temp_filenames = null)
32843300
{
3285-
global $html_output, $html_file, $temp_target, $temp_urlbase, $line_length, $SHOW_ONLY_GROUPS;
3286-
3301+
global $temp_target, $temp_urlbase, $line_length, $SHOW_ONLY_GROUPS, $outputHandler;
32873302
if (!$SHOW_ONLY_GROUPS || in_array($result, $SHOW_ONLY_GROUPS)) {
32883303
echo "$result $tested [$tested_file] $extra\n";
32893304
} elseif (!$SHOW_ONLY_GROUPS) {
32903305
clear_show_test();
32913306
}
32923307

3293-
if ($html_output) {
3308+
if ($outputHandler->isHtmlEnabled()) {
32943309
if (isset($temp_filenames['file']) && file_exists($temp_filenames['file'])) {
32953310
$url = str_replace($temp_target, $temp_urlbase, $temp_filenames['file']);
32963311
$tested = "<a href='$url'>$tested</a>";
@@ -3321,8 +3336,7 @@ function show_result($result, $tested, $tested_file, $extra = '', $temp_filename
33213336
$mem = "&nbsp;";
33223337
}
33233338

3324-
fwrite(
3325-
$html_file,
3339+
$outputHandler->writeHtml(
33263340
"<tr>" .
33273341
"<td>$result</td>" .
33283342
"<td>$tested</td>" .
@@ -3703,6 +3717,11 @@ function init_output_buffers()
37033717
}
37043718
}
37053719

3720+
function is_worker_mode()
3721+
{
3722+
return getenv("TEST_PHP_WORKER");
3723+
}
3724+
37063725
function check_proc_open_function_exists()
37073726
{
37083727
if (!function_exists('proc_open')) {

tests-util/OutputHandler.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
class OutputHandler
4+
{
5+
6+
/**
7+
* @var null|string
8+
*/
9+
private $htmlOutputFilename = null;
10+
11+
/**
12+
* @var null|resource
13+
*/
14+
private $htmlHandle = null;
15+
16+
/**
17+
* @param string $outputFilename
18+
*/
19+
public function setHtmlOutputFilename($outputFilename)
20+
{
21+
$this->htmlOutputFilename = $outputFilename;
22+
}
23+
24+
public function clearHtmlFile()
25+
{
26+
ftruncate($this->getHtmlFileHandle(), 0);
27+
}
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getHtmlOutputFilename()
33+
{
34+
if($this->htmlOutputFilename === null) {
35+
throw new \Exception('No output filename has been set.');
36+
}
37+
38+
return $this->htmlOutputFilename;
39+
}
40+
41+
/**
42+
* @return false|resource
43+
*/
44+
public function getHtmlFileHandle()
45+
{
46+
if($this->htmlHandle !== null) {
47+
return $this->htmlHandle;
48+
}
49+
50+
$this->fileHandle = fopen($this->getHtmlOutputFilename(), 'at+');
51+
52+
return $this->fileHandle;
53+
}
54+
55+
/**
56+
* @return bool
57+
*/
58+
public function isHtmlEnabled()
59+
{
60+
return !empty($this->getHtmlOutputFilename()) && is_resource($this->getHtmlFileHandle());
61+
}
62+
63+
public function closeHtmlHandle()
64+
{
65+
fclose($this->getHtmlFileHandle());
66+
}
67+
68+
/**
69+
* @param $content
70+
*/
71+
public function writeHtml($content)
72+
{
73+
fwrite($this->getHtmlFileHandle(), $content);
74+
}
75+
76+
}

0 commit comments

Comments
 (0)