shellAliasTest.php

  1. 8.0.x tests/shellAliasTest.php
  2. 6.x tests/shellAliasTest.php
  3. 7.x tests/shellAliasTest.php
  4. 5.x tests/shellAliasTest.php
  5. master tests/shellAliasTest.php

Tests for Shell aliases.

@group base

Classes

Namesort descending Description
shellAliasesCase @file Tests for Shell aliases.

File

tests/shellAliasTest.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Shell aliases.
  5. *
  6. * @group base
  7. */
  8. class shellAliasesCase extends Drush_CommandTestCase {
  9. /**
  10. * Write a config file that contains the shell-aliases array.
  11. */
  12. static function setUpBeforeClass() {
  13. parent::setUpBeforeClass();
  14. $contents = "
  15. <?php
  16. \$options['shell-aliases'] = array(
  17. 'glopts' => 'topic core-global-options',
  18. 'pull' => '!git pull',
  19. 'echosimple' => '!echo {{@target}}',
  20. 'echotest' => '!echo {{@target}} {{%root}} {{%mypath}}',
  21. 'compound-command' => '!cd {{%sandbox}} && echo second',
  22. );
  23. ";
  24. file_put_contents(UNISH_SANDBOX . '/drushrc.php', trim($contents));
  25. mkdir(UNISH_SANDBOX . '/b');
  26. $contents = "
  27. <?php
  28. \$options['shell-aliases'] = array(
  29. 'also' => '!echo alternate config file included too',
  30. );
  31. ";
  32. file_put_contents(UNISH_SANDBOX . '/b/drushrc.php', trim($contents));
  33. $aliases['myalias'] = array(
  34. 'root' => '/path/to/drupal',
  35. 'uri' => 'mysite.org',
  36. '#peer' => '@live',
  37. 'path-aliases' => array (
  38. '%mypath' => '/srv/data/mypath',
  39. '%sandbox' => UNISH_SANDBOX,
  40. ),
  41. );
  42. $contents = unish_file_aliases($aliases);
  43. file_put_contents(UNISH_SANDBOX . '/aliases.drushrc.php', $contents);
  44. }
  45. /**
  46. * Test shell aliases to Drush commands.
  47. */
  48. public function testShellAliasDrushLocal() {
  49. $options = array(
  50. 'config' => UNISH_SANDBOX,
  51. );
  52. $this->drush('glopts', array(), $options);
  53. $output = $this->getOutput();
  54. $this->assertContains('These options are applicable to most drush commands.', $output, 'Successfully executed local shell alias to drush command');
  55. }
  56. /**
  57. * Test shell aliases to Bash commands. Assure we pass along extra arguments
  58. * and options.
  59. */
  60. public function testShellAliasBashLocal() {
  61. $options = array(
  62. 'config' => UNISH_SANDBOX,
  63. 'simulate' => NULL,
  64. 'rebase' => NULL,
  65. );
  66. $this->drush('pull', array('origin'), $options, NULL, NULL, self::EXIT_SUCCESS, '2>&1');
  67. $output = $this->getOutput();
  68. $this->assertContains('Calling proc_open(git pull origin --rebase);', $output);
  69. }
  70. public function testShellAliasDrushRemote() {
  71. $options = array(
  72. 'config' => UNISH_SANDBOX,
  73. 'simulate' => NULL,
  74. 'ssh-options' => '',
  75. );
  76. $this->drush('glopts', array(), $options, 'user@server/path/to/drupal#sitename');
  77. // $expected might be different on non unix platforms. We shall see.
  78. // n.b. --config is not included in calls to remote systems.
  79. $bash = $this->escapeshellarg('drush --nocolor --uri=sitename --root=/path/to/drupal core-topic core-global-options 2>&1');
  80. $expected = "Simulating backend invoke: ssh user@server $bash 2>&1";
  81. $output = $this->getOutput();
  82. $this->assertEquals($expected, $output, 'Expected remote shell alias to a drush command was built');
  83. }
  84. public function testShellAliasBashRemote() {
  85. $options = array(
  86. 'config' => UNISH_SANDBOX,
  87. 'simulate' => NULL,
  88. 'ssh-options' => '',
  89. 'rebase' => NULL,
  90. );
  91. $this->drush('pull', array('origin'), $options, 'user@server/path/to/drupal#sitename', NULL, self::EXIT_SUCCESS, '2>&1');
  92. // $expected might be different on non unix platforms. We shall see.
  93. $expected = "Calling proc_open(ssh user@server 'cd /path/to/drupal && git pull origin --rebase');";
  94. $output = $this->getOutput();
  95. $this->assertEquals($expected, $output, 'Expected remote shell alias to a bash command was built');
  96. }
  97. /**
  98. * Test shell aliases with simple replacements -- no alias.
  99. */
  100. public function testShellAliasSimpleReplacement() {
  101. $options = array(
  102. 'config' => UNISH_SANDBOX,
  103. );
  104. $this->drush('echosimple', array(), $options);
  105. // Windows command shell actually prints quotes. See http://drupal.org/node/1452944.
  106. $expected = $this->is_windows() ? '"@none"' : '@none';
  107. $output = $this->getOutput();
  108. $this->assertEquals($expected, $output);
  109. }
  110. /**
  111. * Test shell aliases with complex replacements -- no alias.
  112. */
  113. public function testShellAliasReplacementNoAlias() {
  114. $options = array(
  115. 'config' => UNISH_SANDBOX,
  116. );
  117. // echo test has replacements that are not satisfied, so this is expected to return an error.
  118. $this->drush('echotest', array(), $options, NULL, NULL, self::EXIT_ERROR);
  119. }
  120. /**
  121. * Test shell aliases with replacements -- alias.
  122. */
  123. public function testShellAliasReplacementWithAlias() {
  124. $options = array(
  125. 'config' => UNISH_SANDBOX,
  126. 'alias-path' => UNISH_SANDBOX,
  127. );
  128. $this->drush('echotest', array(), $options, '@myalias');
  129. // Windows command shell actually prints quotes. See http://drupal.org/node/1452944.
  130. $expected = $this->is_windows() ? '"@myalias"' : '@myalias';
  131. $expected .= ' /path/to/drupal /srv/data/mypath';
  132. $output = $this->getOutput();
  133. $this->assertEquals($expected, $output);
  134. }
  135. /**
  136. * Test shell aliases with replacements and compound commands.
  137. */
  138. public function testShellAliasCompoundCommands() {
  139. $options = array(
  140. 'config' => UNISH_SANDBOX,
  141. 'alias-path' => UNISH_SANDBOX,
  142. );
  143. $this->drush('compound-command', array(), $options, '@myalias');
  144. $expected = 'second';
  145. $output = $this->getOutput();
  146. $this->assertEquals($expected, $output);
  147. }
  148. /**
  149. * Test shell aliases with multiple config files.
  150. */
  151. public function testShellAliasMultipleConfigFiles() {
  152. $options = array(
  153. 'config' => UNISH_SANDBOX . "/b" . PATH_SEPARATOR . UNISH_SANDBOX,
  154. 'alias-path' => UNISH_SANDBOX,
  155. );
  156. $this->drush('also', array(), $options);
  157. $expected = "alternate config file included too";
  158. $output = $this->getOutput();
  159. $this->assertEquals($expected, $output);
  160. }
  161. }