Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / tests / bootstrap.inc
1 <?php
2
3 /**
4  * @file
5  *   Initialize a sandboxed environment. Starts with call unish_init() at bottom.
6  */
7
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";
15   fwrite(STDERR, $msg);
16   return FALSE;
17 }
18
19 unish_init();
20
21 /**
22  * Initialize our environment at the start of each run (i.e. suite).
23  */
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');
29   }
30   elseif (isset($GLOBALS['UNISH_DRUPAL_MAJOR_VERSION'])) {
31     $unish_drupal_major = $GLOBALS['UNISH_DRUPAL_MAJOR_VERSION'];
32   }
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');
38     }
39     elseif (isset($GLOBALS['UNISH_DRUPAL_MINOR_VERSION'])) {
40       $unish_drupal_minor = '.' . $GLOBALS['UNISH_DRUPAL_MINOR_VERSION'];
41     }
42   }
43   define('UNISH_DRUPAL_MINOR_VERSION', $unish_drupal_minor);
44
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');
49   }
50   elseif (isset($GLOBALS['UNISH_DB_URL'])) {
51     $unish_db_url = $GLOBALS['UNISH_DB_URL'];
52   }
53   define('UNISH_DB_URL', $unish_db_url);
54
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;
63     }
64     define('UNISH_DRUSH', $unish_drush);
65   }
66
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');
69
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);
79   }
80   else {
81     $ret = mkdir(UNISH_CACHE, 0777, TRUE);
82   }
83
84   $home = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'home';
85   putenv("HOME=$home");
86   putenv("HOMEDRIVE=$home");
87
88   putenv('ETC_PREFIX=' . UNISH_SANDBOX);
89   putenv('SHARE_PREFIX=' . UNISH_SANDBOX);
90   putenv('TEMP=' . UNISH_TMP);
91
92   define('UNISH_USERGROUP', isset($GLOBALS['UNISH_USERGROUP']) ? $GLOBALS['UNISH_USERGROUP'] : NULL);
93
94   define('UNISH_BACKEND_OUTPUT_DELIMITER', 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END');
95 }
96
97 /**
98  * Deletes the specified file or directory and everything inside it.
99  *
100  * Usually respects read-only files and folders. To do a forced delete use
101  * drush_delete_tmp_dir() or set the parameter $forced.
102  *
103  * To avoid permission denied error on Windows, make sure your CWD is not
104  * inside the directory being deleted.
105  *
106  * This is essentially a copy of drush_delete_dir().
107  *
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.
110  *
111  * @param string $dir
112  *   The file or directory to delete.
113  * @param bool $force
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.
119  *
120  * @return bool
121  *   FALSE on failure, TRUE if everything was deleted.
122  *
123  * @see drush_delete_dir()
124  */
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) {
128     return unlink($dir);
129   }
130   // Allow to delete symlinks even if the target doesn't exist.
131   if (!is_link($dir) && !file_exists($dir)) {
132     return TRUE;
133   }
134   if (!is_dir($dir)) {
135     if ($force) {
136       // Force deletion of items with readonly flag.
137       @chmod($dir, 0777);
138     }
139     return unlink($dir);
140   }
141   if (unish_delete_dir_contents($dir, $force) === FALSE) {
142     return FALSE;
143   }
144   if ($force) {
145     // Force deletion of items with readonly flag.
146     @chmod($dir, 0777);
147   }
148   return rmdir($dir);
149 }
150
151 /**
152  * Deletes the contents of a directory.
153  *
154  * This is essentially a copy of drush_delete_dir_contents().
155  *
156  * @param string $dir
157  *   The directory to delete.
158  * @param bool $force
159  *   Whether or not to try everything possible to delete the contents, even if
160  *   they're read-only. Defaults to FALSE.
161  *
162  * @return bool
163  *   FALSE on failure, TRUE if everything was deleted.
164  *
165  * @see drush_delete_dir_contents()
166  */
167 function unish_delete_dir_contents($dir, $force = FALSE) {
168   $scandir = @scandir($dir);
169   if (!is_array($scandir)) {
170     return FALSE;
171   }
172
173   foreach ($scandir as $item) {
174     if ($item == '.' || $item == '..') {
175       continue;
176     }
177     if ($force) {
178       @chmod($dir, 0777);
179     }
180     if (!unish_file_delete_recursive($dir . '/' . $item, $force)) {
181       return FALSE;
182     }
183   }
184   return TRUE;
185 }