completeTest.php

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

Classes

Namesort descending Description
completeCase @group base

File

tests/completeTest.php
View source
  1. <?php
  2. /**
  3. * @group base
  4. */
  5. class completeCase extends Drush_CommandTestCase {
  6. /**
  7. * Write a config file that contains our configuration file.
  8. */
  9. static function setUpBeforeClass() {
  10. parent::setUpBeforeClass();
  11. $contents = "
  12. <?php
  13. \$options['shell-aliases'] = array(
  14. 'uninstall' => 'pm-uninstall',
  15. );
  16. ";
  17. file_put_contents(UNISH_SANDBOX . '/drushrc.php', trim($contents));
  18. }
  19. public function testComplete() {
  20. // We copy our completetest commandfile into our path.
  21. // We cannot use --include since complete deliberately avoids drush
  22. // command dispatch.
  23. copy(dirname(__FILE__) . '/completetest.drush.inc', getenv('HOME') . '/.drush/completetest.drush.inc');
  24. $sites = $this->setUpDrupal(2);
  25. $env = key($sites);
  26. $root = $this->webroot();
  27. // We copy the unit test command into (only) our dev site, so we have a
  28. // detectable difference we can use to detect cache correctness between
  29. // sites.
  30. mkdir("$root/sites/$env/modules");
  31. copy(dirname(__FILE__) . '/completetestsite.drush.inc', "$root/sites/$env/modules/completetestsite.drush.inc");
  32. // Clear the cache, so it finds our test command.
  33. $this->drush('php-eval', array('drush_cache_clear_all();'), array(), '@' . $env);
  34. // Create a sample directory and file to test file/directory completion.
  35. mkdir("astronaut");
  36. mkdir("asteroid");
  37. mkdir("asteroid/ceres");
  38. mkdir("asteroid/chiron");
  39. touch('astronaut/aldrin.tar.gz');
  40. touch('astronaut/armstrong.tar.gz');
  41. touch('astronaut/yuri gagarin.tar.gz');
  42. touch('zodiac.tar.gz');
  43. touch('zodiac.txt');
  44. // Create directory for temporary debug logs.
  45. mkdir(UNISH_SANDBOX . '/complete-debug');
  46. // Test cache clearing for global cache, which should affect all
  47. // environments. First clear the cache:
  48. $this->drush('php-eval', array('drush_complete_cache_clear();'));
  49. // Confirm we get cache rebuilds for runs both in and out of a site
  50. // which is expected since these should resolve to separate cache IDs.
  51. $this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra', FALSE);
  52. $this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf', FALSE);
  53. // Next, rerun and check results to confirm cache IDs are generated
  54. // correctly on our fast bootstrap when returning the cached result.
  55. $this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra');
  56. $this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf');
  57. // Test cache clearing for a completion type, which should be effective only
  58. // for current environment - i.e. a specific site should not be effected.
  59. $this->drush('php-eval', array('drush_complete_cache_clear("command-names");'));
  60. $this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra');
  61. $this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf', FALSE);
  62. // Test cache clearing for a command specific completion type, which should
  63. // be effective only for current environment. Prime caches first.
  64. $this->verifyComplete('@dev aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
  65. $this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
  66. $this->drush('php-eval', array('drush_complete_cache_clear("arguments", "aaaaaaaard");'));
  67. // We cleared the global cache for this argument, not the site specific
  68. // cache should still exist.
  69. $this->verifyComplete('@dev aaaaaaaard a', 'aardvark', 'aardwolf');
  70. $this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
  71. // Test overall context sensitivity - almost all of these are cache hits.
  72. // No context (i.e. "drush <tab>"), should list aliases and commands.
  73. $this->verifyComplete('""', '@dev', 'zzzzzzzzebra');
  74. // Site alias alone.
  75. $this->verifyComplete('@', '@dev', '@stage');
  76. // Command alone.
  77. $this->verifyComplete('aaaaaaaa', 'aaaaaaaard', 'aaaaaaaard-wolf');
  78. // Command with single result.
  79. $this->verifyComplete('aaaaaaaard-v', 'aaaaaaaard-vark', 'aaaaaaaard-vark');
  80. // Command with no results should produce no output.
  81. $this->verifyComplete('dont-name-a-command-like-this', '', '');
  82. // Commands that start the same as another command (i.e. aaaaaaaard is a
  83. // valid command, but we should still list aaaaaaaardwolf when completing on
  84. // "aaaaaaaard").
  85. $this->verifyComplete('@dev aaaaaaaard', 'aaaaaaaard', 'aaaaaaaard-zebra');
  86. // Global option alone.
  87. $this->verifyComplete('--n', '--no', '--notify-audio');
  88. // Site alias + command.
  89. $this->verifyComplete('@dev aaaaaaaa', 'aaaaaaaard', 'aaaaaaaard-zebra');
  90. // Site alias + command, should allow no further site aliases or commands.
  91. $this->verifyComplete('@dev aaaaaaaard-wolf @', '', '', FALSE);
  92. $this->verifyComplete('@dev aaaaaaaard-wolf aaaaaaaa', '', '');
  93. // Command + command option.
  94. $this->verifyComplete('aaaaaaaard --', '--ears', '--nose');
  95. // Site alias + command + command option.
  96. $this->verifyComplete('@dev aaaaaaaard --', '--ears', '--nose');
  97. // Command + all arguments
  98. $this->verifyComplete('aaaaaaaard ""', 'aardvark', 'zebra');
  99. // Command + argument.
  100. $this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf');
  101. // Site alias + command + regular argument.
  102. // Note: this is checked implicitly by the argument cache testing above.
  103. if ($this->is_windows()) {
  104. $this->markTestSkipped('Complete tests not fully working nor needed on Windows.');
  105. }
  106. // Site alias + command + file/directory argument tests.
  107. // Current directory substrings.
  108. // NOTE: This command arg has not been used yet, so cache miss is expected.
  109. $this->verifyComplete('archive-restore ""', 'asteroid/', 'zodiac.tar.gz', FALSE);
  110. $this->verifyComplete('archive-restore a', 'asteroid/', 'astronaut/');
  111. $this->verifyComplete('archive-restore ast', 'asteroid/', 'astronaut/');
  112. $this->verifyComplete('archive-restore aste', 'asteroid/', 'asteroid/');
  113. $this->verifyComplete('archive-restore asteroid', 'asteroid/', 'asteroid/');
  114. $this->verifyComplete('archive-restore asteroid/', 'ceres', 'chiron');
  115. $this->verifyComplete('archive-restore asteroid/ch', 'asteroid/chiron/', 'asteroid/chiron/');
  116. $this->verifyComplete('archive-restore astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
  117. $this->verifyComplete('archive-restore astronaut/y', 'astronaut/yuri\ gagarin.tar.gz', 'astronaut/yuri\ gagarin.tar.gz');
  118. // Leading dot style current directory substrings.
  119. $this->verifyComplete('archive-restore .', './asteroid/', './zodiac.tar.gz');
  120. $this->verifyComplete('archive-restore ./', './asteroid/', './zodiac.tar.gz');
  121. $this->verifyComplete('archive-restore ./a', './asteroid/', './astronaut/');
  122. $this->verifyComplete('archive-restore ./ast', './asteroid/', './astronaut/');
  123. $this->verifyComplete('archive-restore ./aste', './asteroid/', './asteroid/');
  124. $this->verifyComplete('archive-restore ./asteroid', './asteroid/', './asteroid/');
  125. $this->verifyComplete('archive-restore ./asteroid/', 'ceres', 'chiron');
  126. $this->verifyComplete('archive-restore ./asteroid/ch', './asteroid/chiron/', './asteroid/chiron/');
  127. $this->verifyComplete('archive-restore ./astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
  128. $this->verifyComplete('archive-restore ./astronaut/y', './astronaut/yuri\ gagarin.tar.gz', './astronaut/yuri\ gagarin.tar.gz');
  129. // Absolute path substrings.
  130. $path = getcwd();
  131. $this->verifyComplete('archive-restore ' . $path, $path . '/', $path . '/');
  132. $this->verifyComplete('archive-restore ' . $path . '/', 'asteroid', 'zodiac.tar.gz');
  133. $this->verifyComplete('archive-restore ' . $path . '/a', $path . '/asteroid', $path . '/astronaut');
  134. $this->verifyComplete('archive-restore ' . $path . '/ast', 'asteroid', 'astronaut');
  135. $this->verifyComplete('archive-restore ' . $path . '/aste', $path . '/asteroid/', $path . '/asteroid/');
  136. $this->verifyComplete('archive-restore ' . $path . '/asteroid', $path . '/asteroid/', $path . '/asteroid/');
  137. $this->verifyComplete('archive-restore ' . $path . '/asteroid/', $path . '/asteroid/ceres', $path . '/asteroid/chiron');
  138. $this->verifyComplete('archive-restore ' . $path . '/asteroid/ch', $path . '/asteroid/chiron/', $path . '/asteroid/chiron/');
  139. $this->verifyComplete('archive-restore ' . $path . '/astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
  140. $this->verifyComplete('archive-restore ' . $path . '/astronaut/y', $path . '/astronaut/yuri\ gagarin.tar.gz', $path . '/astronaut/yuri\ gagarin.tar.gz');
  141. // Absolute via parent path substrings.
  142. $this->verifyComplete('archive-restore ' . $path . '/asteroid/../astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
  143. $this->verifyComplete('archive-restore ' . $path . '/asteroid/../astronaut/y', $path . '/asteroid/../astronaut/yuri\ gagarin.tar.gz', $path . '/asteroid/../astronaut/yuri\ gagarin.tar.gz');
  144. // Parent directory path substrings.
  145. chdir('asteroid/chiron');
  146. $this->verifyComplete('archive-restore ../../astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
  147. $this->verifyComplete('archive-restore ../../astronaut/y', '../../astronaut/yuri\ gagarin.tar.gz', '../../astronaut/yuri\ gagarin.tar.gz');
  148. chdir($path);
  149. }
  150. /**
  151. * Helper function to call completion and make common checks.
  152. *
  153. * @param $command
  154. * The command line to attempt to complete.
  155. * @param $first
  156. * String indicating the expected first completion suggestion.
  157. * @param $last
  158. * String indicating the expected last completion suggestion.
  159. * @param bool $cache_hit
  160. * Optional parameter, if TRUE or omitted the debug log is checked to
  161. * ensure a cache hit on the last cache debug log entry, if FALSE then a
  162. * cache miss is checked for.
  163. */
  164. function verifyComplete($command, $first, $last, $cache_hit = TRUE) {
  165. // We capture debug output to a separate file, so we can check for cache
  166. // hits/misses.
  167. $debug_file = tempnam(UNISH_SANDBOX . '/complete-debug', 'complete-debug');
  168. // Commands should take the format:
  169. // drush --early=includes/complete.inc [--complete-debug] drush [@alias] [command]...
  170. $exec = sprintf('%s --early=includes/complete.inc --config=%s --complete-debug %s %s 2> %s', UNISH_DRUSH, UNISH_SANDBOX . '/drushrc.php', UNISH_DRUSH, $command, $debug_file);
  171. $this->execute($exec);
  172. $result = $this->getOutputAsList();
  173. $actual = reset($result);
  174. $this->assertEquals("$command: (f) $first", "$command: (f) $actual");
  175. $actual = end($result);
  176. $this->assertEquals("$command: (l) $last", "$command: (l) $actual");
  177. // If checking for HIT, we ensure no MISS exists, if checking for MISS we
  178. // ensure no HIT exists. However, we exclude the first cache report, since
  179. // it is expected that the command-names cache (loaded when matching
  180. // command names) may sometimes be a HIT even when we are testing for a MISS
  181. // in the actual cache we are loading to complete against.
  182. $check_not_exist = 'HIT';
  183. if ($cache_hit) {
  184. $check_not_exist = 'MISS';
  185. }
  186. $contents = file_get_contents($debug_file);
  187. // Find the all cache messages of type "-complete-"
  188. preg_match_all("/Cache [A-Z]* cid:.*-complete-/", $contents, $matches);
  189. $contents = implode("\n", $matches[0]);
  190. $first_cache_pos = strpos($contents, 'Cache ') + 6;
  191. $this->assertFalse(strpos($contents, 'Cache ' . $check_not_exist . ' cid', $first_cache_pos));
  192. unlink($debug_file);
  193. }
  194. }