bootstrap.inc

  1. 8.0.x tests/bootstrap.inc
  2. 8.0.x includes/bootstrap.inc
  3. 6.x includes/bootstrap.inc
  4. 7.x tests/bootstrap.inc
  5. 7.x includes/bootstrap.inc
  6. 5.x includes/bootstrap.inc
  7. master includes/bootstrap.inc
  8. master tests/bootstrap.inc

Initialize a sandboxed environment. Starts with call unish_init() at bottom.

Functions

Namesort descending Description
unish_delete_dir_contents Deletes the contents of a directory.
unish_file_delete_recursive Deletes the specified file or directory and everything inside it.
unish_init Initialize our environment at the start of each run (i.e. suite).

File

tests/bootstrap.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Initialize a sandboxed environment. Starts with call unish_init() at bottom.
  5. */
  6. // This code copied from drush_bootstrap_prepare() except for 1st line.
  7. $drush_base_path = dirname(__DIR__);
  8. $local_vendor_path = $drush_base_path . '/vendor/autoload.php';
  9. $global_vendor_path = $drush_base_path . '/../../../vendor/autoload.php';
  10. // Check for a local composer install or a global composer install. Vendor dirs are in different spots.
  11. if ((!@include $local_vendor_path) && (!@include $global_vendor_path)) {
  12. $msg = "Unable to load autoload.php. Drush now requires Composer in order to install its dependencies and autoload classes. Please see README.md\n";
  13. fwrite(STDERR, $msg);
  14. return FALSE;
  15. }
  16. unish_init();
  17. /**
  18. * Initialize our environment at the start of each run (i.e. suite).
  19. */
  20. function unish_init() {
  21. // Default drupal major version to run tests over.
  22. $unish_drupal_major = '7';
  23. if (getenv('UNISH_DRUPAL_MAJOR_VERSION')) {
  24. $unish_drupal_major = getenv('UNISH_DRUPAL_MAJOR_VERSION');
  25. }
  26. elseif (isset($GLOBALS['UNISH_DRUPAL_MAJOR_VERSION'])) {
  27. $unish_drupal_major = $GLOBALS['UNISH_DRUPAL_MAJOR_VERSION'];
  28. }
  29. define('UNISH_DRUPAL_MAJOR_VERSION', $unish_drupal_major);
  30. // We read from env then globals then default to mysql.
  31. $unish_db_url = 'mysql://root:@127.0.0.1';
  32. if (getenv('UNISH_DB_URL')) {
  33. $unish_db_url = getenv('UNISH_DB_URL');
  34. }
  35. elseif (isset($GLOBALS['UNISH_DB_URL'])) {
  36. $unish_db_url = $GLOBALS['UNISH_DB_URL'];
  37. }
  38. define('UNISH_DB_URL', $unish_db_url);
  39. // UNISH_DRUSH value can come from phpunit.xml.
  40. if (!defined('UNISH_DRUSH')) {
  41. // Let the UNISH_DRUSH environment variable override if set.
  42. $unish_drush = isset($_SERVER['UNISH_DRUSH']) ? $_SERVER['UNISH_DRUSH'] : NULL;
  43. $unish_drush = isset($GLOBALS['UNISH_DRUSH']) ? $GLOBALS['UNISH_DRUSH'] : $unish_drush;
  44. if (empty($unish_drush)) {
  45. $unish_drush = \Unish\UnishTestCase::is_windows() ? exec('for %i in (drush) do @echo. %~$PATH:i') : dirname(__DIR__) . '/drush';
  46. }
  47. define('UNISH_DRUSH', $unish_drush);
  48. }
  49. define('UNISH_TMP', realpath(getenv('UNISH_TMP') ? getenv('UNISH_TMP') : (isset($GLOBALS['UNISH_TMP']) ? $GLOBALS['UNISH_TMP'] : sys_get_temp_dir())));
  50. define('UNISH_SANDBOX', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-sandbox');
  51. // Cache dir lives outside the sandbox so that we get persistence across classes.
  52. define('UNISH_CACHE', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-cache');
  53. putenv("CACHE_PREFIX=" . UNISH_CACHE);
  54. // Wipe at beginning of run.
  55. if (file_exists(UNISH_CACHE)) {
  56. // TODO: We no longer clean up cache dir between runs. Much faster, but we
  57. // we should watch for subtle problems. To manually clean up, delete the
  58. // UNISH_TMP/drush-cache directory.
  59. // unish_file_delete_recursive($cache, TRUE);
  60. }
  61. else {
  62. $ret = mkdir(UNISH_CACHE, 0777, TRUE);
  63. }
  64. $home = UNISH_SANDBOX . '/home';
  65. putenv("HOME=$home");
  66. putenv("HOMEDRIVE=$home");
  67. putenv('ETC_PREFIX=' . UNISH_SANDBOX);
  68. putenv('SHARE_PREFIX=' . UNISH_SANDBOX);
  69. putenv('TEMP=' . UNISH_TMP);
  70. define('UNISH_USERGROUP', isset($GLOBALS['UNISH_USERGROUP']) ? $GLOBALS['UNISH_USERGROUP'] : NULL);
  71. define('UNISH_BACKEND_OUTPUT_DELIMITER', 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END');
  72. }
  73. /**
  74. * Deletes the specified file or directory and everything inside it.
  75. *
  76. * Usually respects read-only files and folders. To do a forced delete use
  77. * drush_delete_tmp_dir() or set the parameter $forced.
  78. *
  79. * This is essentially a copy of drush_delete_dir().
  80. *
  81. * @todo This sort of duplication isn't very DRY. This is bound to get out of
  82. * sync with drush_delete_dir(), as in fact it already has before.
  83. *
  84. * @param string $dir
  85. * The file or directory to delete.
  86. * @param bool $force
  87. * Whether or not to try everything possible to delete the directory, even if
  88. * it's read-only. Defaults to FALSE.
  89. * @param bool $follow_symlinks
  90. * Whether or not to delete symlinked files. Defaults to FALSE--simply
  91. * unlinking symbolic links.
  92. *
  93. * @return bool
  94. * FALSE on failure, TRUE if everything was deleted.
  95. *
  96. * @see drush_delete_dir()
  97. */
  98. function unish_file_delete_recursive($dir, $force = FALSE, $follow_symlinks = FALSE) {
  99. // Do not delete symlinked files, only unlink symbolic links
  100. if (is_link($dir) && !$follow_symlinks) {
  101. return unlink($dir);
  102. }
  103. // Allow to delete symlinks even if the target doesn't exist.
  104. if (!is_link($dir) && !file_exists($dir)) {
  105. return TRUE;
  106. }
  107. if (!is_dir($dir)) {
  108. if ($force) {
  109. // Force deletion of items with readonly flag.
  110. @chmod($dir, 0777);
  111. }
  112. return unlink($dir);
  113. }
  114. if (unish_delete_dir_contents($dir, $force) === FALSE) {
  115. return FALSE;
  116. }
  117. if ($force) {
  118. // Force deletion of items with readonly flag.
  119. @chmod($dir, 0777);
  120. }
  121. return rmdir($dir);
  122. }
  123. /**
  124. * Deletes the contents of a directory.
  125. *
  126. * This is essentially a copy of drush_delete_dir_contents().
  127. *
  128. * @param string $dir
  129. * The directory to delete.
  130. * @param bool $force
  131. * Whether or not to try everything possible to delete the contents, even if
  132. * they're read-only. Defaults to FALSE.
  133. *
  134. * @return bool
  135. * FALSE on failure, TRUE if everything was deleted.
  136. *
  137. * @see drush_delete_dir_contents()
  138. */
  139. function unish_delete_dir_contents($dir, $force = FALSE) {
  140. $scandir = @scandir($dir);
  141. if (!is_array($scandir)) {
  142. return FALSE;
  143. }
  144. foreach ($scandir as $item) {
  145. if ($item == '.' || $item == '..') {
  146. continue;
  147. }
  148. if ($force) {
  149. @chmod($dir, 0777);
  150. }
  151. if (!unish_file_delete_recursive($dir . '/' . $item, $force)) {
  152. return FALSE;
  153. }
  154. }
  155. return TRUE;
  156. }