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("aardvark");
  36. touch('aard wolf.tar.gz');
  37. // Create directory for temporary debug logs.
  38. mkdir(UNISH_SANDBOX . '/complete-debug');
  39. // Test cache clearing for global cache, which should affect all
  40. // environments. First clear the cache:
  41. $this->drush('php-eval', array('drush_complete_cache_clear();'));
  42. // Confirm we get cache rebuilds for runs both in and out of a site
  43. // which is expected since these should resolve to separate cache IDs.
  44. $this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra', FALSE);
  45. $this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf', FALSE);
  46. // Next, rerun and check results to confirm cache IDs are generated
  47. // correctly on our fast bootstrap when returning the cached result.
  48. $this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra');
  49. $this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf');
  50. // Test cache clearing for a completion type, which should be effective only
  51. // for current environment - i.e. a specific site should not be effected.
  52. $this->drush('php-eval', array('drush_complete_cache_clear("command-names");'));
  53. $this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra');
  54. $this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf', FALSE);
  55. // Test cache clearing for a command specific completion type, which should
  56. // be effective only for current environment. Prime caches first.
  57. $this->verifyComplete('@dev aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
  58. $this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
  59. $this->drush('php-eval', array('drush_complete_cache_clear("arguments", "aaaaaaaard");'));
  60. // We cleared the global cache for this argument, not the site specific
  61. // cache should still exist.
  62. $this->verifyComplete('@dev aaaaaaaard a', 'aardvark', 'aardwolf');
  63. $this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
  64. // Test overall context sensitivity - almost all of these are cache hits.
  65. // No context (i.e. "drush <tab>"), should list aliases and commands.
  66. $this->verifyComplete('""', '@dev', 'zzzzzzzzebra');
  67. // Site alias alone.
  68. $this->verifyComplete('@', '@dev', '@stage');
  69. // Command alone.
  70. $this->verifyComplete('aaaaaaaa', 'aaaaaaaard', 'aaaaaaaard-wolf');
  71. // Command with single result.
  72. $this->verifyComplete('aaaaaaaard-v', 'aaaaaaaard-vark', 'aaaaaaaard-vark');
  73. // Command with no results should produce no output.
  74. $this->verifyComplete('dont-name-a-command-like-this', '', '');
  75. // Commands that start the same as another command (i.e. aaaaaaaard is a
  76. // valid command, but we should still list aaaaaaaardwolf when completing on
  77. // "aaaaaaaard").
  78. $this->verifyComplete('@dev aaaaaaaard', 'aaaaaaaard', 'aaaaaaaard-zebra');
  79. // Global option alone.
  80. $this->verifyComplete('--n', '--no', '--nocolor');
  81. // Site alias + command.
  82. $this->verifyComplete('@dev aaaaaaaa', 'aaaaaaaard', 'aaaaaaaard-zebra');
  83. // Site alias + command, should allow no further site aliases or commands.
  84. $this->verifyComplete('@dev aaaaaaaard-wolf @', '', '', FALSE);
  85. $this->verifyComplete('@dev aaaaaaaard-wolf aaaaaaaa', '', '');
  86. // Command + command option.
  87. $this->verifyComplete('aaaaaaaard --', '--ears', '--nose');
  88. // Site alias + command + command option.
  89. $this->verifyComplete('@dev aaaaaaaard --', '--ears', '--nose');
  90. // Command + all arguments
  91. $this->verifyComplete('aaaaaaaard ""', 'aardvark', 'zebra');
  92. // Command + argument.
  93. $this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf');
  94. // Site alias + command + regular argument.
  95. // Note: this is checked implicitly by the argument cache testing above.
  96. if ($this->is_windows()) {
  97. $this->markTestSkipped('Complete tests not fully working nor needed on Windows.');
  98. }
  99. // Site alias + command + file/directory argument. This is a command
  100. // argument we have not used so far, so a cache miss is expected.
  101. $this->verifyComplete('archive-restore aard', 'aard wolf.tar.gz', 'aardvark/', FALSE);
  102. // Site alias + command + file/directory argument with quoting.
  103. $this->verifyComplete('archive-restore aard\ w', 'aard\ wolf.tar.gz', 'aard\ wolf.tar.gz');
  104. }
  105. /**
  106. * Helper function to call completion and make common checks.
  107. *
  108. * @param $command
  109. * The command line to attempt to complete.
  110. * @param $first
  111. * String indicating the expected first completion suggestion.
  112. * @param $last
  113. * String indicating the expected last completion suggestion.
  114. * @param bool $cache_hit
  115. * Optional parameter, if TRUE or omitted the debug log is checked to
  116. * ensure a cache hit on the last cache debug log entry, if FALSE then a
  117. * cache miss is checked for.
  118. */
  119. function verifyComplete($command, $first, $last, $cache_hit = TRUE) {
  120. // We capture debug output to a separate file, so we can check for cache
  121. // hits/misses.
  122. $debug_file = tempnam(UNISH_SANDBOX . '/complete-debug', 'complete-debug');
  123. // Commands should take the format:
  124. // drush --early=includes/complete.inc [--complete-debug] drush [@alias] [command]...
  125. $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);
  126. $this->execute($exec);
  127. $result = $this->getOutputAsList();
  128. $actual = reset($result);
  129. $this->assertEquals("$command: (f) $first", "$command: (f) $actual");
  130. $actual = end($result);
  131. $this->assertEquals("$command: (l) $last", "$command: (l) $actual");
  132. // If checking for HIT, we ensure no MISS exists, if checking for MISS we
  133. // ensure no HIT exists. However, we exclude the first cache report, since
  134. // it is expected that the command-names cache (loaded when matching
  135. // command names) may sometimes be a HIT even when we are testing for a MISS
  136. // in the actual cache we are loading to complete against.
  137. $check_not_exist = 'HIT';
  138. if ($cache_hit) {
  139. $check_not_exist = 'MISS';
  140. }
  141. $contents = file_get_contents($debug_file);
  142. $first_cache_pos = strpos($contents, 'Cache ') + 6;
  143. $this->assertFalse(strpos($contents, 'Cache ' . $check_not_exist . ' cid', $first_cache_pos));
  144. unlink($debug_file);
  145. }
  146. }