From ed7259d1125b16df567bfb1195048495ffdc38ca Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 13 May 2012 09:56:57 +0200 Subject: [PATCH] MDL-32923 on failure print hint how to execute the failed test case --- lib/phpunit/classes/hint_resultprinter.php | 67 ++++++++++++++++++++++++++++++ lib/phpunit/classes/util.php | 3 +- phpunit.xml.dist | 2 + 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 lib/phpunit/classes/hint_resultprinter.php diff --git a/lib/phpunit/classes/hint_resultprinter.php b/lib/phpunit/classes/hint_resultprinter.php new file mode 100644 index 00000000000..dccb38638cd --- /dev/null +++ b/lib/phpunit/classes/hint_resultprinter.php @@ -0,0 +1,67 @@ +. + +/** + * Helper test listener. + * + * @package core + * @category phpunit + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +/** + * Helper test listener that prints command necessary + * for execution of failed test. + * + * @package core + * @category phpunit + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class Hint_ResultPrinter extends PHPUnit_TextUI_ResultPrinter { + protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) { + global $CFG; + + parent::printDefectTrace($defect); + + $failedTest = $defect->failedTest(); + $testName = get_class($failedTest); + + $exception = $defect->thrownException(); + $trace = $exception->getTrace(); + $file = null; + $dirroot = realpath($CFG->dirroot).DIRECTORY_SEPARATOR; + $classpath = realpath("$CFG->dirroot/lib/phpunit/classes").DIRECTORY_SEPARATOR; + foreach ($trace as $item) { + if (strpos($item['file'], $dirroot) === 0 and strpos($item['file'], $classpath) !== 0) { + $file = $item['file']; + break; + } + } + if ($file === null) { + return; + } + + $cwd = getcwd(); + if (strpos($file, $cwd) === 0) { + $file = substr($file, strlen($cwd)+1); + } + + $this->write("\n phpunit $testName $file\n"); + } +} diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index 4c7ea7a85c2..a9c54f42ff8 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -1011,8 +1011,7 @@ class phpunit_util { // fix link to schema $level = substr_count(str_replace('\\', '/', $cpath), '/') - substr_count(str_replace('\\', '/', $CFG->dirroot), '/'); - $fcontents = str_replace('lib/phpunit/phpunit.xsd', str_repeat('../', $level).'lib/phpunit/phpunit.xsd', $fcontents); - $fcontents = str_replace('lib/phpunit/bootstrap.php', str_repeat('../', $level).'lib/phpunit/bootstrap.php', $fcontents); + $fcontents = str_replace('lib/phpunit/', str_repeat('../', $level).'lib/phpunit/', $fcontents); // Write the file $result = false; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 82bf8fbaa04..fc9b380e034 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,6 +15,8 @@ stopOnSkipped="false" strict="false" verbose="false" + printerClass="Hint_ResultPrinter" + printerFile="lib/phpunit/classes/hint_resultprinter.php" > -- 2.11.4.GIT