5 * Initialize a sandboxed environment. Starts with call unish_init() at bottom.
8 // This code copied from drush_bootstrap_prepare() except for 1st line.
9 $drush_base_path = dirname(__DIR__);
10 $local_vendor_path = $drush_base_path . '/vendor/autoload.php';
11 $global_vendor_path = $drush_base_path . '/../../../vendor/autoload.php';
12 // Check for a local composer install or a global composer install. Vendor dirs are in different spots.
13 if ((!@include $local_vendor_path) && (!@include $global_vendor_path)) {
14 $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";
22 * Initialize our environment at the start of each run (i.e. suite).
24 function unish_init() {
25 // Default drupal major version to run tests over.
26 $unish_drupal_major = '8';
27 if (getenv('UNISH_DRUPAL_MAJOR_VERSION')) {
28 $unish_drupal_major = getenv('UNISH_DRUPAL_MAJOR_VERSION');
30 elseif (isset($GLOBALS['UNISH_DRUPAL_MAJOR_VERSION'])) {
31 $unish_drupal_major = $GLOBALS['UNISH_DRUPAL_MAJOR_VERSION'];
33 define('UNISH_DRUPAL_MAJOR_VERSION', $unish_drupal_major);
34 $unish_drupal_minor = '';
35 if ($unish_drupal_major == 8) {
36 if (getenv('UNISH_DRUPAL_MINOR_VERSION')) {
37 $unish_drupal_minor = '.' . getenv('UNISH_DRUPAL_MINOR_VERSION');
39 elseif (isset($GLOBALS['UNISH_DRUPAL_MINOR_VERSION'])) {
40 $unish_drupal_minor = '.' . $GLOBALS['UNISH_DRUPAL_MINOR_VERSION'];
43 define('UNISH_DRUPAL_MINOR_VERSION', $unish_drupal_minor);
45 // We read from env then globals then default to mysql.
46 $unish_db_url = 'mysql://root:@127.0.0.1';
47 if (getenv('UNISH_DB_URL')) {
48 $unish_db_url = getenv('UNISH_DB_URL');
50 elseif (isset($GLOBALS['UNISH_DB_URL'])) {
51 $unish_db_url = $GLOBALS['UNISH_DB_URL'];
53 define('UNISH_DB_URL', $unish_db_url);
55 // UNISH_DRUSH value can come from phpunit.xml.
56 if (!defined('UNISH_DRUSH')) {
57 // Let the UNISH_DRUSH environment variable override if set.
58 $unish_drush = isset($_SERVER['UNISH_DRUSH']) ? $_SERVER['UNISH_DRUSH'] : NULL;
59 $unish_drush = isset($GLOBALS['UNISH_DRUSH']) ? $GLOBALS['UNISH_DRUSH'] : $unish_drush;
60 if (empty($unish_drush)) {
61 $script = \Unish\UnitUnishTestCase::is_windows() ? 'dr.bat' : 'drush';
62 $unish_drush = dirname(__DIR__) . DIRECTORY_SEPARATOR . $script;
64 define('UNISH_DRUSH', $unish_drush);
67 define('UNISH_TMP', realpath(getenv('UNISH_TMP') ? getenv('UNISH_TMP') : (isset($GLOBALS['UNISH_TMP']) ? $GLOBALS['UNISH_TMP'] : sys_get_temp_dir())));
68 define('UNISH_SANDBOX', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-sandbox');
70 // Cache dir lives outside the sandbox so that we get persistence across classes.
71 define('UNISH_CACHE', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-cache');
72 putenv("CACHE_PREFIX=" . UNISH_CACHE);
73 // Wipe at beginning of run.
74 if (file_exists(UNISH_CACHE)) {
75 // TODO: We no longer clean up cache dir between runs. Much faster, but we
76 // we should watch for subtle problems. To manually clean up, delete the
77 // UNISH_TMP/drush-cache directory.
78 // unish_file_delete_recursive($cache, TRUE);
81 $ret = mkdir(UNISH_CACHE, 0777, TRUE);
84 $home = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'home';
86 putenv("HOMEDRIVE=$home");
88 putenv('ETC_PREFIX=' . UNISH_SANDBOX);
89 putenv('SHARE_PREFIX=' . UNISH_SANDBOX);
90 putenv('TEMP=' . UNISH_TMP);
92 define('UNISH_USERGROUP', isset($GLOBALS['UNISH_USERGROUP']) ? $GLOBALS['UNISH_USERGROUP'] : NULL);
94 define('UNISH_BACKEND_OUTPUT_DELIMITER', 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END');
98 * Deletes the specified file or directory and everything inside it.
100 * Usually respects read-only files and folders. To do a forced delete use
101 * drush_delete_tmp_dir() or set the parameter $forced.
103 * To avoid permission denied error on Windows, make sure your CWD is not
104 * inside the directory being deleted.
106 * This is essentially a copy of drush_delete_dir().
108 * @todo This sort of duplication isn't very DRY. This is bound to get out of
109 * sync with drush_delete_dir(), as in fact it already has before.
112 * The file or directory to delete.
114 * Whether or not to try everything possible to delete the directory, even if
115 * it's read-only. Defaults to FALSE.
116 * @param bool $follow_symlinks
117 * Whether or not to delete symlinked files. Defaults to FALSE--simply
118 * unlinking symbolic links.
121 * FALSE on failure, TRUE if everything was deleted.
123 * @see drush_delete_dir()
125 function unish_file_delete_recursive($dir, $force = TRUE, $follow_symlinks = FALSE) {
126 // Do not delete symlinked files, only unlink symbolic links
127 if (is_link($dir) && !$follow_symlinks) {
130 // Allow to delete symlinks even if the target doesn't exist.
131 if (!is_link($dir) && !file_exists($dir)) {
136 // Force deletion of items with readonly flag.
141 if (unish_delete_dir_contents($dir, $force) === FALSE) {
145 // Force deletion of items with readonly flag.
152 * Deletes the contents of a directory.
154 * This is essentially a copy of drush_delete_dir_contents().
157 * The directory to delete.
159 * Whether or not to try everything possible to delete the contents, even if
160 * they're read-only. Defaults to FALSE.
163 * FALSE on failure, TRUE if everything was deleted.
165 * @see drush_delete_dir_contents()
167 function unish_delete_dir_contents($dir, $force = FALSE) {
168 $scandir = @scandir($dir);
169 if (!is_array($scandir)) {
173 foreach ($scandir as $item) {
174 if ($item == '.' || $item == '..') {
180 if (!unish_file_delete_recursive($dir . '/' . $item, $force)) {