be5b978e9e0a2bfba3809d0fcd4ee0c677cf9ede
[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
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');
39   }
40   elseif (isset($GLOBALS['UNISH_DB_URL'])) {
41     $unish_db_url = $GLOBALS['UNISH_DB_URL'];
42   }
43   define('UNISH_DB_URL', $unish_db_url);
44
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;
53     }
54     define('UNISH_DRUSH', $unish_drush);
55   }
56
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');
59
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);
69   }
70   else {
71     $ret = mkdir(UNISH_CACHE, 0777, TRUE);
72   }
73
74   $home = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'home';
75   putenv("HOME=$home");
76   putenv("HOMEDRIVE=$home");
77
78   putenv('ETC_PREFIX=' . UNISH_SANDBOX);
79   putenv('SHARE_PREFIX=' . UNISH_SANDBOX);
80   putenv('TEMP=' . UNISH_TMP);
81
82   define('UNISH_USERGROUP', isset($GLOBALS['UNISH_USERGROUP']) ? $GLOBALS['UNISH_USERGROUP'] : NULL);
83
84   define('UNISH_BACKEND_OUTPUT_DELIMITER', 'DRUSH_BACKEND_OUTPUT_START>>>%s<<<DRUSH_BACKEND_OUTPUT_END');
85 }
86
87 /**
88  * Deletes the specified file or directory and everything inside it.
89  *
90  * Usually respects read-only files and folders. To do a forced delete use
91  * drush_delete_tmp_dir() or set the parameter $forced.
92  *
93  * To avoid permission denied error on Windows, make sure your CWD is not
94  * inside the directory being deleted.
95  *
96  * This is essentially a copy of drush_delete_dir().
97  *
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.
100  *
101  * @param string $dir
102  *   The file or directory to delete.
103  * @param bool $force
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.
109  *
110  * @return bool
111  *   FALSE on failure, TRUE if everything was deleted.
112  *
113  * @see drush_delete_dir()
114  */
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) {
118     return unlink($dir);
119   }
120   // Allow to delete symlinks even if the target doesn't exist.
121   if (!is_link($dir) && !file_exists($dir)) {
122     return TRUE;
123   }
124   if (!is_dir($dir)) {
125     if ($force) {
126       // Force deletion of items with readonly flag.
127       @chmod($dir, 0777);
128     }
129     return unlink($dir);
130   }
131   if (unish_delete_dir_contents($dir, $force) === FALSE) {
132     return FALSE;
133   }
134   if ($force) {
135     // Force deletion of items with readonly flag.
136     @chmod($dir, 0777);
137   }
138   return rmdir($dir);
139 }
140
141 /**
142  * Deletes the contents of a directory.
143  *
144  * This is essentially a copy of drush_delete_dir_contents().
145  *
146  * @param string $dir
147  *   The directory to delete.
148  * @param bool $force
149  *   Whether or not to try everything possible to delete the contents, even if
150  *   they're read-only. Defaults to FALSE.
151  *
152  * @return bool
153  *   FALSE on failure, TRUE if everything was deleted.
154  *
155  * @see drush_delete_dir_contents()
156  */
157 function unish_delete_dir_contents($dir, $force = FALSE) {
158   $scandir = @scandir($dir);
159   if (!is_array($scandir)) {
160     return FALSE;
161   }
162
163   foreach ($scandir as $item) {
164     if ($item == '.' || $item == '..') {
165       continue;
166     }
167     if ($force) {
168       @chmod($dir, 0777);
169     }
170     if (!unish_file_delete_recursive($dir . '/' . $item, $force)) {
171       return FALSE;
172     }
173   }
174   return TRUE;
175 }