Index: commands/simpletest/simpletest.drush.inc =================================================================== RCS file: /cvs/drupal/contributions/modules/drush/commands/simpletest/simpletest.drush.inc,v retrieving revision 1.1 diff -u -p -r1.1 simpletest.drush.inc --- commands/simpletest/simpletest.drush.inc 4 Apr 2009 02:02:40 -0000 1.1 +++ commands/simpletest/simpletest.drush.inc 17 Sep 2009 17:51:43 -0000 @@ -10,10 +10,12 @@ */ function simpletest_drush_help($section) { switch ($section) { + case 'drush:test': + return dt("Run a specified test and output the results on the command line."); case 'drush:test mail': return dt("Run tests and email the results. See the docs for run-tests.sh to understand --extra."); - case 'drush:test clean': - return dt("Clean leftover tables and file directories from prior test runs."); + case 'drush:test clean': + return dt("Clean leftover tables and file directories from prior test runs."); } } @@ -21,6 +23,19 @@ function simpletest_drush_help($section) * Implementation of hook_drush_command(). */ function simpletest_drush_command() { + $items['test'] = array( + 'callback' => 'drush_test_one', + 'description' => 'Run a specified test and outputs the results on the command line.', + 'examples' => array( + "drush test BlockTestCase" => "Run BlockTestCase and output results on the command line.", + ), + 'arguments' => array( + 'test_class' => 'The test case you want to run.', + ), + 'drupal dependencies' => array('simpletest'), + 'core' => array('6','7'), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, + ); $items['test mail'] = array( 'callback' => 'drush_test_mail', 'description' => 'Run all tests and mail the results to your team.', @@ -53,6 +68,56 @@ function drush_test_clean() { drush_print("Simpletest environment successfully cleaned."); } +// A Drush command callback. +function drush_test_one($test_class) { + // This function doesn't exist in older versions of simpletest. For now they are not supported. + $classes = simpletest_test_get_all_classes(); + + // Check that the test class parameter has been set. + if (!$test_class) { + $table = array(array(t('Available test cases'))); + foreach ($classes as $class => $discard) { + $table[] = array($class); + } + drush_print_table($table, TRUE); + return; + } + + // Check the parameter is valid. + if (!$classes[$test_class]) { + drush_set_error('simpletest test case does not exist', t("Test case !case not found. Perhaps you spelled it wrong or need to enable the module?\nUse the drush test command with no arguments to see a list of available test cases. E.g.\n\n\tdrush test\n", array('!case' => $test_class))); + return; + } + + // Run tests. We need an ID to get be able to retrieve errors. + db_query('INSERT INTO {simpletest_test_id} (test_id) VALUES (default)'); + $id = db_last_insert_id('simpletest_test_id', 'test_id'); + $test = new $test_class($id); + $test->run(); + + // t() variables. + $vars = array( + '!passes' => $test->results['#pass'], + '!fails' => $test->results['#fail'], + ); + + // Just print a summary if there were no fails. + if (!$test->results['#fail']) { + return t('!passes tests passed.', $vars); + } + + // Print detailed results if there were fails. + module_load_include('pages.inc', 'simpletest'); + $table = array(array(t('Status'), t('Message'))); + $results = simpletest_result_get($id); + foreach ($results[$test_class] as $result) { + $status = $result->status == 'fail' ? 'FAIL' : $result->status; + $row = array($status, $result->message); + $table[] = $row; + } + $table = drush_print_table($table, TRUE); + $message = t('!fails tests FAILED. !passes tests passed', $vars); +} // A Drush command callback. function drush_test_mail($recipients) { @@ -73,7 +138,7 @@ function drush_test_mail($recipients) { // Copied from run-tests.sh function drush_simpletest_find_php() { - // Determine location of php command automatically, unless a comamnd line argument is supplied. + // Determine location of php command automatically, unless a command line argument is supplied. if (!$php = drush_get_option('php')) { if (!empty($_ENV['_'])) { // '_' is an environment variable set by the shell. It contains the command that was executed.