From fa73cbd8419e5702dd1f6662603117ab4f886343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Tue, 28 Jun 2011 11:15:16 -0400 Subject: [PATCH] 1133284 split up unit from functional tests this is mostly moving code around and renaming the Drush_TestCase class --- tests/backendTest.php | 2 +- tests/commandTest.php | 2 +- tests/contextTest.php | 2 +- tests/coreTest.php | 2 +- tests/drush_testcase.inc | 226 ++++++++++++++++++++++++---------------- tests/fieldTest.php | 2 +- tests/pmDownloadTest.php | 2 +- tests/pmEnDisUnListVarTest.php | 2 +- tests/pmUpdateCodeTest.php | 2 +- tests/shellAliasTest.php | 2 +- tests/siteAliasTest.php | 2 +- tests/siteUpgradeTest.php | 2 +- tests/userTest.php | 2 +- 13 files changed, 149 insertions(+), 101 deletions(-) diff --git a/tests/backendTest.php b/tests/backendTest.php index dbd574f..3411a09 100644 --- a/tests/backendTest.php +++ b/tests/backendTest.php @@ -13,7 +13,7 @@ * - No network calls and thus faster. */ -class backendCase extends Drush_TestCase { +class backendCase extends Drush_CommandTestCase { const DRUSH_BACKEND_OUTPUT_DELIMITER = 'DRUSH_BACKEND_OUTPUT_START>>>%s<<root = $this->sites[$this->env]['root']; diff --git a/tests/coreTest.php b/tests/coreTest.php index 6bef7fb..2dc93af 100644 --- a/tests/coreTest.php +++ b/tests/coreTest.php @@ -4,7 +4,7 @@ * @file * Tests for core commands. */ -class coreCase extends Drush_TestCase { +class coreCase extends Drush_CommandTestCase { /* * Test standalone php-script scripts. Assure that script args and options work. diff --git a/tests/drush_testcase.inc b/tests/drush_testcase.inc index a5eed7b..cca8fb5 100644 --- a/tests/drush_testcase.inc +++ b/tests/drush_testcase.inc @@ -2,14 +2,6 @@ abstract class Drush_TestCase extends PHPUnit_Framework_TestCase { - // Unix exit codes. - const EXIT_SUCCESS = 0; - const EXIT_ERROR = 1; - /* - * An array of Drupal sites that are setup in the drush-sandbox. - */ - var $sites; - function __construct() { $this->_output = false; } @@ -47,6 +39,102 @@ abstract class Drush_TestCase extends PHPUnit_Framework_TestCase { return $arg; } + /** + * Accessor for the last output. + * @return string Output as text. + * @access public + */ + function getOutput() { + return implode("\n", $this->_output); + } + + /** + * Accessor for the last output. + * @return array Output as array of lines. + * @access public + */ + function getOutputAsList() { + return $this->_output; + } + + /** + * Same code as drush_delete_dir(). + * @see drush_delete_dir() + * + * @param string $dir + * @return boolean + */ + function file_delete_recursive($dir) { + if (!file_exists($dir)) { + return TRUE; + } + if (!is_dir($dir)) { + @chmod($dir, 0777); // Make file writeable + return unlink($dir); + } + foreach (scandir($dir) as $item) { + if ($item == '.' || $item == '..') { + continue; + } + if (!self::file_delete_recursive($dir.'/'.$item)) { + return FALSE; + } + } + return rmdir($dir); + } + + function setUpDrupal($env = 'dev', $install = FALSE, $version_string = '7', $profile = NULL) { + $root = UNISH_SANDBOX . '/web'; + $this->sites[$env]['root'] = $root; + $site = "$root/sites/$env"; + if (is_null($profile)) { + $profile = substr($version_string, 0, 1) >= 7 ? 'testing' : 'default'; + } + + // Download Drupal if not already present. + if (!file_exists($root)) { + $options = array( + 'destination' => UNISH_SANDBOX, + 'drupal-project-rename' => 'web', + 'yes' => NULL, + 'quiet' => NULL, + 'cache' => NULL, + ); + $this->drush('pm-download', array("drupal-$version_string"), $options); + } + + // If specified, install Drupal as a multi-site. + if ($install) { + $options = array( + 'root' => $root, + 'db-url' => UNISH_DB_URL . '/unish_' . $env, + 'sites-subdir' => $env, + 'yes' => NULL, + 'quiet' => NULL, + ); + $this->drush('site-install', array($profile), $options); + // Give us our write perms back. + $ret = chmod($site, 0777); + + // Stash the db_url for this site. + $this->sites[$env]['db_url'] = UNISH_DB_URL . '/unish_' . $env; + } + else { + mkdir($site); + touch("$site/settings.php"); + } + } +} + +abstract class Drush_CommandTestCase extends Drush_TestCase { + + // Unix exit codes. + const EXIT_SUCCESS = 0; + const EXIT_ERROR = 1; + /* + * An array of Drupal sites that are setup in the drush-sandbox. + */ + var $sites; /** * Assure that each class starts with an empty sandbox directory. @@ -153,66 +241,6 @@ abstract class Drush_TestCase extends PHPUnit_Framework_TestCase { return $this->execute(implode(' ', $exec)); } - /** - * Accessor for the last output. - * @return string Output as text. - * @access public - */ - function getOutput() { - return implode("\n", $this->_output); - } - - /** - * Accessor for the last output. - * @return array Output as array of lines. - * @access public - */ - function getOutputAsList() { - return $this->_output; - } - - function setUpDrupal($env = 'dev', $install = FALSE, $version_string = '7', $profile = NULL) { - $root = UNISH_SANDBOX . '/web'; - $this->sites[$env]['root'] = $root; - $site = "$root/sites/$env"; - if (is_null($profile)) { - $profile = substr($version_string, 0, 1) >= 7 ? 'testing' : 'default'; - } - - // Download Drupal if not already present. - if (!file_exists($root)) { - $options = array( - 'destination' => UNISH_SANDBOX, - 'drupal-project-rename' => 'web', - 'yes' => NULL, - 'quiet' => NULL, - 'cache' => NULL, - ); - $this->drush('pm-download', array("drupal-$version_string"), $options); - } - - // If specified, install Drupal as a multi-site. - if ($install) { - $options = array( - 'root' => $root, - 'db-url' => UNISH_DB_URL . '/unish_' . $env, - 'sites-subdir' => $env, - 'yes' => NULL, - 'quiet' => NULL, - ); - $this->drush('site-install', array($profile), $options); - // Give us our write perms back. - $ret = chmod($site, 0777); - - // Stash the db_url for this site. - $this->sites[$env]['db_url'] = UNISH_DB_URL . '/unish_' . $env; - } - else { - mkdir($site); - touch("$site/settings.php"); - } - } - // Copied from D7 - profiles/standard/standard.install function create_node_types_php() { $php = " @@ -257,31 +285,51 @@ abstract class Drush_TestCase extends PHPUnit_Framework_TestCase { return $contents; } +} + +/** + * Base class for Drush unit tests + * + * Those tests will run in a bootstrapped Drush environment + * + * This should be ran in separate processes, which the following + * annotation should do in 3.6 and above: + * + * @runTestsInSeparateProcesses + */ +abstract class Drush_UnitTestCase extends Drush_TestCase { + /** - * Same code as drush_delete_dir(). - * @see drush_delete_dir() + * Minimally bootstrap drush * - * @param string $dir - * @return boolean + * This is equivalent to the level DRUSH_BOOTSTRAP_NONE, as we + * haven't run drush_bootstrap() yet. To do anything, you'll need to + * bootstrap to some level using drush_bootstrap(). + * + * @see drush_bootstrap() */ - function file_delete_recursive($dir) { - if (!file_exists($dir)) { - return TRUE; - } - if (!is_dir($dir)) { - @chmod($dir, 0777); // Make file writeable - return unlink($dir); - } - foreach (scandir($dir) as $item) { - if ($item == '.' || $item == '..') { - continue; - } - if (!self::file_delete_recursive($dir.'/'.$item)) { - return FALSE; - } - } - return rmdir($dir); + public static function setUpBeforeClass() { + // mostly taken from drush.php + define('DRUSH_BASE_PATH', dirname(__FILE__).'/../'); + define('DRUSH_REQUEST_TIME', microtime(TRUE)); + + require_once DRUSH_BASE_PATH . '/includes/environment.inc'; + require_once DRUSH_BASE_PATH . '/includes/command.inc'; + require_once DRUSH_BASE_PATH . '/includes/drush.inc'; + require_once DRUSH_BASE_PATH . '/includes/backend.inc'; + require_once DRUSH_BASE_PATH . '/includes/batch.inc'; + require_once DRUSH_BASE_PATH . '/includes/context.inc'; + require_once DRUSH_BASE_PATH . '/includes/sitealias.inc'; + require_once DRUSH_BASE_PATH . '/includes/exec.inc'; + require_once DRUSH_BASE_PATH . '/includes/drupal.inc'; + require_once DRUSH_BASE_PATH . '/includes/output.inc'; + require_once DRUSH_BASE_PATH . '/includes/filesystem.inc'; + require_once DRUSH_BASE_PATH . '/includes/dbtng.inc'; + drush_set_context('argc', $GLOBALS['argc']); + drush_set_context('argv', $GLOBALS['argv']); + drush_set_context('DRUSH_COLUMNS', 80); } + } // We read from globals here because env can be empty and ini did not work in quick test. diff --git a/tests/fieldTest.php b/tests/fieldTest.php index 00414ea..147c4b5 100644 --- a/tests/fieldTest.php +++ b/tests/fieldTest.php @@ -4,7 +4,7 @@ * @file * Tests for field.drush.inc */ -class fieldCase extends Drush_TestCase { +class fieldCase extends Drush_CommandTestCase { public function testField() { $this->setUpDrupal('dev', TRUE); diff --git a/tests/pmDownloadTest.php b/tests/pmDownloadTest.php index 7fcf36c..64c3b41 100644 --- a/tests/pmDownloadTest.php +++ b/tests/pmDownloadTest.php @@ -3,7 +3,7 @@ /** * pm-download testing */ -class pmDownloadCase extends Drush_TestCase { +class pmDownloadCase extends Drush_CommandTestCase { public function testPmDownload() { $this->drush('pm-download', array('devel'), array('cache' => NULL)); $this->assertFileExists(UNISH_SANDBOX . '/devel/README.txt'); diff --git a/tests/pmEnDisUnListVarTest.php b/tests/pmEnDisUnListVarTest.php index d3583c9..9248ea3 100644 --- a/tests/pmEnDisUnListVarTest.php +++ b/tests/pmEnDisUnListVarTest.php @@ -4,7 +4,7 @@ * @file * Tests for enable, disable, uninstall, pm-list, and variable-* commands. */ -class EnDisUnListVarCase extends Drush_TestCase { +class EnDisUnListVarCase extends Drush_CommandTestCase { public function testEnDisUnListVar() { $this->setUpDrupal('dev', TRUE); diff --git a/tests/pmUpdateCodeTest.php b/tests/pmUpdateCodeTest.php index 27a5c2a..2b7c6f3 100644 --- a/tests/pmUpdateCodeTest.php +++ b/tests/pmUpdateCodeTest.php @@ -7,7 +7,7 @@ * @todo test security-only once one of these modules or core gets a security release. */ -class pmUpdateCode extends Drush_TestCase { +class pmUpdateCode extends Drush_CommandTestCase { /* * Download old core and older contrib releases which will always need updating. diff --git a/tests/shellAliasTest.php b/tests/shellAliasTest.php index 5a186f7..ce67ff7 100644 --- a/tests/shellAliasTest.php +++ b/tests/shellAliasTest.php @@ -4,7 +4,7 @@ * @file * Tests for Shell aliases. */ -class shellAliasesCase extends Drush_TestCase { +class shellAliasesCase extends Drush_CommandTestCase { /* * Assure that shell aliases expand as expected. diff --git a/tests/siteAliasTest.php b/tests/siteAliasTest.php index dcbb132..9d1550b 100644 --- a/tests/siteAliasTest.php +++ b/tests/siteAliasTest.php @@ -4,7 +4,7 @@ * @file * Tests for sitealias.inc */ -class saCase extends Drush_TestCase { +class saCase extends Drush_CommandTestCase { /* * Assure that site lists work as expected. diff --git a/tests/siteUpgradeTest.php b/tests/siteUpgradeTest.php index b243573..4ac6412 100644 --- a/tests/siteUpgradeTest.php +++ b/tests/siteUpgradeTest.php @@ -12,7 +12,7 @@ * - updatedb and batch.inc */ -class siteUpgradeCase extends Drush_TestCase { +class siteUpgradeCase extends Drush_CommandTestCase { function testUpgrade() { $env = 'testupgrade'; $this->setUpDrupal($env, TRUE, '6'); diff --git a/tests/userTest.php b/tests/userTest.php index a3defbe..da07d04 100644 --- a/tests/userTest.php +++ b/tests/userTest.php @@ -4,7 +4,7 @@ * @file * Tests for user.drush.inc */ -class userCase extends Drush_TestCase { +class userCase extends Drush_CommandTestCase { /* * Create, edit, block, and cancel users. -- 1.7.2.5