cvs: pear-core /PEAR RunTest.php

From: Date: Mon, 05 Jul 2004 01:13:49 +0000
Subject: cvs: pear-core /PEAR RunTest.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-21513@lists.php.net to get a copy of this message
cox Sun Jul 4 21:13:49 2004 EDT Added files: /pear-core/PEAR RunTest.php Log: Experimental version of a simplified PHP run-test.php

http://cvs.php.net/co.php/pear-core/PEAR/RunTest.php?r=1.1&p=1 Index: pear-core/PEAR/RunTest.php +++ pear-core/PEAR/RunTest.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 5 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at the following url: | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Tomas V.V.Cox <cox@idecnet.com> | // | | // +----------------------------------------------------------------------+ // // $Id: RunTest.php,v 1.1 2004/07/05 01:13:49 cox Exp $ // /** * Simplified version of PHP's test suite * -- EXPERIMENTAL -- Try it with: $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);' TODO: Actually finish the development and testing */ require_once 'PEAR.php'; require_once 'PEAR/Config.php'; define('DETAILED', 1); class PEAR_RunTest { // // Run an individual test case. // function run($file) { $cwd = getcwd(); putenv('PHP_PEAR_RUNTESTS=1'); $conf = &PEAR_Config::singleton(); $php = $conf->get('php_bin'); //var_dump($php);exit; global $log_format, $info_params, $ini_overwrites; $info_params = ''; $log_format = 'LEOD'; // Load the sections of the test file. $section_text = array( 'TEST' => '(unnamed test)', 'SKIPIF' => '', 'GET' => '', 'ARGS' => '', ); if (!$fp = fopen($file, "r")) { return PEAR::raiseError("Cannot open test file: $file"); } $section = ''; while (!feof($fp)) { $line = fgets($fp); // Match the beginning of a section. if (ereg('^--([A-Z]+)--',$line,$r)) { $section = $r[1]; $section_text[$section] = ''; continue; } // Add to the section text. $section_text[$section] .= $line; } fclose($fp); $shortname = str_replace($cwd.'/', '', $file); $tested = trim($section_text['TEST'])." [$shortname]"; $tmp = realpath(dirname($file)); $tmp_skipif = $tmp . uniqid('/phpt.'); $tmp_file = ereg_replace('\.phpt$','.php',$file); $tmp_post = $tmp . uniqid('/phpt.'); @unlink($tmp_skipif); @unlink($tmp_file); @unlink($tmp_post); // unlink old test results @unlink(ereg_replace('\.phpt$','.diff',$file)); @unlink(ereg_replace('\.phpt$','.log',$file)); @unlink(ereg_replace('\.phpt$','.exp',$file)); @unlink(ereg_replace('\.phpt$','.out',$file)); // Check if test should be skipped. $info = ''; $warn = false; if (array_key_exists('SKIPIF', $section_text)) { if (trim($section_text['SKIPIF'])) { $this->save_text($tmp_skipif, $section_text['SKIPIF']); //$extra = substr(PHP_OS, 0, 3) !== "WIN" ? // "unset REQUEST_METHOD;": ""; //$output = `$extra $php $info_params -f $tmp_skipif`; $output = `$php $info_params -f $tmp_skipif`; unlink($tmp_skipif); if (eregi("^skip", trim($output))) { echo "SKIP $tested"; $reason = (eregi("^skip[[:space:]]*(.+)\$", trim($output))) ? eregi_replace("^skip[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE; if ($reason) { echo " (reason: $reason)\n"; } else { echo "\n"; } if (isset($old_php)) { $php = $old_php; } return 'SKIPPED'; } if (eregi("^info", trim($output))) { $reason = (ereg("^info[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^info[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE; if ($reason) { $info = " (info: $reason)"; } } if (eregi("^warn", trim($output))) { $reason = (ereg("^warn[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^warn[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE; if ($reason) { $warn = true; /* only if there is a reason */ $info = " (warn: $reason)"; } } } } // We've satisfied the preconditions - run the test! $this->save_text($tmp_file,$section_text['FILE']); $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : ''; $ini_settings=''; $cmd = "$php$ini_settings -f $tmp_file$args 2>&1"; $out = `$cmd`; @unlink($tmp_post); // Does the output match what is expected? $output = trim($out); $output = preg_replace('/\r\n/', "\n", $output); if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) { if (isset($section_text['EXPECTF'])) { $wanted = trim($section_text['EXPECTF']); } else { $wanted = trim($section_text['EXPECTREGEX']); } $wanted_re = preg_replace('/\r\n/',"\n",$wanted); if (isset($section_text['EXPECTF'])) { $wanted_re = preg_quote($wanted_re, '/'); // Stick to basics $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re); $wanted_re = str_replace("%d", "[0-9]+", $wanted_re); $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re); $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re); $wanted_re = str_replace("%c", ".", $wanted_re); // %f allows two points "-.0.0" but that is the best *simple* expression } /* DEBUG YOUR REGEX HERE var_dump($wanted_re); print(str_repeat('=', 80) . "\n"); var_dump($output); */ if (preg_match("/^$wanted_re\$/s", $output)) { @unlink($tmp_file); echo "PASS $tested$info\n"; if (isset($old_php)) { $php = $old_php; } return 'PASSED'; } } else { $wanted = trim($section_text['EXPECT']); $wanted = preg_replace('/\r\n/',"\n",$wanted); // compare and leave on success $ok = (0 == strcmp($output,$wanted)); if ($ok) { @unlink($tmp_file); echo "PASS $tested$info\n"; if (isset($old_php)) { $php = $old_php; } return 'PASSED'; } } // Test failed so we need to report details. if ($warn) { echo "WARN $tested$info\n"; } else { echo "FAIL $tested$info\n"; } $GLOBALS['__PHP_FAILED_TESTS__'][] = array( 'name' => $file, 'test_name' => $tested, 'output' => ereg_replace('\.phpt$','.log', $file), 'diff' => ereg_replace('\.phpt$','.diff', $file), 'info' => $info ); // write .exp if (strpos($log_format,'E') !== FALSE) { $logname = ereg_replace('\.phpt$','.exp',$file); $log = fopen($logname,'w') or error("Cannot create test log - $logname"); fwrite($log,$wanted); fclose($log); } // write .out if (strpos($log_format,'O') !== FALSE) { $logname = ereg_replace('\.phpt$','.out',$file); $log = fopen($logname,'w') or error("Cannot create test log - $logname"); fwrite($log,$output); fclose($log); } // write .diff if (strpos($log_format,'D') !== FALSE) { $logname = ereg_replace('\.phpt$','.diff',$file); $log = fopen($logname,'w') or error("Cannot create test log - $logname"); fwrite($log, $this->generate_diff($wanted,$output)); fclose($log); } // write .log if (strpos($log_format,'L') !== FALSE) { $logname = ereg_replace('\.phpt$','.log',$file); $log = fopen($logname,'w') or error("Cannot create test log - $logname"); fwrite($log," ---- EXPECTED OUTPUT $wanted ---- ACTUAL OUTPUT $output ---- FAILED "); fclose($log); //error_report($file,$logname,$tested); } if (isset($old_php)) { $php = $old_php; } return $warn ? 'WARNED' : 'FAILED'; } function generate_diff($wanted,$output) { $w = explode("\n", $wanted); $o = explode("\n", $output); $w1 = array_diff_assoc($w,$o); $o1 = array_diff_assoc($o,$w); $w2 = array(); $o2 = array(); foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1).$val; foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1).$val; $diff = array_merge($w2, $o2); ksort($diff); return implode("\r\n", $diff); } function error($message) { echo "ERROR: {$message}\n"; exit(1); } // // Write the given text to a temporary file, and return the filename. // function save_text($filename, $text) { $fp = fopen($filename, 'w') or error("Cannot open file '" . $filename . "' (save_text)"); fwrite($fp,$text); fclose($fp); if (1 < DETAILED) echo " FILE $filename {{{ $text }}} "; } } function error($message) { echo "ERROR: {$message}\n"; exit(1); } ?>
« previous php.pear.cvs (#21513) next »