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

---
 commands/core/core.drush.inc |   21 +++++++++++--
 tests/quickDrupalTest.php    |   66 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 tests/quickDrupalTest.php

diff --git a/commands/core/core.drush.inc b/commands/core/core.drush.inc
index 10a3854..64eb9b8 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.
@@ -817,6 +818,7 @@ function drush_core_quick_drupal() {
   $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) {
@@ -835,13 +837,23 @@ function drush_core_quick_drupal() {
       $name = 'quick-drupal-' . gmdate('YmdHis', $_SERVER['REQUEST_TIME']);
     }
     $base = getcwd() . '/' . $name;
-    drush_set_option('destination', $base);
     $core = drush_get_option('core', 'drupal');
+    $root = $base . '/' . $core;
+    drush_set_option('destination', $base);
     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.');
+    drush_set_option('root', $root);
+    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.');
+      }
+    }
+    else {
+      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');
@@ -901,6 +913,7 @@ 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'),
   );
   $pm = pm_drush_command();
   foreach ($pm['pm-download']['options'] as $option => $description) {
diff --git a/tests/quickDrupalTest.php b/tests/quickDrupalTest.php
new file mode 100644
index 0000000..594da01
--- /dev/null
+++ b/tests/quickDrupalTest.php
@@ -0,0 +1,66 @@
+<?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(
+      '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;
+    // TODO: control the location / name of the Drupal site that will be built
+    $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'])) {
+      // TODO: select the Drupal site that was built
+      $this->drush('pm-list', array(), array('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' => 'defaults.make',
+        'expected-modules' => 'TODO: Fill in passing output',
+        'args' => array(),
+        'options'  => array(),
+      ),
+    );
+    return $tests[$key];
+  }
+}
-- 
1.7.1

