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

Namespace

Unish

Classes

Namesort descending Description
shellAliasesCase Tests for Shell aliases.

File

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