From ddcc975d1a7a476a55ef81f6474d1a027d67c3c0 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 26 Nov 2011 20:18:03 +0000 Subject: [PATCH] - Added JUnit output format option patch by: Ferenc Kovacs (tyrael at php.net) --- run-tests.php | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/run-tests.php b/run-tests.php index e9c6fcb8fda..127265798a3 100755 --- a/run-tests.php +++ b/run-tests.php @@ -200,6 +200,26 @@ if (getenv('TEST_PHP_DETAILED')) { $DETAILED = 0; } +// Check whether a junit log is wanted. +$JUNIT = getenv('TEST_PHP_JUNIT'); +if (empty($JUNIT) || (!file_exists($JUNIT) && !is_writable(dirname($JUNIT))) || (file_exists($JUNIT) && !is_writable($JUNIT)) || !($JUNIT = @fopen($JUNIT, 'w'))) { + $JUNIT = FALSE; +} +else{ + $JUNIT = array( + 'fp' => $JUNIT, + 'test_total' => 0, + 'test_pass' => 0, + 'test_fail' => 0, + 'test_error' => 0, + 'test_skip' => 0, + 'started_at' => microtime(true), + 'finished_at' => NULL, + 'execution_time'=> NULL, + 'result_xml' => '', + ); +} + if (getenv('SHOW_ONLY_GROUPS')) { $SHOW_ONLY_GROUPS = explode(",", getenv('SHOW_ONLY_GROUPS')); } else { @@ -811,6 +831,10 @@ HELP; if ($output_file != '' && $just_save_results) { save_or_mail_results(); } + + if ($JUNIT) { + save_junit_xml(); + } if (getenv('REPORT_EXIT_STATUS') == 1 and preg_match('/FAILED(?: |$)/', implode(' ', $test_results))) { exit(1); @@ -946,6 +970,10 @@ if ($html_output) { save_or_mail_results(); +if ($JUNIT) { + save_junit_xml(); +} + if (getenv('REPORT_EXIT_STATUS') == 1 and $sum_results['FAILED']) { exit(1); } @@ -1165,6 +1193,7 @@ function run_test($php, $file, $env) global $leak_check, $temp_source, $temp_target, $cfg, $environment; global $no_clean; global $valgrind_version; + global $JUNIT; $temp_filenames = null; $org_file = $file; @@ -1300,6 +1329,14 @@ TEST $file 'diff' => '', 'info' => "$bork_info [$file]", ); + + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_error']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'BORKED'; } @@ -1322,6 +1359,13 @@ TEST $file $php = realpath("./sapi/cgi/php-cgi") . ' -C '; } else { show_result('SKIP', $tested, $tested_file, "reason: CGI not available"); + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_skip']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= 'CGI not available'."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'SKIPPED'; } } @@ -1459,7 +1503,14 @@ TEST $file $env['USE_ZEND_ALLOC'] = '1'; } + if ($JUNIT) { + $test_started_at = microtime(true); + } $output = system_with_timeout("$extra $php $pass_options -q $ini_settings -d display_errors=0 $test_skipif", $env); + if ($JUNIT) { + $test_finished_at = microtime(true); + $test_execution_time= number_format($test_finished_at-$test_started_at, 2); + } if (!$cfg['keep']['skip']) { @unlink($test_skipif); @@ -1481,6 +1532,13 @@ TEST $file @unlink($test_skipif); } + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_skip']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'SKIPPED'; } @@ -1535,6 +1593,12 @@ TEST $file // a redirected test never fails $IN_REDIRECT = false; + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_pass']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'REDIR'; } else { @@ -1566,6 +1630,13 @@ TEST $file 'diff' => '', 'info' => "$bork_info [$file]", ); + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_error']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'BORKED'; } @@ -1619,6 +1690,13 @@ TEST $file $env['REQUEST_METHOD'] = 'POST'; if (empty($request)) { + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_error']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'BORKED'; } @@ -1681,7 +1759,14 @@ HTTP_COOKIE = " . $env['HTTP_COOKIE'] . " COMMAND $cmd "; + if ($JUNIT) { + $test_started_at = microtime(true); + } $out = system_with_timeout($cmd, $env, isset($section_text['STDIN']) ? $section_text['STDIN'] : null); + if ($JUNIT) { + $test_finished_at = microtime(true); + $test_execution_time= number_format($test_finished_at-$test_started_at, 2); + } if (array_key_exists('CLEAN', $section_text) && (!$no_clean || $cfg['keep']['clean'])) { @@ -1870,6 +1955,12 @@ COMMAND $cmd $info = " (warn: XFAIL section but test passes)"; }else { show_result("PASS", $tested, $tested_file, '', $temp_filenames); + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_pass']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'PASSED'; } } @@ -1899,6 +1990,12 @@ COMMAND $cmd $info = " (warn: XFAIL section but test passes)"; }else { show_result("PASS", $tested, $tested_file, '', $temp_filenames); + if ($JUNIT) { + $JUNIT['test_total']++; + $JUNIT['test_pass']++; + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['result_xml'] .= ''."\n"; + } return 'PASSED'; } } @@ -1994,6 +2091,23 @@ $output if (isset($old_php)) { $php = $old_php; } + + if ($JUNIT) { + $JUNIT['result_xml'] .= ''."\n"; + $JUNIT['test_total']++; + if (in_array('XFAIL', $restype)) { + $JUNIT['test_pass']++; + } + elseif (in_array('FAIL', $restype)) { + $JUNIT['test_fail']++; + $JUNIT['result_xml'] .= ''."\n"; + } + else{ + $JUNIT['test_error']++; + $JUNIT['result_xml'] .= ''."\n"; + } + $JUNIT['result_xml'] .= ''."\n"; + } return $restype[0] . 'ED'; } @@ -2481,6 +2595,20 @@ function show_result($result, $tested, $tested_file, $extra = '', $temp_filename } } +function save_junit_xml() { + global $JUNIT; + $JUNIT['finished_at'] = microtime(true); + $JUNIT['execution_time']= number_format(($JUNIT['finished_at']-$JUNIT['started_at']), 2); + $JUNIT['result_xml'] = ''."\n". + ''."\n". + ''."\n". + $JUNIT['result_xml']. + ''."\n". + ''; + fwrite($JUNIT['fp'], $JUNIT['result_xml']); +} + + /* * Local variables: * tab-width: 4