MDL-43450 behat: Improving error messages
authorDavid Monllao <davidm@moodle.com>
Fri, 10 Jan 2014 08:44:23 +0000 (16:44 +0800)
committerDavid Monllao <davidm@moodle.com>
Fri, 24 Jan 2014 08:45:27 +0000 (16:45 +0800)
admin/tool/behat/lang/en/tool_behat.php
admin/tool/behat/renderer.php
lib/behat/classes/behat_command.php

index d8b2b51..04e034c 100644 (file)
  * @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:<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>';
index 324241b..cc5b4ab 100644 (file)
@@ -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;
+    }
+
 }
index 76a4d25..5013e39 100644 (file)
@@ -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') . '<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;
         }
     }