From b8c4d91ca04c0cf79e77d47d8cfb7f40a71b6364 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 10 Jan 2014 16:44:23 +0800 Subject: [PATCH] MDL-43450 behat: Improving error messages --- admin/tool/behat/lang/en/tool_behat.php | 15 ++-- admin/tool/behat/renderer.php | 103 +++++++++++++++++------- lib/behat/classes/behat_command.php | 55 +++++++------ 3 files changed, 108 insertions(+), 65 deletions(-) diff --git a/admin/tool/behat/lang/en/tool_behat.php b/admin/tool/behat/lang/en/tool_behat.php index d8b2b51409d..04e034c1b43 100644 --- a/admin/tool/behat/lang/en/tool_behat.php +++ b/admin/tool/behat/lang/en/tool_behat.php @@ -22,17 +22,19 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string['aim'] = 'This administration tool helps developers and test writers to create .feature files describing Moodle\'s functionalities and run them automatically.'; +$string['aim'] = 'This administration tool helps developers and test writers to create .feature files describing Moodle\'s functionalities and run them automatically. Step definitions available for use in .feature files are listed below.'; $string['allavailablesteps'] = 'All the available steps definitions'; +$string['errorbehatcommand'] = 'Error running behat CLI command. Try running "{$a} --help" manually from CLI to find out more about the problem.'; +$string['errorcomposer'] = 'Composer dependencies are not installed.'; +$string['errordataroot'] = '$CFG->behat_dataroot is not set or is invalid.'; +$string['errorsetconfig'] = '$CFG->behat_dataroot, $CFG->behat_prefix and $CFG->behat_wwwroot need to be set in config.php.'; $string['giveninfo'] = 'Given. Processes to set up the environment'; $string['infoheading'] = 'Info'; $string['installinfo'] = 'Read {$a} for installation and tests execution info'; -$string['moreinfoin'] = 'More info in {$a}'; $string['newstepsinfo'] = 'Read {$a} for info about how to add new steps definitions'; $string['newtestsinfo'] = 'Read {$a} for info about how to write new tests'; $string['nostepsdefinitions'] = 'There aren\'t steps definitions matching this filters'; $string['pluginname'] = 'Acceptance testing'; -$string['runclitool'] = 'To list the steps definitions you need to run the Behat CLI tool to create the $CFG->behat_dataroot directory. Go to your moodle dirroot and run "{$a}"'; $string['stepsdefinitionscomponent'] = 'Area'; $string['stepsdefinitionscontains'] = 'Contains'; $string['stepsdefinitionsfilters'] = 'Steps definitions'; @@ -41,6 +43,7 @@ $string['theninfo'] = 'Then. Checkings to ensure the outcomes are the expected o $string['unknownexceptioninfo'] = 'There was a problem with Selenium or your browser. Please ensure you are using the latest version of Selenium. Error:'; $string['viewsteps'] = 'Filter'; $string['wheninfo'] = 'When. Actions that provokes an event'; -$string['wrongbehatsetup'] = 'Something is wrong with behat setup, ensure:'; +$string['wrongbehatsetup'] = 'Something is wrong with the behat setup and so step definitions cannot be listed: {$a->errormsg}

Please check:'; diff --git a/admin/tool/behat/renderer.php b/admin/tool/behat/renderer.php index 324241b9da1..cc5b4ab9dde 100644 --- a/admin/tool/behat/renderer.php +++ b/admin/tool/behat/renderer.php @@ -45,37 +45,7 @@ class tool_behat_renderer extends plugin_renderer_base { */ public function render_stepsdefinitions($stepsdefinitions, $form) { - $title = get_string('pluginname', 'tool_behat'); - - // Header. - $html = $this->output->header(); - $html .= $this->output->heading($title); - - // Info. - $installurl = behat_command::DOCS_URL . '#Installation'; - $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank')); - $writetestsurl = behat_command::DOCS_URL . '#Writting_features'; - $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank')); - $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions'; - $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank')); - $infos = array( - get_string('installinfo', 'tool_behat', $installlink), - get_string('newtestsinfo', 'tool_behat', $writetestslink), - get_string('newstepsinfo', 'tool_behat', $writestepslink) - ); - - // List of steps. - $html .= $this->output->box_start(); - $html .= html_writer::tag('h1', get_string('infoheading', 'tool_behat')); - $html .= html_writer::tag('div', get_string('aim', 'tool_behat')); - $html .= html_writer::empty_tag('div'); - $html .= html_writer::empty_tag('ul'); - $html .= html_writer::empty_tag('li'); - $html .= implode(html_writer::end_tag('li') . html_writer::empty_tag('li'), $infos); - $html .= html_writer::end_tag('li'); - $html .= html_writer::end_tag('ul'); - $html .= html_writer::end_tag('div'); - $html .= $this->output->box_end(); + $html = $this->generic_info(); // Form. ob_start(); @@ -123,4 +93,75 @@ class tool_behat_renderer extends plugin_renderer_base { return $html; } + + /** + * Renders an error message adding the generic info about the tool purpose and setup. + * + * @param string $msg The error message + * @return string HTML + */ + public function render_error($msg) { + + $html = $this->generic_info(); + + $a = new stdClass(); + $a->errormsg = $msg; + $a->behatcommand = behat_command::get_behat_command(); + $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR . + 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php'; + + $msg = get_string('wrongbehatsetup', 'tool_behat', $a); + + // Error box including generic error string + specific error msg. + $html .= $this->output->box_start('box errorbox'); + $html .= html_writer::tag('div', $msg); + $html .= $this->output->box_end(); + + $html .= $this->output->footer(); + + return $html; + } + + /** + * Generic info about the tool. + * + * @return string + */ + protected function generic_info() { + + $title = get_string('pluginname', 'tool_behat'); + + // Header. + $html = $this->output->header(); + $html .= $this->output->heading($title); + + // Info. + $installurl = behat_command::DOCS_URL . '#Installation'; + $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank')); + $writetestsurl = behat_command::DOCS_URL . '#Writting_features'; + $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank')); + $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions'; + $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank')); + $infos = array( + get_string('installinfo', 'tool_behat', $installlink), + get_string('newtestsinfo', 'tool_behat', $writetestslink), + get_string('newstepsinfo', 'tool_behat', $writestepslink) + ); + + // List of steps. + $html .= $this->output->box_start(); + $html .= html_writer::tag('h1', get_string('infoheading', 'tool_behat')); + $html .= html_writer::tag('div', get_string('aim', 'tool_behat')); + $html .= html_writer::empty_tag('div'); + $html .= html_writer::empty_tag('ul'); + $html .= html_writer::empty_tag('li'); + $html .= implode(html_writer::end_tag('li') . html_writer::empty_tag('li'), $infos); + $html .= html_writer::end_tag('li'); + $html .= html_writer::end_tag('ul'); + $html .= html_writer::end_tag('div'); + $html .= $this->output->box_end(); + + return $html; + } + } diff --git a/lib/behat/classes/behat_command.php b/lib/behat/classes/behat_command.php index 76a4d258699..5013e39e7c8 100644 --- a/lib/behat/classes/behat_command.php +++ b/lib/behat/classes/behat_command.php @@ -114,8 +114,7 @@ class behat_command { /** * Checks if behat is set up and working * - * Uses notice() instead of behat_error() because is - * also called from web interface + * Notifies failures both from CLI and web interface. * * It checks behat dependencies have been installed and runs * the behat help command to ensure it works as expected @@ -125,24 +124,11 @@ class behat_command { public static function behat_setup_problem() { global $CFG; - $clibehaterrorstr = "Behat dependencies not installed. Ensure you ran the composer installer. " . self::DOCS_URL . "#Installation\n"; - // Moodle setting. if (!self::are_behat_dependencies_installed()) { - - // With HTML. - if (!CLI_SCRIPT) { - - $msg = get_string('wrongbehatsetup', 'tool_behat'); - $docslink = self::DOCS_URL . '#Installation'; - $docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank')); - $msg .= get_string('moreinfoin', 'tool_behat', $docslink); - } else { - $msg = $clibehaterrorstr; - } - - self::output_msg($msg); + // Returning composer error code to avoid conflicts with behat and moodle error codes. + self::output_msg(get_string('errorcomposer', 'tool_behat')); return BEHAT_EXITCODE_COMPOSER; } @@ -150,22 +136,23 @@ class behat_command { list($output, $code) = self::run(' --help'); if ($code != 0) { + // Returning composer error code to avoid conflicts with behat and moodle error codes. - if (!CLI_SCRIPT) { - $msg = get_string('wrongbehatsetup', 'tool_behat'); - } else { - $msg = $clibehaterrorstr; - } - self::output_msg($msg); + self::output_msg(get_string('errorbehatcommand', 'tool_behat', self::get_behat_command())); return BEHAT_EXITCODE_COMPOSER; } + if (empty($CFG->behat_dataroot) || empty($CFG->behat_prefix) || empty($CFG->behat_wwwroot)) { + self::output_msg(get_string('errorsetconfig', 'tool_behat')); + return BEHAT_EXITCODE_CONFIG; + } + // Checking behat dataroot existence otherwise echo about admin/tool/behat/cli/init.php. if (!empty($CFG->behat_dataroot)) { $CFG->behat_dataroot = realpath($CFG->behat_dataroot); } if (empty($CFG->behat_dataroot) || !is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) { - self::output_msg(get_string('runclitool', 'tool_behat', 'php admin/tool/behat/cli/init.php')); + self::output_msg(get_string('errordataroot', 'tool_behat')); return BEHAT_EXITCODE_CONFIG; } @@ -193,13 +180,25 @@ class behat_command { * @return void */ protected static function output_msg($msg) { + global $CFG, $PAGE; + // If we are using the web interface we want pretty messages. if (!CLI_SCRIPT) { - // General info about the tool purpose. - $msg = get_string('aim', 'tool_behat') . '

' . $msg; - notice($msg); + + $renderer = $PAGE->get_renderer('tool_behat'); + echo $renderer->render_error($msg); + + // Stopping execution. + exit(1); + } else { - echo $msg; + + // We continue execution after this. + $clibehaterrorstr = "Ensure you set \$CFG->behat_* vars in config.php " . + "and you ran admin/tool/behat/cli/init.php.\n" . + "More info in " . self::DOCS_URL . "#Installation\n\n"; + + echo 'Error: ' . $msg . "\n\n" . $clibehaterrorstr; } }