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