From cc1c4de8dc626354badfa8c35a977042b5c619ee Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Wed, 7 Oct 2020 20:41:44 +0200 Subject: [PATCH] MDL-67673 phpunit: Remove the rerun hint on failed test cases That custom printer was using some hacks to be able to intercept configuration switches, reflection and other tricks to be able to print the: To re-run: vendor/bin/phpunit --verbose "core_setuplib_testcase" lib/tests/setuplib_test.php line on every failed/exceptional/skipped test. After some internal discussion it was agreed that the normal phpunit output: 1) core_setuplib_testcase::test_localcachedir Time is lower that allowed start value Failed asserting that 1601976686 is equal to 1601976687 or is greater than 1601976687. /var/www/html/lib/phpunit/classes/advanced_testcase.php:446 /var/www/html/lib/tests/setuplib_test.php:170 /var/www/html/lib/phpunit/classes/advanced_testcase.php:80 has already all the information at hand about how to rerun a test: - vendor/bin/phpunit lib/tests/setuplib_test.php - vendor/bin/phpunit --filter core_setuplib_testcase::test_localcachedir - vendor/bin/phpunit --filter ::test_localcachedir - vendor/bin/phpunit --testsuite componentname_testsuite - vendor/bin/phpunit --config <> - use --cache-result to get failed tests rerun with ease. - ... So better, let's use standard phpunit output and done. Also, note that, with the upgrade to phpunit 8.5, the printer was not working correctly any more, causing some switches, like --verbose ... to be ignored. Sure it could have been fixed but, as commented above, no real need for all that "parapheranlia" to print the rerun information. --- lib/phpunit/classes/hint_resultprinter.php | 148 --------------------- lib/phpunit/lib.php | 1 - phpunit.xml.dist | 1 - 3 files changed, 150 deletions(-) delete mode 100644 lib/phpunit/classes/hint_resultprinter.php diff --git a/lib/phpunit/classes/hint_resultprinter.php b/lib/phpunit/classes/hint_resultprinter.php deleted file mode 100644 index 228db3e10a9..00000000000 --- a/lib/phpunit/classes/hint_resultprinter.php +++ /dev/null @@ -1,148 +0,0 @@ -. - -/** - * 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 { - public function __construct() { - // ARRGH - PHPUnit does not give us commandline arguments or xml config, so let's hack hard! - if (defined('DEBUG_BACKTRACE_PROVIDE_OBJECT')) { - $backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT); - if (isset($backtrace[2]['object']) and ($backtrace[2]['object'] instanceof PHPUnit\TextUI\Command)) { - list($verbose, $colors, $debug) = Hacky_TextUI_Command_reader::get_settings_hackery($backtrace[2]['object']); - parent::__construct(null, $verbose, $colors, $debug); - return; - } - } - // Fallback if something goes wrong. - parent::__construct(null, false, self::COLOR_DEFAULT, false); - } - - protected function printDefectTrace(PHPUnit\Framework\TestFailure $defect): void { - global $CFG; - - parent::printDefectTrace($defect); - - $failedTest = $defect->failedTest(); - $testName = get_class($failedTest); - - $exception = $defect->thrownException(); - $trace = $exception->getTrace(); - - if (class_exists('ReflectionClass')) { - $reflection = new ReflectionClass($testName); - $file = $reflection->getFileName(); - - } else { - $file = false; - $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) { - if ($content = file_get_contents($item['file'])) { - if (preg_match('/class\s+'.$testName.'\s+extends/', $content)) { - $file = $item['file']; - break; - } - } - } - } - } - - if ($file === false) { - return; - } - - $cwd = getcwd(); - if (strpos($file, $cwd) === 0) { - $file = substr($file, strlen($cwd)+1); - $file = testing_cli_fix_directory_separator($file); - } - - $pathprefix = testing_cli_argument_path('/'); - if ($pathprefix) { - $pathprefix .= DIRECTORY_SEPARATOR; - } - - // There is only vendor/bin/phpunit executable. There is no .cmd or .bat files. - $executable = $pathprefix . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'phpunit'; - $executable = testing_cli_fix_directory_separator($executable); - - // Add server arguments to the rerun if passed. - if (isset($_SERVER['argv'][0])) { - if (preg_match('/phpunit(\.bat|\.cmd)?$/', $_SERVER['argv'][0])) { - for($i=1;$iwrite("\nTo re-run:\n $executable \"$testName\" $file\n"); - } -} - - -/** - * Class used in bloody hack that works around result printer constructor troubles. - * - * @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 Hacky_TextUI_Command_reader extends PHPUnit\TextUI\Command { - public static function get_settings_hackery(PHPUnit\TextUI\Command $toread) { - $arguments = $toread->arguments; - $config = PHPUnit\Util\Configuration::getInstance($arguments['configuration'])->getPHPUnitConfiguration(); - - $verbose = isset($config['verbose']) ? $config['verbose'] : false; - $verbose = isset($arguments['verbose']) ? $arguments['verbose'] : $verbose; - - $colors = isset($config['colors']) ? $config['colors'] : Hint_ResultPrinter::COLOR_DEFAULT; - $colors = isset($arguments['colors']) ? $arguments['colors'] : $colors; - - $debug = isset($config['debug']) ? $config['debug'] : false; - $debug = isset($arguments['debug']) ? $arguments['debug'] : $debug; - - return array($verbose, $colors, $debug); - } -} diff --git a/lib/phpunit/lib.php b/lib/phpunit/lib.php index 3396c7b5a19..d8ac8b78991 100644 --- a/lib/phpunit/lib.php +++ b/lib/phpunit/lib.php @@ -35,7 +35,6 @@ require_once(__DIR__.'/classes/base_testcase.php'); require_once(__DIR__.'/classes/basic_testcase.php'); require_once(__DIR__.'/classes/database_driver_testcase.php'); require_once(__DIR__.'/classes/advanced_testcase.php'); -require_once(__DIR__.'/classes/hint_resultprinter.php'); // Loaded here because phpunit.xml does not support relative links for printerFile. require_once(__DIR__.'/classes/constraint_object_is_equal_with_exceptions.php'); require_once(__DIR__.'/../testing/classes/test_lock.php'); require_once(__DIR__.'/../testing/classes/tests_finder.php'); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2e84bf29166..e12def32e83 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,7 +16,6 @@ stopOnSkipped="false" beStrictAboutTestsThatDoNotTestAnything="false" beStrictAboutOutputDuringTests="true" - printerClass="Hint_ResultPrinter" testSuiteLoaderClass="phpunit_autoloader" >