Skip to content

Commit 0264f85

Browse files
authored
Merge pull request #108 from wp-cli/add/behat-debug-env
2 parents b2c0db5 + 4a62dfc commit 0264f85

File tree

2 files changed

+155
-120
lines changed

2 files changed

+155
-120
lines changed

.github/workflows/testing.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,31 @@ jobs:
6565
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0']
6666
wp: ['latest']
6767
mysql: ['8.0']
68-
test: ["composer behat || composer behat-rerun"]
6968
include:
7069
- php: '5.6'
7170
wp: 'trunk'
7271
mysql: '8.0'
73-
test: "composer behat || composer behat-rerun"
7472
- php: '5.6'
7573
wp: 'trunk'
7674
mysql: '5.7'
77-
test: "composer behat || composer behat-rerun"
7875
- php: '5.6'
7976
wp: 'trunk'
8077
mysql: '5.6'
81-
test: "composer behat || composer behat-rerun"
8278
- php: '7.4'
8379
wp: 'trunk'
8480
mysql: '8.0'
85-
test: "composer behat || composer behat-rerun"
8681
- php: '8.0'
8782
wp: 'trunk'
8883
mysql: '8.0'
89-
test: "composer behat || composer behat-rerun"
9084
- php: '8.0'
9185
wp: 'trunk'
9286
mysql: '5.7'
93-
test: "composer behat || composer behat-rerun"
9487
- php: '8.0'
9588
wp: 'trunk'
9689
mysql: '5.6'
97-
test: "composer behat || composer behat-rerun"
9890
- php: '5.6'
9991
wp: '3.7'
10092
mysql: '5.6'
101-
test: "composer behat || composer behat-rerun"
10293
runs-on: ubuntu-20.04
10394

10495
services:
@@ -165,8 +156,12 @@ jobs:
165156
if: steps.check_files.outputs.files_exists == 'true'
166157
run: composer prepare-tests
167158

159+
- name: Check Behat environment
160+
if: steps.check_files.outputs.files_exists == 'true'
161+
run: WP_CLI_TEST_DEBUG_BEHAT_ENV=1 composer behat
162+
168163
- name: Run Behat
169164
if: steps.check_files.outputs.files_exists == 'true'
170165
env:
171166
WP_VERSION: '${{ matrix.wp }}'
172-
run: ${{ matrix.test }}
167+
run: composer behat || composer behat-rerun

src/Context/FeatureContext.php

Lines changed: 150 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -12,96 +12,6 @@
1212
use WP_CLI\Process;
1313
use WP_CLI\Utils;
1414

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-
10515
/**
10616
* Features context.
10717
*/
@@ -183,45 +93,92 @@ class FeatureContext implements SnippetAcceptingContext {
18393
*
18494
* @return string Absolute path to the Composer vendor folder.
18595
*/
186-
private static function get_vendor_dir() {
187-
static $vendor_dir = null;
96+
public static function get_vendor_dir() {
97+
static $vendor_folder = null;
18898

189-
if ( null !== $vendor_dir ) {
190-
return $vendor_dir;
99+
if ( null !== $vendor_folder ) {
100+
return $vendor_folder;
191101
}
192102

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__ ) ) ) ),
197109
];
198110

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;
203119
break;
204120
}
205121
}
206122

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+
}
210158
}
211159

212-
return $vendor_dir;
160+
return $framework_folder;
213161
}
214162

215163
/**
216164
* Get the environment variables required for launched `wp` processes
217165
*/
218166
private static function get_process_env_variables() {
219167
// 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+
222179
$path_separator = Utils\is_windows() ? ';' : ':';
223180
$env = array(
224-
'PATH' => $bin_dir . $path_separator . $vendor_dir . $path_separator . getenv( 'PATH' ),
181+
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
225182
'BEHAT_RUN' => 1,
226183
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
227184
);
@@ -256,10 +213,17 @@ private static function get_process_env_variables() {
256213
$env['TRAVIS_BUILD_DIR'] = $travis_build_dir;
257214
}
258215

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+
259222
$github_token = getenv( 'GITHUB_TOKEN' );
260223
if ( false !== $github_token ) {
261224
$env['GITHUB_TOKEN'] = $github_token;
262225
}
226+
263227
return $env;
264228
}
265229

@@ -334,6 +298,10 @@ public static function prepare( BeforeSuiteScope $scope ) {
334298
echo $result->stdout;
335299
echo PHP_EOL;
336300

301+
if ( getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) {
302+
exit;
303+
}
304+
337305
self::cache_wp_files();
338306

339307
$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() {
929897

930898
$env = self::get_process_env_variables();
931899
$src = isset( $env['TRAVIS_BUILD_DIR'] ) ? $env['TRAVIS_BUILD_DIR'] : realpath(
932-
__DIR__ . '/../wp-cli-tests/'
900+
__DIR__ . '/../../wp-cli-tests/'
933901
);
934902

935903
self::copy_dir( $src, self::$composer_local_repository . '/' );
@@ -1185,5 +1153,77 @@ private static function log_proc_method_run_time( $key, $start_time ) {
11851153
self::$proc_method_run_times[ $key ][0] += $run_time;
11861154
self::$proc_method_run_times[ $key ][1]++;
11871155
}
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}" );
11881226

11891227
}
1228+
1229+
wpcli_bootstrap_behat_feature_context();

0 commit comments

Comments
 (0)