Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
MDL-43450 behat: Improving error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Jan 24, 2014
1 parent 9788e26 commit b8c4d91
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 65 deletions.
15 changes: 9 additions & 6 deletions admin/tool/behat/lang/en/tool_behat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -41,6 +43,7 @@
$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:<ul>
<li>You ran "php admin/tool/behat/cli/init.php" from your moodle root directory</li>
<li>vendor/bin/behat file has execution permissions</li></ul>';
$string['wrongbehatsetup'] = 'Something is wrong with the behat setup and so step definitions cannot be listed: <b>{$a->errormsg}</b><br/><br/>Please check:<ul>
<li>$CFG->behat_dataroot, $CFG->behat_prefix and $CFG->behat_wwwroot are set in config.php with different values from $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.</li>
<li>You ran "{$a->behatinit}" from your Moodle root directory.</li>
<li>Dependencies are installed in vendor/ and {$a->behatcommand} file has execution permissions.</li></ul>';
103 changes: 72 additions & 31 deletions admin/tool/behat/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -123,4 +93,75 @@ function($matches){

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;
}

}
55 changes: 27 additions & 28 deletions lib/behat/classes/behat_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public final static function run($options = '') {
/**
* 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
Expand All @@ -125,47 +124,35 @@ public final static function run($options = '') {
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;
}

// Behat test 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;
}

Expand Down Expand Up @@ -193,13 +180,25 @@ public static function are_behat_dependencies_installed() {
* @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') . '<br /><br />' . $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;
}
}

Expand Down

0 comments on commit b8c4d91

Please sign in to comment.