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. $expected = "Calling proc_open(ssh user@server 'cd /path/to/drupal && git pull origin --rebase');";
  103. $output = $this->getOutput();
  104. $this->assertEquals($expected, $output, 'Expected remote shell alias to a bash command was built');
  105. }
  106. /**
  107. * Test shell aliases with simple replacements -- no alias.
  108. */
  109. public function testShellAliasSimpleReplacement() {
  110. $options = array(
  111. 'config' => UNISH_SANDBOX,
  112. );
  113. $this->drush('echosimple', array(), $options);
  114. // Windows command shell actually prints quotes. See http://drupal.org/node/1452944.
  115. $expected = $this->is_windows() ? '"@none"' : '@none';
  116. $output = $this->getOutput();
  117. $this->assertEquals($expected, $output);
  118. }
  119. /**
  120. * Test shell aliases with complex replacements -- no alias.
  121. */
  122. public function testShellAliasReplacementNoAlias() {
  123. $options = array(
  124. 'config' => UNISH_SANDBOX,
  125. );
  126. // echo test has replacements that are not satisfied, so this is expected to return an error.
  127. $this->drush('echotest', array(), $options, NULL, NULL, self::EXIT_ERROR);
  128. }
  129. /**
  130. * Test shell aliases with replacements -- alias.
  131. */
  132. public function testShellAliasReplacementWithAlias() {
  133. $options = array(
  134. 'config' => UNISH_SANDBOX,
  135. 'alias-path' => UNISH_SANDBOX,
  136. );
  137. $this->drush('echotest', array(), $options, '@myalias');
  138. // Windows command shell actually prints quotes. See http://drupal.org/node/1452944.
  139. $expected = $this->is_windows() ? '"@myalias"' : '@myalias';
  140. $expected .= ' /path/to/drupal /srv/data/mypath';
  141. $output = $this->getOutput();
  142. $this->assertEquals($expected, $output);
  143. }
  144. /**
  145. * Test shell aliases with replacements and compound commands.
  146. */
  147. public function testShellAliasCompoundCommands() {
  148. $options = array(
  149. 'config' => UNISH_SANDBOX,
  150. 'alias-path' => UNISH_SANDBOX,
  151. );
  152. $this->drush('compound-command', array(), $options, '@myalias');
  153. $expected = 'second';
  154. $output = $this->getOutput();
  155. $this->assertEquals($expected, $output);
  156. }
  157. /**
  158. * Test shell aliases with multiple config files.
  159. */
  160. public function testShellAliasMultipleConfigFiles() {
  161. $options = array(
  162. 'config' => UNISH_SANDBOX . "/b" . PATH_SEPARATOR . UNISH_SANDBOX,
  163. 'alias-path' => UNISH_SANDBOX,
  164. );
  165. $this->drush('also', array(), $options);
  166. $expected = "alternate config file included too";
  167. $output = $this->getOutput();
  168. $this->assertEquals($expected, $output);
  169. }
  170. }