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

Namespace

Unish

Classes

Namesort descending Description
completeCase @group base

File

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