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 http://docs.drush.org/en/master/install/\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 = '8';
  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. $script = \Unish\UnitUnishTestCase::is_windows() ? 'dr.bat' : 'drush';
  46. $unish_drush = dirname(__DIR__) . DIRECTORY_SEPARATOR . $script;
  47. }
  48. define('UNISH_DRUSH', $unish_drush);
  49. }
  50. define('UNISH_TMP', realpath(getenv('UNISH_TMP') ? getenv('UNISH_TMP') : (isset($GLOBALS['UNISH_TMP']) ? $GLOBALS['UNISH_TMP'] : sys_get_temp_dir())));
  51. define('UNISH_SANDBOX', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-sandbox');
  52. // Cache dir lives outside the sandbox so that we get persistence across classes.
  53. define('UNISH_CACHE', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-cache');
  54. putenv("CACHE_PREFIX=" . UNISH_CACHE);
  55. // Wipe at beginning of run.
  56. if (file_exists(UNISH_CACHE)) {
  57. // TODO: We no longer clean up cache dir between runs. Much faster, but we
  58. // we should watch for subtle problems. To manually clean up, delete the
  59. // UNISH_TMP/drush-cache directory.
  60. // unish_file_delete_recursive($cache, TRUE);
  61. }
  62. else {
  63. $ret = mkdir(UNISH_CACHE, 0777, TRUE);
  64. }
  65. $home = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'home';
  66. putenv("HOME=$home");
  67. putenv("HOMEDRIVE=$home");
  68. putenv('ETC_PREFIX=' . UNISH_SANDBOX);
  69. putenv('SHARE_PREFIX=' . UNISH_SANDBOX);
  70. putenv('TEMP=' . UNISH_TMP);
  71. define('UNISH_USERGROUP', isset($GLOBALS['UNISH_USERGROUP']) ? $GLOBALS['UNISH_USERGROUP'] : NULL);
  72. define('UNISH_BACKEND_OUTPUT_DELIMITER', 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END');
  73. }
  74. /**
  75. * Deletes the specified file or directory and everything inside it.
  76. *
  77. * Usually respects read-only files and folders. To do a forced delete use
  78. * drush_delete_tmp_dir() or set the parameter $forced.
  79. *
  80. * To avoid permission denied error on Windows, make sure your CWD is not
  81. * inside the directory being deleted.
  82. *
  83. * This is essentially a copy of drush_delete_dir().
  84. *
  85. * @todo This sort of duplication isn't very DRY. This is bound to get out of
  86. * sync with drush_delete_dir(), as in fact it already has before.
  87. *
  88. * @param string $dir
  89. * The file or directory to delete.
  90. * @param bool $force
  91. * Whether or not to try everything possible to delete the directory, even if
  92. * it's read-only. Defaults to FALSE.
  93. * @param bool $follow_symlinks
  94. * Whether or not to delete symlinked files. Defaults to FALSE--simply
  95. * unlinking symbolic links.
  96. *
  97. * @return bool
  98. * FALSE on failure, TRUE if everything was deleted.
  99. *
  100. * @see drush_delete_dir()
  101. */
  102. function unish_file_delete_recursive($dir, $force = TRUE, $follow_symlinks = FALSE) {
  103. // Do not delete symlinked files, only unlink symbolic links
  104. if (is_link($dir) && !$follow_symlinks) {
  105. return unlink($dir);
  106. }
  107. // Allow to delete symlinks even if the target doesn't exist.
  108. if (!is_link($dir) && !file_exists($dir)) {
  109. return TRUE;
  110. }
  111. if (!is_dir($dir)) {
  112. if ($force) {
  113. // Force deletion of items with readonly flag.
  114. @chmod($dir, 0777);
  115. }
  116. return unlink($dir);
  117. }
  118. if (unish_delete_dir_contents($dir, $force) === FALSE) {
  119. return FALSE;
  120. }
  121. if ($force) {
  122. // Force deletion of items with readonly flag.
  123. @chmod($dir, 0777);
  124. }
  125. return rmdir($dir);
  126. }
  127. /**
  128. * Deletes the contents of a directory.
  129. *
  130. * This is essentially a copy of drush_delete_dir_contents().
  131. *
  132. * @param string $dir
  133. * The directory to delete.
  134. * @param bool $force
  135. * Whether or not to try everything possible to delete the contents, even if
  136. * they're read-only. Defaults to FALSE.
  137. *
  138. * @return bool
  139. * FALSE on failure, TRUE if everything was deleted.
  140. *
  141. * @see drush_delete_dir_contents()
  142. */
  143. function unish_delete_dir_contents($dir, $force = FALSE) {
  144. $scandir = @scandir($dir);
  145. if (!is_array($scandir)) {
  146. return FALSE;
  147. }
  148. foreach ($scandir as $item) {
  149. if ($item == '.' || $item == '..') {
  150. continue;
  151. }
  152. if ($force) {
  153. @chmod($dir, 0777);
  154. }
  155. if (!unish_file_delete_recursive($dir . '/' . $item, $force)) {
  156. return FALSE;
  157. }
  158. }
  159. return TRUE;
  160. }