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