Skip to content

Commit

Permalink
Set backend results and add tests for annotated commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed May 2, 2016
1 parent 5ca898f commit 4d110d2
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 6 deletions.
23 changes: 17 additions & 6 deletions includes/preflight.inc
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,13 @@ function drush_init_dependency_injection_container($input = null, $output = null
->withArgument(\Drush::getVersion())
->withMethodCall('setDispatcher', ['eventDispatcher'])
->withMethodCall('setAutoExit', [false]);
$container->share('commandDiscovery', 'Consolidation\AnnotatedCommand\CommandFileDiscovery')
->withMethodCall('addSearchLocation', ['CommandFiles'])
->withMethodCall('setSearchPattern', ['#.*(Commands|CommandFile).php$#']);
$container->share('commandDiscovery', 'Consolidation\AnnotatedCommand\CommandFileDiscovery');
// ->withMethodCall('addSearchLocation', ['CommandFiles'])
// ->withMethodCall('setSearchPattern', ['#.*(Commands|CommandFile).php$#']);
$container->share('formatterManager', 'Consolidation\OutputFormatters\FormatterManager');
$container->share('hookManager', 'Consolidation\AnnotatedCommand\Hooks\HookManager');
$container->share('backendResultSetter', 'Drush\Backend\BackendResultSetter');
$container->share('hookManager', 'Consolidation\AnnotatedCommand\Hooks\HookManager')
->withMethodCall('addOutputExtractor', ['backendResultSetter']);
$container->share('commandProcessor', 'Consolidation\AnnotatedCommand\CommandProcessor')
->withArgument('hookManager')
->withMethodCall('setFormatterManager', ['formatterManager']);
Expand All @@ -295,6 +297,7 @@ function drush_init_dependency_injection_container($input = null, $output = null
return $container;
}


// TODO: Where should this go?
function drush_init_application_global_options($container) {
$application = $container->get('application');
Expand Down Expand Up @@ -353,14 +356,22 @@ function drush_init_register_command_files($container, $commandFiles) {
$application = $container->get('application');
$commandFactory = $container->get('commandFactory');
foreach ($commandFiles as $sourcePath => $className) {
if (!class_exists($className)) {
include $sourcePath;
}
$classAlias = str_replace('\\', '', $className);

// Add and fetch our class from the container to apply the inductors
$container->share($classAlias, $className);
$commandFileInstance = $container->get($classAlias);
$commandList = $commandFactory->createCommandsFromClass($commandFileInstance);
foreach ($commandList as $command) {
$application->add($command);
$commandName = $command->getName();
$drushAlias = strtr($commandName, ':', '-');
if ($commandName != $drushAlias) {
$aliases = $command->getAliases();
$command->setAliases(array_unique(array_merge($aliases, [$drushAlias])));
}
$application->add($command);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions lib/Drush/Backend/BackendResultSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Drush\Backend;

use Consolidation\AnnotatedCommand\Hooks\ExtractOutputInterface;

class BackendResultSetter implements ExtractOutputInterface
{
public function extractOutput($structured_data) {
$return = drush_backend_get_result();
if (empty($return)) {
drush_backend_set_result($structured_data);
}
}
}
148 changes: 148 additions & 0 deletions tests/annotatedCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Unish;

/**
* @group base
*/
class annotatedCommandCase extends CommandUnishTestCase {
public function testExecute() {
$sites = $this->setUpDrupal(1, TRUE);
$uri = key($sites);
$root = $this->webroot();
$options = array(
'root' => $root,
'uri' => $uri,
'yes' => NULL,
);

// Copy the 'woot' module over to the Drupal site we just set up.
$this->setupModulesForTests($root);

// Enable out module. This will also clear the commandfile cache.
$this->drush('pm-enable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);

// drush woot --help
$this->drush('woot', array(), $options + ['help' => NULL], NULL, NULL, self::EXIT_SUCCESS);
$output = $this->getOutput();
$this->assertContains('Woot mightily.', $output);
// TODO: Symfony Console does not print alias info like this
// $this->assertContains('Aliases: wt', $output);

// drush help woot. TODO: drush help does not find annotated commands yet
// $this->drush('help', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);
// $output = $this->getOutput();
// $this->assertContains('Woot mightily.', $output);

// drush woot
$this->drush('woot', array(), $options, NULL, NULL, self::EXIT_SUCCESS);
$output = $this->getOutput();
$this->assertEquals('Woot!', $output);

// drush my-cat --help
$this->drush('my-cat', array(), $options + ['help' => NULL], NULL, NULL, self::EXIT_SUCCESS);
$output = $this->getOutput();
// TODO: the command description does not appear in the help text yet
//$this->assertContains('This is the my-cat command', $output);
$this->assertContains('bet alpha --flip', $output);
$this->assertContains('The first parameter', $output);
$this->assertContains('The other parameter', $output);
$this->assertContains('Whether or not the second parameter', $output);
// TODO: Symfony Console does not print alias info like this
// $this->assertContains('Aliases: c', $output);

// drush help my-cat
// TODO: help cannot find annotated commands yet
//$this->drush('help', array('my-cat'), $options, NULL, NULL, self::EXIT_SUCCESS);
//$output = $this->getOutput();
//$this->assertContains('This is the my-cat command', $output);

// drush my-cat bet alpha --flip
$this->drush('my-cat', array('bet', 'alpha'), $options + ['flip' => NULL], NULL, NULL, self::EXIT_SUCCESS);
$output = $this->getOutput();
$this->assertEquals('alphabet', $output);

// drush woot --help with the 'woot' module ignored
$this->drush('woot', array(), $options + ['help' => NULL, 'ignored-modules' => 'woot'], NULL, NULL, self::EXIT_ERROR);

// drush my-cat bet alpha --flip
$this->drush('my-cat', array('bet', 'alpha'), $options + ['flip' => NULL, 'ignored-modules' => 'woot'], NULL, NULL, self::EXIT_ERROR);

$this->drush('try-formatters', array(), $options, NULL, NULL, self::EXIT_SUCCESS);
$output = $this->getOutput();
$expected = <<<EOT
+------+------+-------+
| I | II | III |
+------+------+-------+
| One | Two | Three |
| Eins | Zwei | Drei |
| Ichi | Ni | San |
| Uno | Dos | Tres |
+------+------+-------+
EOT;
$this->assertEquals($expected, $output);

$this->drush('try-formatters --format=yaml --fields=III,II', array(), $options, NULL, NULL, self::EXIT_SUCCESS);
$output = $this->getOutput();
$expected = <<<EOT
en:
third: Three
second: Two
de:
third: Drei
second: Zwei
jp:
third: San
second: Ni
es:
third: Tres
second: Dos
EOT;
$this->assertEquals($expected, $output);

$this->drush('try-formatters', array(), $options + ['backend' => NULL]);
$parsed = $this->parse_backend_output($this->getOutput());
$data = $parsed['object'];
$expected = <<<EOT
{"en":{"first":"One","second":"Two","third":"Three"},"de":{"first":"Eins","second":"Zwei","third":"Drei"},"jp":{"first":"Ichi","second":"Ni","third":"San"},"es":{"first":"Uno","second":"Dos","third":"Tres"}}
EOT;
$this->assertEquals($expected, json_encode($data));

// drush try-formatters --help
$this->drush('try-formatters', array(), $options + ['help' => NULL], NULL, NULL, self::EXIT_SUCCESS);
$output = $this->getOutput();
// TODO: Command description does not appear in help text yet
//$this->assertContains('Demonstrate formatters', $output);
$this->assertContains('try:formatters --fields=first,third', $output);
$this->assertContains('try:formatters --fields=III,II', $output);
// TODO: Help for formats and fields not available yet
//$this->assertContains('--fields=<first, second, third>', $output);
//$this->assertContains('Fields to output. All available', $output);
//$this->assertContains('--format=<table>', $output);
//$this->assertContains('Select output format. Available:', $output);
// TODO: Symfony Console does not print alias info like this
// $this->assertContains('Aliases: try-formatters', $output);

// Disable the woot module to avoid cross-contamination of the Drupal
// test site's database. (not necessary?)
if (UNISH_DRUPAL_MAJOR_VERSION == 8) {
$this->drush('pm-uninstall', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);
}
else {
$this->drush('pm-disable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);
}
// Also kill the Drush cache so that our 'woot' command is not cached.
$this->drush('cache-clear', array('drush'), $options, NULL, NULL, self::EXIT_SUCCESS);
}

public function setupModulesForTests($root) {
$wootModule = __DIR__ . '/resources/modules/d' . UNISH_DRUPAL_MAJOR_VERSION . '/woot';
$modulesDir = "$root/sites/all/modules";
$this->mkdir($modulesDir);
\symlink($wootModule, "$modulesDir/woot");
if ((UNISH_DRUPAL_MAJOR_VERSION < 8) && !file_exists("$wootModule/Command/WootCommands.php")) {
$woot8Module = __DIR__ . '/resources/modules/d8/woot';
\symlink("$woot8Module/src/Command/WootCommands.php", "$wootModule/Command/WootCommands.php");
}
}
}
1 change: 1 addition & 0 deletions tests/resources/modules/d7/woot/Command/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WootCommands.php
4 changes: 4 additions & 0 deletions tests/resources/modules/d7/woot/woot.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = woot
description = Woot Mightily
core = 7.x
files[] = woot.module
22 changes: 22 additions & 0 deletions tests/resources/modules/d7/woot/woot.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Implements hook_menu
*/
function woot_menu() {
$items = array();

$items['woot'] = array(
'title' => 'Woot',
'description' => 'Woot mightily.',
'page callback' => 'woot_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);

return $items;
}

function woot_page() {
return array('#markup' => 'Woot!');
}
14 changes: 14 additions & 0 deletions tests/resources/modules/d8/woot/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "drupal/woot",
"type": "drupal-module",
"description": "Woot Mightily.",
"keywords": ["Drupal"],
"license": "GPL-2.0+",
"homepage": "http://drupal.org/project/woot",
"minimum-stability": "dev",
"support": {
"issues": "http://drupal.org/project/issues/woot",
"source": "http://cgit.drupalcode.org/woot"
},
"require": { }
}
66 changes: 66 additions & 0 deletions tests/resources/modules/d8/woot/src/Command/WootCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace Drupal\woot\Command;

use Consolidation\OutputFormatters\StructuredData\RowsOfFields;

/**
* For commands that are parts of modules, Drush expects to find commandfiles in
* __MODULE__/src/Command, and the namespace is Drupal/__MODULE__/Command.
*/
class WootCommands
{
/**
* Woot mightily.
*
* @aliases wt
*/
public function woot()
{
return 'Woot!';
}

/**
* This is the my-cat command
*
* This command will concatinate two parameters. If the --flip flag
* is provided, then the result is the concatination of two and one.
*
* @param string $one The first parameter.
* @param string $two The other parameter.
* @option boolean $flip Whether or not the second parameter should come first in the result.
* @aliases c
* @usage bet alpha --flip
* Concatinate "alpha" and "bet".
*/
public function myCat($one, $two = '', $options = ['flip' => false])
{
if ($options['flip']) {
return "{$two}{$one}";
}
return "{$one}{$two}";
}

/**
* Demonstrate formatters. Default format is 'table'.
*
* @field-labels
* first: I
* second: II
* third: III
* @usage try:formatters --format=yaml
* @usage try:formatters --format=csv
* @usage try:formatters --fields=first,third
* @usage try:formatters --fields=III,II
* @return Consolidation\OutputFormatters\StructuredData\RowsOfFields
*/
public function tryFormatters($options = ['format' => 'table', 'fields' => ''])
{
$outputData = [
'en' => [ 'first' => 'One', 'second' => 'Two', 'third' => 'Three' ],
'de' => [ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei' ],
'jp' => [ 'first' => 'Ichi', 'second' => 'Ni', 'third' => 'San' ],
'es' => [ 'first' => 'Uno', 'second' => 'Dos', 'third' => 'Tres' ],
];
return new RowsOfFields($outputData);
}
}
31 changes: 31 additions & 0 deletions tests/resources/modules/d8/woot/src/Controller/WootController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* @file
* Contains Drupal\woot\Controller\WootController.
*/

namespace Drupal\woot\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
* Class WootController.
*
* @package Drupal\woot\Controller
*/
class WootController extends ControllerBase {
/**
* Woot.
*
* @return string
* Return Hello string.
*/
public function woot() {
return [
'#type' => 'markup',
'#markup' => $this->t('Woot!')
];
}

}
5 changes: 5 additions & 0 deletions tests/resources/modules/d8/woot/woot.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Woot
type: module
description: Woot Mightily.
core: 8.x
package: Other
33 changes: 33 additions & 0 deletions tests/resources/modules/d8/woot/woot.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* @file
* Contains woot.module.
*/

use Drupal\Core\Routing\RouteMatchInterface;

/**
* Implements hook_help().
*/
function woot_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the woot module.
case 'help.page.woot':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Woot Mightily.') . '</p>';
return $output;

default:
}
}

/**
* Implements hook_theme().
*/
function woot_theme() {
$theme = [];

return $theme;
}
Loading

0 comments on commit 4d110d2

Please sign in to comment.