|
12 | 12 | use WP_CLI\Process; |
13 | 13 | use WP_CLI\Utils; |
14 | 14 |
|
15 | | -/** |
16 | | - * Load required support files as needed before heading into the Behat context. |
17 | | - */ |
18 | | -function wpcli_bootstrap_behat_feature_context() { |
19 | | - // We try to detect the vendor folder in the most probable locations. |
20 | | - $vendor_locations = [ |
21 | | - // wp-cli/wp-cli-tests is the root project. |
22 | | - dirname( dirname( __DIR__ ) ) . '/vendor', |
23 | | - // wp-cli/wp-cli-tests is a dependency. |
24 | | - dirname( dirname( dirname( dirname( __DIR__ ) ) ) ), |
25 | | - ]; |
26 | | - |
27 | | - $vendor_folder = ''; |
28 | | - foreach ( $vendor_locations as $location ) { |
29 | | - if ( |
30 | | - is_dir( $location ) |
31 | | - && is_readable( $location ) |
32 | | - && is_file( "{$location}/autoload.php" ) |
33 | | - ) { |
34 | | - $vendor_folder = $location; |
35 | | - break; |
36 | | - } |
37 | | - } |
38 | | - |
39 | | - // Didn't manage to detect a valid vendor folder. |
40 | | - if ( empty( $vendor_folder ) ) { |
41 | | - return; |
42 | | - } |
43 | | - |
44 | | - // We assume the vendor folder is located in the project root folder. |
45 | | - $project_folder = dirname( $vendor_folder ); |
46 | | - |
47 | | - // Now we need to detect the location of wp-cli/wp-cli package. |
48 | | - $framework_locations = [ |
49 | | - // wp-cli/wp-cli is the root project. |
50 | | - $project_folder, |
51 | | - // wp-cli/wp-cli is a dependency. |
52 | | - "{$vendor_folder}/wp-cli/wp-cli", |
53 | | - ]; |
54 | | - |
55 | | - $framework_folder = ''; |
56 | | - foreach ( $framework_locations as $location ) { |
57 | | - if ( |
58 | | - is_dir( $location ) |
59 | | - && is_readable( $location ) |
60 | | - && is_file( "{$location}/php/utils.php" ) |
61 | | - ) { |
62 | | - $framework_folder = $location; |
63 | | - break; |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - // Load helper functionality that is needed for the tests. |
68 | | - require_once "{$framework_folder}/php/utils.php"; |
69 | | - require_once "{$framework_folder}/php/WP_CLI/Process.php"; |
70 | | - require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php"; |
71 | | - |
72 | | - // Manually load Composer file includes by generating a config with require: |
73 | | - // statements for each file. |
74 | | - $project_composer = "{$project_folder}/composer.json"; |
75 | | - if ( ! file_exists( $project_composer ) ) { |
76 | | - return; |
77 | | - } |
78 | | - |
79 | | - $composer = json_decode( file_get_contents( $project_composer ) ); |
80 | | - if ( empty( $composer->autoload->files ) ) { |
81 | | - return; |
82 | | - } |
83 | | - |
84 | | - $contents = "require:\n"; |
85 | | - foreach ( $composer->autoload->files as $file ) { |
86 | | - $contents .= " - {$project_folder}/{$file}\n"; |
87 | | - } |
88 | | - |
89 | | - $temp_folder = sys_get_temp_dir() . '/wp-cli-package-test'; |
90 | | - if ( |
91 | | - ! is_dir( $temp_folder ) |
92 | | - && ! mkdir( $temp_folder ) |
93 | | - && ! is_dir( $temp_folder ) |
94 | | - ) { |
95 | | - return; |
96 | | - } |
97 | | - |
98 | | - $project_config = "{$temp_folder}/config.yml"; |
99 | | - file_put_contents( $project_config, $contents ); |
100 | | - putenv( 'WP_CLI_CONFIG_PATH=' . $project_config ); |
101 | | -} |
102 | | - |
103 | | -wpcli_bootstrap_behat_feature_context(); |
104 | | - |
105 | 15 | /** |
106 | 16 | * Features context. |
107 | 17 | */ |
@@ -183,45 +93,92 @@ class FeatureContext implements SnippetAcceptingContext { |
183 | 93 | * |
184 | 94 | * @return string Absolute path to the Composer vendor folder. |
185 | 95 | */ |
186 | | - private static function get_vendor_dir() { |
187 | | - static $vendor_dir = null; |
| 96 | + public static function get_vendor_dir() { |
| 97 | + static $vendor_folder = null; |
188 | 98 |
|
189 | | - if ( null !== $vendor_dir ) { |
190 | | - return $vendor_dir; |
| 99 | + if ( null !== $vendor_folder ) { |
| 100 | + return $vendor_folder; |
191 | 101 | } |
192 | 102 |
|
193 | | - $paths = [ |
194 | | - dirname( dirname( __DIR__ ) ) . '/vendor/bin/wp', |
195 | | - dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . '/bin/wp', |
196 | | - dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) . '/bin/wp', |
| 103 | + // We try to detect the vendor folder in the most probable locations. |
| 104 | + $vendor_locations = [ |
| 105 | + // wp-cli/wp-cli-tests is the root project. |
| 106 | + dirname( dirname( __DIR__ ) ) . '/vendor', |
| 107 | + // wp-cli/wp-cli-tests is a dependency. |
| 108 | + dirname( dirname( dirname( dirname( __DIR__ ) ) ) ), |
197 | 109 | ]; |
198 | 110 |
|
199 | | - foreach ( $paths as $path ) { |
200 | | - if ( file_exists( $path ) && is_executable( $path ) ) { |
201 | | - $vendor_dir = (string) realpath( dirname( dirname( $path ) ) ); |
202 | | - |
| 111 | + $vendor_folder = ''; |
| 112 | + foreach ( $vendor_locations as $location ) { |
| 113 | + if ( |
| 114 | + is_dir( $location ) |
| 115 | + && is_readable( $location ) |
| 116 | + && is_file( "{$location}/autoload.php" ) |
| 117 | + ) { |
| 118 | + $vendor_folder = $location; |
203 | 119 | break; |
204 | 120 | } |
205 | 121 | } |
206 | 122 |
|
207 | | - if ( null === $vendor_dir ) { |
208 | | - // Did not detect WP-CLI binary, so make a random guess. |
209 | | - $vendor_dir = '/usr/local/bin'; |
| 123 | + return $vendor_folder; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Get the path to the WP-CLI framework folder. |
| 128 | + * |
| 129 | + * @return string Absolute path to the WP-CLI framework folder. |
| 130 | + */ |
| 131 | + public static function get_framework_dir() { |
| 132 | + static $framework_folder = null; |
| 133 | + |
| 134 | + if ( null !== $framework_folder ) { |
| 135 | + return $framework_folder; |
| 136 | + } |
| 137 | + |
| 138 | + $vendor_folder = self::get_vendor_dir(); |
| 139 | + |
| 140 | + // Now we need to detect the location of wp-cli/wp-cli package. |
| 141 | + $framework_locations = [ |
| 142 | + // wp-cli/wp-cli is the root project. |
| 143 | + dirname( $vendor_folder ), |
| 144 | + // wp-cli/wp-cli is a dependency. |
| 145 | + "{$vendor_folder}/wp-cli/wp-cli", |
| 146 | + ]; |
| 147 | + |
| 148 | + $framework_folder = ''; |
| 149 | + foreach ( $framework_locations as $location ) { |
| 150 | + if ( |
| 151 | + is_dir( $location ) |
| 152 | + && is_readable( $location ) |
| 153 | + && is_file( "{$location}/php/utils.php" ) |
| 154 | + ) { |
| 155 | + $framework_folder = $location; |
| 156 | + break; |
| 157 | + } |
210 | 158 | } |
211 | 159 |
|
212 | | - return $vendor_dir; |
| 160 | + return $framework_folder; |
213 | 161 | } |
214 | 162 |
|
215 | 163 | /** |
216 | 164 | * Get the environment variables required for launched `wp` processes |
217 | 165 | */ |
218 | 166 | private static function get_process_env_variables() { |
219 | 167 | // Ensure we're using the expected `wp` binary. |
220 | | - $bin_dir = getenv( 'WP_CLI_BIN_DIR' ) ?: realpath( dirname( dirname( __DIR__ ) ) . '/bin' ); |
221 | | - $vendor_dir = self::get_vendor_dir(); |
| 168 | + $bin_path = getenv( 'WP_CLI_BIN_DIR' ) ?: realpath( self::get_vendor_dir() . '/bin' ); |
| 169 | + wp_cli_behat_env_debug( "WP-CLI binary path: {$bin_path}" ); |
| 170 | + |
| 171 | + if ( ! file_exists( "{$bin_path}/wp" ) ) { |
| 172 | + wp_cli_behat_env_debug( "WARNING: No file named 'wp' found in the provided/detected path." ); |
| 173 | + } |
| 174 | + |
| 175 | + if ( ! is_executable( "{$bin_path}/wp" ) ) { |
| 176 | + wp_cli_behat_env_debug( "WARNING: File named 'wp' found in the provided/detected path is not executable." ); |
| 177 | + } |
| 178 | + |
222 | 179 | $path_separator = Utils\is_windows() ? ';' : ':'; |
223 | 180 | $env = array( |
224 | | - 'PATH' => $bin_dir . $path_separator . $vendor_dir . $path_separator . getenv( 'PATH' ), |
| 181 | + 'PATH' => $bin_path . $path_separator . getenv( 'PATH' ), |
225 | 182 | 'BEHAT_RUN' => 1, |
226 | 183 | 'HOME' => sys_get_temp_dir() . '/wp-cli-home', |
227 | 184 | ); |
@@ -256,10 +213,17 @@ private static function get_process_env_variables() { |
256 | 213 | $env['TRAVIS_BUILD_DIR'] = $travis_build_dir; |
257 | 214 | } |
258 | 215 |
|
| 216 | + // Dump environment for debugging purposes, but before adding the GitHub token. |
| 217 | + wp_cli_behat_env_debug( 'Environment:' ); |
| 218 | + foreach ( $env as $key => $value ) { |
| 219 | + wp_cli_behat_env_debug( " [{$key}] => {$value}" ); |
| 220 | + } |
| 221 | + |
259 | 222 | $github_token = getenv( 'GITHUB_TOKEN' ); |
260 | 223 | if ( false !== $github_token ) { |
261 | 224 | $env['GITHUB_TOKEN'] = $github_token; |
262 | 225 | } |
| 226 | + |
263 | 227 | return $env; |
264 | 228 | } |
265 | 229 |
|
@@ -334,6 +298,10 @@ public static function prepare( BeforeSuiteScope $scope ) { |
334 | 298 | echo $result->stdout; |
335 | 299 | echo PHP_EOL; |
336 | 300 |
|
| 301 | + if ( getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) { |
| 302 | + exit; |
| 303 | + } |
| 304 | + |
337 | 305 | self::cache_wp_files(); |
338 | 306 |
|
339 | 307 | $result = Process::create( Utils\esc_cmd( 'wp core version --debug --path=%s', self::$cache_dir ), null, self::get_process_env_variables() )->run_check(); |
@@ -929,7 +897,7 @@ public function composer_add_wp_cli_local_repository() { |
929 | 897 |
|
930 | 898 | $env = self::get_process_env_variables(); |
931 | 899 | $src = isset( $env['TRAVIS_BUILD_DIR'] ) ? $env['TRAVIS_BUILD_DIR'] : realpath( |
932 | | - __DIR__ . '/../wp-cli-tests/' |
| 900 | + __DIR__ . '/../../wp-cli-tests/' |
933 | 901 | ); |
934 | 902 |
|
935 | 903 | self::copy_dir( $src, self::$composer_local_repository . '/' ); |
@@ -1185,5 +1153,77 @@ private static function log_proc_method_run_time( $key, $start_time ) { |
1185 | 1153 | self::$proc_method_run_times[ $key ][0] += $run_time; |
1186 | 1154 | self::$proc_method_run_times[ $key ][1]++; |
1187 | 1155 | } |
| 1156 | +} |
| 1157 | + |
| 1158 | +function wp_cli_behat_env_debug( $message ) { |
| 1159 | + if ( ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) { |
| 1160 | + return; |
| 1161 | + } |
| 1162 | + |
| 1163 | + echo "{$message}\n"; |
| 1164 | +} |
| 1165 | + |
| 1166 | +/** |
| 1167 | + * Load required support files as needed before heading into the Behat context. |
| 1168 | + */ |
| 1169 | +function wpcli_bootstrap_behat_feature_context() { |
| 1170 | + $vendor_folder = FeatureContext::get_vendor_dir(); |
| 1171 | + wp_cli_behat_env_debug( "Vendor folder location: {$vendor_folder}" ); |
| 1172 | + |
| 1173 | + // Didn't manage to detect a valid vendor folder. |
| 1174 | + if ( empty( $vendor_folder ) ) { |
| 1175 | + return; |
| 1176 | + } |
| 1177 | + |
| 1178 | + // We assume the vendor folder is located in the project root folder. |
| 1179 | + $project_folder = dirname( $vendor_folder ); |
| 1180 | + |
| 1181 | + $framework_folder = FeatureContext::get_framework_dir(); |
| 1182 | + wp_cli_behat_env_debug( "Framework folder location: {$framework_folder}" ); |
| 1183 | + |
| 1184 | + // Didn't manage to detect a valid framework folder. |
| 1185 | + if ( empty( $vendor_folder ) ) { |
| 1186 | + return; |
| 1187 | + } |
| 1188 | + |
| 1189 | + // Load helper functionality that is needed for the tests. |
| 1190 | + require_once "{$framework_folder}/php/utils.php"; |
| 1191 | + require_once "{$framework_folder}/php/WP_CLI/Process.php"; |
| 1192 | + require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php"; |
| 1193 | + |
| 1194 | + // Manually load Composer file includes by generating a config with require: |
| 1195 | + // statements for each file. |
| 1196 | + $project_composer = "{$project_folder}/composer.json"; |
| 1197 | + if ( ! file_exists( $project_composer ) ) { |
| 1198 | + return; |
| 1199 | + } |
| 1200 | + |
| 1201 | + $composer = json_decode( file_get_contents( $project_composer ) ); |
| 1202 | + if ( empty( $composer->autoload->files ) ) { |
| 1203 | + return; |
| 1204 | + } |
| 1205 | + |
| 1206 | + $contents = "require:\n"; |
| 1207 | + foreach ( $composer->autoload->files as $file ) { |
| 1208 | + $contents .= " - {$project_folder}/{$file}\n"; |
| 1209 | + } |
| 1210 | + |
| 1211 | + $temp_folder = sys_get_temp_dir() . '/wp-cli-package-test'; |
| 1212 | + if ( |
| 1213 | + ! is_dir( $temp_folder ) |
| 1214 | + && ! mkdir( $temp_folder ) |
| 1215 | + && ! is_dir( $temp_folder ) |
| 1216 | + ) { |
| 1217 | + return; |
| 1218 | + } |
| 1219 | + |
| 1220 | + $project_config = "{$temp_folder}/config.yml"; |
| 1221 | + file_put_contents( $project_config, $contents ); |
| 1222 | + putenv( 'WP_CLI_CONFIG_PATH=' . $project_config ); |
| 1223 | + |
| 1224 | + wp_cli_behat_env_debug( "Project config file location: {$project_config}" ); |
| 1225 | + wp_cli_behat_env_debug( "Project config:\n{$contents}" ); |
1188 | 1226 |
|
1189 | 1227 | } |
| 1228 | + |
| 1229 | +wpcli_bootstrap_behat_feature_context(); |
0 commit comments