drushScriptTest.php

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

Namespace

Unish

Classes

Namesort descending Description
drushScriptCase Tests for the 'drush' script itself

File

tests/drushScriptTest.php
View source
  1. <?php
  2. namespace Unish;
  3. use Webmozart\PathUtil\Path;
  4. /**
  5. * Tests for the 'drush' script itself
  6. */
  7. class drushScriptCase extends CommandUnishTestCase {
  8. /**
  9. * Test `PHP_OPTIONS=... drush`
  10. */
  11. public function testPhpOptionsTest() {
  12. $this->markTestSkipped('Environment variables not yet passed along to Process by execute().');
  13. // @todo: could probably run this test on mingw
  14. if ($this->is_windows()) {
  15. $this->markTestSkipped('Environment variable tests not currently functional on Windows.');
  16. }
  17. $options = array();
  18. $env = array('PHP_OPTIONS' => '-d default_mimetype="text/drush"');
  19. $this->drush('ev', array('print ini_get("default_mimetype");'), $options, NULL, NULL, self::EXIT_SUCCESS, NULL, $env);
  20. $output = $this->getOutput();
  21. $this->assertEquals('text/drush', $output);
  22. }
  23. public function testDrushFinder() {
  24. // We don't really need a real Drupal site; we could
  25. // create a fake site, as long as we had the right signature
  26. // files to allow us to bootstrap to the DRUPAL_ROOT phase.
  27. $this->setUpDrupal(1, TRUE);
  28. $globalDrushDotPhp = Path::join(UNISH_DRUSH, '../drush.php');
  29. // Control: test `drush --root ` ... with no site-local Drush
  30. $drush_location = $this->getDrushLocation();
  31. $this->assertEquals($globalDrushDotPhp, $drush_location);
  32. // We will try copying a site-local Drush to
  33. // all of the various locations the 'drush finder'
  34. // might expect to find it.
  35. $drush_locations = array(
  36. "vendor",
  37. "../vendor",
  38. "sites/all/vendor",
  39. "sites/all",
  40. );
  41. foreach ($drush_locations as $drush_base) {
  42. $drush_root = $this->create_site_local_drush($drush_base);
  43. // Test `drush --root ` ... with a site-local Drush
  44. $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
  45. $this->assertEquals(realpath($drush_root . '/drush.php'), realpath($drush_location));
  46. // Ensure that --local was NOT added
  47. $result = $this->drush('ev', array('return drush_get_option("local");'), array('root' => $this->webroot()));
  48. $output = $this->getOutput();
  49. $this->assertEquals("", $output);
  50. // Run the `drush --root` test again, this time with
  51. // a drush.wrapper script in place.
  52. $this->createDrushWrapper($drush_base);
  53. $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
  54. $this->assertEquals(realpath($drush_root . '/drush.php'), realpath($drush_location));
  55. // Test to see if --local was added
  56. $result = $this->drush('ev', array('var_export(drush_get_option("local"));'), array('root' => $this->webroot()));
  57. $output = $this->getOutput();
  58. $this->assertEquals("true", $output);
  59. // Get rid of the symlink and site-local Drush we created
  60. $this->remove_site_local_drush($drush_base);
  61. }
  62. // Next, try again with a site-local Drush in a location
  63. // that Drush does not search.
  64. $mysterious_location = "path/drush/does/not/search";
  65. $drush_root = $this->create_site_local_drush($mysterious_location);
  66. // We should not find the site-local Drush without a Drush wrapper.
  67. $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
  68. $this->assertEquals($globalDrushDotPhp, $drush_location);
  69. $this->createDrushWrapper($mysterious_location);
  70. // Now that there is a Drush wrapper, we should be able to find the site-local Drush.
  71. $drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
  72. $this->assertEquals(realpath($drush_root . '/drush.php'), $drush_location);
  73. }
  74. /**
  75. * Copy UNISH_DRUSH into the specified site-local location.
  76. */
  77. function create_site_local_drush($drush_base) {
  78. $drush_root = $this->webroot() . '/' . $drush_base . '/drush/drush';
  79. $bin_dir = $this->webroot() . '/' . $drush_base . '/bin';
  80. $this->mkdir(dirname($drush_root));
  81. $this->recursive_copy(dirname(UNISH_DRUSH), $drush_root);
  82. @chmod($drush_root . '/drush', 0777);
  83. @chmod($drush_root . '/drush.launcher', 0777);
  84. $this->mkdir($bin_dir);
  85. symlink($drush_root . '/drush', $bin_dir . '/drush');
  86. return $drush_root;
  87. }
  88. function remove_site_local_drush($drush_base) {
  89. // Get rid of the symlink and site-local Drush we created
  90. unish_file_delete_recursive($this->webroot() . '/' . $drush_base . '/drush/drush');
  91. unlink($this->webroot() . '/' . $drush_base . '/bin/drush');
  92. if (file_exists($this->webroot() . '/drush.wrapper')) {
  93. unlink($this->webroot() . '/drush.wrapper');
  94. }
  95. }
  96. /**
  97. * TODO: Create a Drush wrapper script, and copy it to
  98. * to the root of the fake Drupal site, and point it
  99. * at the specified site-local Drush script.
  100. */
  101. function createDrushWrapper($drush_base) {
  102. $drush_launcher = $drush_base . '/drush/drush/drush.launcher';
  103. $drush_wrapper_src = dirname(UNISH_DRUSH) . '/examples/drush.wrapper';
  104. $drush_wrapper_contents = file_get_contents($drush_wrapper_src);
  105. $drush_wrapper_contents = preg_replace('#\.\./vendor/bin/drush.launcher#', $drush_launcher, $drush_wrapper_contents);
  106. $drush_wrapper_target = $this->webroot() . '/drush.wrapper';
  107. file_put_contents($drush_wrapper_target, $drush_wrapper_contents);
  108. @chmod($drush_wrapper_target, 0777);
  109. }
  110. /**
  111. * Get the current location of the Drush script via
  112. * `drush status 'Drush script' --format=yaml`. This
  113. * will return results other than UNISH_DRUSH in the
  114. * presence of a site-local Drush.
  115. */
  116. function getDrushLocation($options = array()) {
  117. $options += array(
  118. 'format' => 'yaml',
  119. 'verbose' => NULL,
  120. );
  121. $result = $this->drush('status', array('Drush script'), $options);
  122. $output = $this->getOutput();
  123. list($key, $value) = explode(": ", $output);
  124. return trim($value, "'");
  125. }
  126. }