From a93d0168e64a318dc497be14d6005e58a8be538c Mon Sep 17 00:00:00 2001
From: Greg Anderson <greg.1.anderson@greenknowe.org>
Date: Fri, 24 Aug 2012 16:07:26 -0700
Subject: Issue #1255708 by greg.1.anderson, kid_icarus, rickmanelius: Use quick-drupal with drush make

---
 commands/core/core.drush.inc  |   45 ++++++++++++++++++++++-----
 commands/make/make.drush.inc  |    1 +
 tests/COVERAGE.txt            |    3 +-
 tests/makefiles/qd-devel.make |    6 ++++
 tests/quickDrupalTest.php     |   67 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 111 insertions(+), 11 deletions(-)
 create mode 100644 tests/makefiles/qd-devel.make
 create mode 100644 tests/quickDrupalTest.php

diff --git a/commands/core/core.drush.inc b/commands/core/core.drush.inc
index 10a3854..dd2e228 100644
--- a/commands/core/core.drush.inc
+++ b/commands/core/core.drush.inc
@@ -305,6 +305,7 @@ function core_drush_command() {
       'drush qd --profile=minimal --dev --cache --core=drupal-8.x --yes' => 'Fire up dev release of Drupal site with minimal install profile.',
       'drush qd testsite devel --server=:8081/admin --browser=firefox --cache --yes' => 'Fire up stable release (using the cache) of Drupal site called "testsite", download and enable devel module, start a server on port 8081 and open /admin in firefox.',
       'drush qd commercesite --core=commerce_kickstart --profile=commerce_kickstart --cache --yes --watchdog' => 'Download and install the "Commerce Kickstart" distribution/install profile, display watchdog messages on the server console.',
+      'drush qd --makefile=mysite.make' => 'Create and install a site from a makefile.',
     ),
   );
   // Add in options/engines.
@@ -814,9 +815,11 @@ function _drush_core_is_named_in_array($key, $the_array) {
  */
 function drush_core_quick_drupal() {
   $requests = FALSE;
+  $make_projects = array();
   $args = func_get_args();
   $name = drush_get_option('use-name');
   drush_set_option('backend', TRUE);
+  $makefile = drush_get_option('makefile');
   if (drush_get_option('use-existing', FALSE)) {
     $root = drush_get_option('root', FALSE);
     if (!$root) {
@@ -834,18 +837,38 @@ function drush_core_quick_drupal() {
     if (empty($name)) {
       $name = 'quick-drupal-' . gmdate('YmdHis', $_SERVER['REQUEST_TIME']);
     }
-    $base = getcwd() . '/' . $name;
-    drush_set_option('destination', $base);
+    $root = drush_get_option('root', FALSE);
     $core = drush_get_option('core', 'drupal');
-    drush_set_option('drupal-project-rename', $core);
-    if (drush_invoke('pm-download', array($core)) === FALSE) {
-      return drush_set_error('QUICK_DRUPAL_CORE_DOWNLOAD_FAIL', 'Drupal core download/extract failed.');
+    $project_rename = $core;
+    if ($root) {
+      $base = dirname($root);
+      $project_rename = basename($root);
+    }
+    else {
+      $base = getcwd() . '/' . $name;
+      $root = $base . '/' . $core;
+    }
+    if (!empty($makefile)) {
+      // Invoke 'drush make $makefile'.
+      $result = drush_invoke_process('@none', 'make', array($makefile, $root));
+      if ($result['error_status'] != 0) {
+        return drush_set_error('DRUSH_QD_MAKE', 'Could not make; aborting.');
+      }
+      $make_projects = array_diff(array_keys($result['object']['projects']), array('drupal'));
+    }
+    else {
+      drush_mkdir($base);
+      drush_set_option('destination', $base);
+      drush_set_option('drupal-project-rename', $project_rename);
+      if (drush_invoke('pm-download', array($core)) === FALSE) {
+        return drush_set_error('QUICK_DRUPAL_CORE_DOWNLOAD_FAIL', 'Drupal core download/extract failed.');
+      }
     }
-    drush_set_option('root', $base . '/' . $core);
   }
   if (!drush_get_option('db-url', FALSE)) {
     drush_set_option('db-url', 'sqlite:' . $base . '/' . $name . '.sqlite');
   }
+  drush_set_option('root', $root);
   if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_ROOT)) {
     return drush_set_error('QUICK_DRUPAL_ROOT_LOCATE_FAIL', 'Unable to locate Drupal root directory.');
   }
@@ -870,15 +893,17 @@ function drush_core_quick_drupal() {
   if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_LOGIN)) {
     return drush_set_error('QUICK_DRUPAL_INSTALL_FAIL', 'Drupal core install failed.');
   }
-  $enable = pm_parse_arguments(drush_get_option('enable', $requests));
+  $enable = array_merge(pm_parse_arguments(drush_get_option('enable', $requests)), $make_projects);
   if (!empty($enable)) {
     if (drush_invoke('pm-enable', $enable) === FALSE) {
      return drush_set_error('QUICK_DRUPAL_PROJECT_ENABLE_FAIL', 'Project enable failed.');
     }
   }
   drush_print(dt('Login URL: ') . drush_invoke('user-login'));
-  if ($server = drush_get_option('server', '/')) {
-    drush_invoke('runserver', array($server));
+  if (!drush_get_option('no-server', FALSE)) {
+    if ($server = drush_get_option('server', '/')) {
+      drush_invoke('runserver', array($server));
+    }
   }
 }

@@ -901,6 +926,8 @@ function drush_core_quick_drupal_options(&$items) {
     'no-server' => 'Avoid starting runserver (and browser) for the created Drupal site.',
     'browser' => 'Optional name of a browser to open site in. If omitted the OS default browser will be used. Set --no-browser to disable.',
     'use-name' => array('hidden' => TRUE, 'description' => 'Overrides "name" argument.'),
+    'makefile' => array('description' => 'Makefile to use. Makefile must specify which version of Drupal core to build.', 'example-value' => 'mysite.make', 'value' => 'optional'),
+    'root' => array('description' => 'Path to Drupal root.', 'example-value' => '/path/to/root', 'value' => 'optional'),
   );
   $pm = pm_drush_command();
   foreach ($pm['pm-download']['options'] as $option => $description) {
diff --git a/commands/make/make.drush.inc b/commands/make/make.drush.inc
index 848187f..c67bb4a 100644
--- a/commands/make/make.drush.inc
+++ b/commands/make/make.drush.inc
@@ -192,6 +192,7 @@ function drush_make($makefile = NULL, $build_path = NULL) {
       make_prepare_install($build_path);
     }
   }
+  return $info;
 }

 /**
diff --git a/tests/COVERAGE.txt b/tests/COVERAGE.txt
index 86dc403..959190f 100644
--- a/tests/COVERAGE.txt
+++ b/tests/COVERAGE.txt
@@ -33,7 +33,6 @@ version: GOOD. Implicit by testStandaloneScript()
 php-eval: GOOD. Implicitly tested by many tests (e.g. completeTest).
 php-script: GOOD.
 drupal-directory: GOOD
-core-cli: FAIR
 cache-get: GOOD
 cache-set: GOOD
 cache-clear: GOOD
@@ -42,7 +41,7 @@ core-cron
 core-status: FAIR: Implicit test by contextTest.
 docs
 core-rsync
-core-quick-drupal
+core-quick-drupal: GOOD
 image: GOOD
 queue-*: GOOD
 runserver
diff --git a/tests/makefiles/qd-devel.make b/tests/makefiles/qd-devel.make
new file mode 100644
index 0000000..32b76ff
--- /dev/null
+++ b/tests/makefiles/qd-devel.make
@@ -0,0 +1,6 @@
+core = "7.x"
+api = 2
+
+projects[drupal][version] = "7.15"
+defaults[projects][subdir] = "contrib"
+projects[devel] = "1.3"
diff --git a/tests/quickDrupalTest.php b/tests/quickDrupalTest.php
new file mode 100644
index 0000000..bd03ae6
--- /dev/null
+++ b/tests/quickDrupalTest.php
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * core-quick-drupal tests.
+ * @group make
+ * @group slow
+ */
+class quickDrupalCase extends Drush_CommandTestCase {
+  /**
+   * Path to test make files.
+   */
+  protected $makefile_path;
+
+  /**
+   * Initialize $makefile_path.
+   */
+  function __construct() {
+    $this->makefile_path =  dirname(__FILE__) . DIRECTORY_SEPARATOR . 'makefiles';
+  }
+
+  /**
+   * Run a given quick-drupal test.
+   *
+   * @param $test
+   *   The test makefile to run, as defined by $this->getQuickDrupalTestParameters();
+   */
+  private function runQuickDrupalTest($test) {
+    $config = $this->getQuickDrupalTestParameters($test);
+    $default_options = array(
+      'yes' => NULL,
+      'no-server' => NULL,
+    );
+    $options = array_merge($config['options'], $default_options);
+    if (array_key_exists('makefile', $config)) {
+      $makefile = $this->makefile_path . DIRECTORY_SEPARATOR . $config['makefile'];
+      $options['makefile'] = $makefile;
+    }
+    $return = !empty($config['fail']) ? self::EXIT_ERROR : self::EXIT_SUCCESS;
+    $target = UNISH_SANDBOX . '/qd-' . $test;
+    $options['root'] = $target;
+    $this->drush('core-quick-drupal', $config['args'], $options, NULL, NULL, $return);
+
+    // Use pm-list to determine if all of the correct modules were enabled
+    if (empty($config['fail'])) {
+      $this->drush('pm-list', array(), array('root' => $target, 'status' => 'enabled', 'no-core' => NULL, 'pipe' => NULL));
+      $output = $this->getOutput();
+      $this->assertEquals($config['expected-modules'], $output, 'quick-drupal included the correct set of modules');
+    }
+  }
+
+  function testQuickDrupal() {
+    $this->runQuickDrupalTest('devel');
+  }
+
+  function getQuickDrupalTestParameters($key) {
+    static $tests = array(
+      'devel' => array(
+        'name'     => 'Test quick-drupal with a makefile that downloads devel',
+        'makefile' => 'qd-devel.make',
+        'expected-modules' => 'devel',
+        'args' => array(),
+        'options'  => array(),
+      ),
+    );
+    return $tests[$key];
+  }
+}
--
1.7.1

