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);
35 // We read from env then globals then default to mysql.
36 $unish_db_url = 'mysql://root:@127.0.0.1';
37 if (getenv('UNISH_DB_URL')) {
38 $unish_db_url = getenv('UNISH_DB_URL');
40 elseif (isset($GLOBALS['UNISH_DB_URL'])) {
41 $unish_db_url = $GLOBALS['UNISH_DB_URL'];
43 define('UNISH_DB_URL', $unish_db_url);
45 // UNISH_DRUSH value can come from phpunit.xml.
46 if (!defined('UNISH_DRUSH')) {
47 // Let the UNISH_DRUSH environment variable override if set.
48 $unish_drush = isset($_SERVER['UNISH_DRUSH']) ? $_SERVER['UNISH_DRUSH'] : NULL;
49 $unish_drush = isset($GLOBALS['UNISH_DRUSH']) ? $GLOBALS['UNISH_DRUSH'] : $unish_drush;
50 if (empty($unish_drush)) {
51 $script = \Unish\UnitUnishTestCase::is_windows() ? 'dr.bat' : 'drush';
52 $unish_drush = dirname(__DIR__) . DIRECTORY_SEPARATOR . $script;
54 define('UNISH_DRUSH', $unish_drush);
57 define('UNISH_TMP', realpath(getenv('UNISH_TMP') ? getenv('UNISH_TMP') : (isset($GLOBALS['UNISH_TMP']) ? $GLOBALS['UNISH_TMP'] : sys_get_temp_dir())));
58 define('UNISH_SANDBOX', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-sandbox');
60 // Cache dir lives outside the sandbox so that we get persistence across classes.
61 define('UNISH_CACHE', UNISH_TMP . DIRECTORY_SEPARATOR . 'drush-cache');
62 putenv("CACHE_PREFIX=" . UNISH_CACHE);
63 // Wipe at beginning of run.
64 if (file_exists(UNISH_CACHE)) {
65 // TODO: We no longer clean up cache dir between runs. Much faster, but we
66 // we should watch for subtle problems. To manually clean up, delete the
67 // UNISH_TMP/drush-cache directory.
68 // unish_file_delete_recursive($cache, TRUE);
71 $ret = mkdir(UNISH_CACHE, 0777, TRUE);
74 $home = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'home';
76 putenv("HOMEDRIVE=$home");
78 putenv('ETC_PREFIX=' . UNISH_SANDBOX);
79 putenv('SHARE_PREFIX=' . UNISH_SANDBOX);
80 putenv('TEMP=' . UNISH_TMP);
82 define('UNISH_USERGROUP', isset($GLOBALS['UNISH_USERGROUP']) ? $GLOBALS['UNISH_USERGROUP'] : NULL);
84 define('UNISH_BACKEND_OUTPUT_DELIMITER', 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END');
88 * Deletes the specified file or directory and everything inside it.
90 * Usually respects read-only files and folders. To do a forced delete use
91 * drush_delete_tmp_dir() or set the parameter $forced.
93 * To avoid permission denied error on Windows, make sure your CWD is not
94 * inside the directory being deleted.
96 * This is essentially a copy of drush_delete_dir().
98 * @todo This sort of duplication isn't very DRY. This is bound to get out of
99 * sync with drush_delete_dir(), as in fact it already has before.
102 * The file or directory to delete.
104 * Whether or not to try everything possible to delete the directory, even if
105 * it's read-only. Defaults to FALSE.
106 * @param bool $follow_symlinks
107 * Whether or not to delete symlinked files. Defaults to FALSE--simply
108 * unlinking symbolic links.
111 * FALSE on failure, TRUE if everything was deleted.
113 * @see drush_delete_dir()
115 function unish_file_delete_recursive($dir, $force = TRUE, $follow_symlinks = FALSE) {
116 // Do not delete symlinked files, only unlink symbolic links
117 if (is_link($dir) && !$follow_symlinks) {
120 // Allow to delete symlinks even if the target doesn't exist.
121 if (!is_link($dir) && !file_exists($dir)) {
126 // Force deletion of items with readonly flag.
131 if (unish_delete_dir_contents($dir, $force) === FALSE) {
135 // Force deletion of items with readonly flag.
142 * Deletes the contents of a directory.
144 * This is essentially a copy of drush_delete_dir_contents().
147 * The directory to delete.
149 * Whether or not to try everything possible to delete the contents, even if
150 * they're read-only. Defaults to FALSE.
153 * FALSE on failure, TRUE if everything was deleted.
155 * @see drush_delete_dir_contents()
157 function unish_delete_dir_contents($dir, $force = FALSE) {
158 $scandir = @scandir($dir);
159 if (!is_array($scandir)) {
163 foreach ($scandir as $item) {
164 if ($item == '.' || $item == '..') {
170 if (!unish_file_delete_recursive($dir . '/' . $item, $force)) {