-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbehat-tags.php
More file actions
312 lines (275 loc) · 8.8 KB
/
behat-tags.php
File metadata and controls
312 lines (275 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* Generate a list of tags to skip during the test run.
*
* Require a minimum version of WordPress:
*
* @require-wp-4.0
* Scenario: Core translation CRUD
*
* Then use in bash script:
*
* BEHAT_TAGS=$(php behat-tags.php)
* vendor/bin/behat --format progress $BEHAT_TAGS
*/
function version_tags(
$prefix,
$current,
$operator = '<',
$features_folder = 'features'
) {
if ( ! $current ) {
return array();
}
$existing_tags = array();
$feature_files = glob( $features_folder . DIRECTORY_SEPARATOR . '*.feature' );
if ( ! empty( $feature_files ) ) {
foreach ( $feature_files as $feature_file ) {
$contents = (string) file_get_contents( $feature_file );
if ( preg_match_all( '/@' . $prefix . '-[0-9\.]+/', $contents, $matches ) ) {
$existing_tags = array_merge( $existing_tags, $matches[0] );
}
}
$existing_tags = array_unique( $existing_tags );
}
$skip_tags = array();
foreach ( $existing_tags as $tag ) {
$compare = str_replace( "@{$prefix}-", '', $tag );
if ( version_compare( $current, $compare, $operator ) ) {
$skip_tags[] = $tag;
}
}
return $skip_tags;
}
function get_db_type_and_version() {
// Detect which client binary is available.
$client_binary = null;
$output = array();
exec( 'command -v mysql 2>/dev/null', $output, $mysql_exit_code );
if ( 0 === $mysql_exit_code ) {
$client_binary = 'mysql';
} else {
$output = array();
exec( 'command -v mariadb 2>/dev/null', $output, $mariadb_exit_code );
if ( 0 === $mariadb_exit_code ) {
$client_binary = 'mariadb';
}
}
if ( null === $client_binary ) {
// No client binary found, return defaults.
return array(
'type' => 'mysql',
'version' => '',
);
}
// Build connection parameters from environment variables.
$host = getenv( 'WP_CLI_TEST_DBHOST' ) ?: 'localhost';
$user = getenv( 'WP_CLI_TEST_DBROOTUSER' ) ?: 'root';
$pass = getenv( 'WP_CLI_TEST_DBROOTPASS' );
// Build the command to get the server version.
$host_parts = explode( ':', $host );
$host_arg = '-h' . escapeshellarg( $host_parts[0] );
$port_arg = '';
if ( isset( $host_parts[1] ) ) {
// Check if it's a port number or socket path.
if ( is_numeric( $host_parts[1] ) ) {
$port_arg = ' --port=' . escapeshellarg( $host_parts[1] ) . ' --protocol=tcp';
} else {
$port_arg = ' --socket=' . escapeshellarg( $host_parts[1] ) . ' --protocol=socket';
}
}
$pass_arg = false !== $pass && '' !== $pass ? '-p' . escapeshellarg( $pass ) : '';
$cmd = sprintf(
'%s %s %s -u%s %s -e "SELECT VERSION()" --skip-column-names 2>/dev/null',
escapeshellcmd( $client_binary ),
$host_arg,
$port_arg,
escapeshellarg( $user ),
$pass_arg
);
$output = array();
$return_code = 0;
exec( $cmd, $output, $return_code );
$version_string = isset( $output[0] ) ? $output[0] : '';
// If the connection failed, fall back to client binary version.
if ( 0 !== $return_code || empty( $version_string ) ) {
$client_version_cmd = sprintf( '%s --version 2>/dev/null', escapeshellcmd( $client_binary ) );
$version_string = exec( $client_version_cmd );
}
// Detect database type from server version string.
$db_type = 'mysql';
if ( false !== stripos( $version_string, 'mariadb' ) ) {
$db_type = 'mariadb';
}
preg_match( '@[0-9]+\.[0-9]+\.[0-9]+@', $version_string, $version );
$db_version = isset( $version[0] ) ? $version[0] : '';
return array(
'type' => $db_type,
'version' => $db_version,
);
}
function get_db_version() {
$db_info = get_db_type_and_version();
return $db_info['version'];
}
$features_folder = getenv( 'BEHAT_FEATURES_FOLDER' ) ?: 'features';
$wp_version = getenv( 'WP_VERSION' );
$wp_version_reqs = array();
// Only apply @require-wp tags when WP_VERSION isn't 'latest', 'nightly' or 'trunk'.
// 'latest', 'nightly' and 'trunk' are expected to work with all features.
if ( $wp_version &&
! in_array( $wp_version, array( 'latest', 'nightly', 'trunk' ), true ) ) {
$wp_version_reqs = array_merge(
version_tags( 'require-wp', $wp_version, '<', $features_folder ),
version_tags( 'less-than-wp', $wp_version, '>=', $features_folder )
);
} else {
// But make sure @less-than-wp tags always exist for those special cases. (Note: @less-than-wp-latest etc won't work and shouldn't be used).
$wp_version_reqs = array_merge(
$wp_version_reqs,
version_tags( 'less-than-wp', '9999', '>=', $features_folder )
);
}
$skip_tags = array_merge(
$wp_version_reqs,
version_tags( 'require-php', PHP_VERSION, '<', $features_folder ),
// Note: this was '>' prior to WP-CLI 1.5.0 but the change is unlikely to
// cause BC issues as usually compared against major.minor only.
version_tags( 'less-than-php', PHP_VERSION, '>=', $features_folder )
);
// Skip GitHub API tests if `GITHUB_TOKEN` not available because of rate
// limiting. See https://github.com/wp-cli/wp-cli/issues/1612
if ( ! getenv( 'GITHUB_TOKEN' ) ) {
$skip_tags[] = '@github-api';
}
# Skip tests known to be broken.
$skip_tags[] = '@broken';
if ( 'sqlite' !== getenv( 'WP_CLI_TEST_OBJECT_CACHE' ) ) {
$skip_tags[] = '@require-object-cache';
} else {
$skip_tags[] = '@skip-object-cache';
}
if ( $wp_version && in_array( $wp_version, array( 'nightly', 'trunk' ), true ) ) {
$skip_tags[] = '@broken-trunk';
}
$db_info = get_db_type_and_version();
$db_version = $db_info['version'];
// Use detected database type from server, unless WP_CLI_TEST_DBTYPE is 'sqlite'.
$env_db_type = getenv( 'WP_CLI_TEST_DBTYPE' );
$db_type = 'sqlite' === $env_db_type ? 'sqlite' : $db_info['type'];
switch ( $db_type ) {
case 'mariadb':
$db_version = get_db_version();
$skip_tags = array_merge(
$skip_tags,
[ '@require-mysql', '@require-sqlite', '@skip-mariadb' ],
version_tags( 'require-mariadb', $db_version, '<', $features_folder ),
version_tags( 'less-than-mariadb', $db_version, '>=', $features_folder )
);
break;
case 'sqlite':
$skip_tags[] = '@require-mariadb';
$skip_tags[] = '@require-mysql';
$skip_tags[] = '@require-mysql-or-mariadb';
$skip_tags[] = '@skip-sqlite';
break;
case 'mysql':
default:
$db_version = get_db_version();
$skip_tags = array_merge(
$skip_tags,
[ '@require-mariadb', '@require-sqlite', '@skip-mysql' ],
version_tags( 'require-mysql', $db_version, '<', $features_folder ),
version_tags( 'less-than-mysql', $db_version, '>=', $features_folder )
);
break;
}
# Require PHP extension, eg 'imagick'.
function extension_tags( $features_folder = 'features' ) {
$extension_tags = array();
$feature_files = glob( $features_folder . DIRECTORY_SEPARATOR . '*.feature' );
if ( ! empty( $feature_files ) ) {
foreach ( $feature_files as $feature_file ) {
$contents = (string) file_get_contents( $feature_file );
if ( preg_match_all( '/@require-extension-[A-Za-z_]*/', $contents, $matches ) ) {
$extension_tags = array_merge( $extension_tags, $matches[0] );
}
}
$extension_tags = array_unique( $extension_tags );
}
$skip_tags = array();
$substr_start = strlen( '@require-extension-' );
foreach ( $extension_tags as $tag ) {
$extension = substr( $tag, $substr_start );
if ( ! extension_loaded( $extension ) ) {
$skip_tags[] = $tag;
}
}
return $skip_tags;
}
/**
* An array of tags for excluding tests based on the operating system.
*
* @param string $features_folder The folder where the feature files are located.
* @return array
*/
function os_tags( $features_folder = 'features' ) {
$os_tags = array();
$feature_files = glob( $features_folder . DIRECTORY_SEPARATOR . '*.feature' );
if ( ! empty( $feature_files ) ) {
foreach ( $feature_files as $feature_file ) {
$contents = (string) file_get_contents( $feature_file );
if ( preg_match_all( '/@(require-(windows|macos|linux)|skip-(windows|macos|linux))/', $contents, $matches ) ) {
$os_tags = array_merge( $os_tags, $matches[0] );
}
}
$os_tags = array_unique( $os_tags );
}
if ( empty( $os_tags ) ) {
return array();
}
$skip_tags = array();
$is_windows = 'Windows' === PHP_OS_FAMILY;
$is_macos = 'Darwin' === PHP_OS_FAMILY;
$is_linux = 'Linux' === PHP_OS_FAMILY;
foreach ( $os_tags as $tag ) {
switch ( $tag ) {
case '@require-windows':
if ( ! $is_windows ) {
$skip_tags[] = $tag;
}
break;
case '@require-macos':
if ( ! $is_macos ) {
$skip_tags[] = $tag;
}
break;
case '@require-linux':
if ( ! $is_linux ) {
$skip_tags[] = $tag;
}
break;
case '@skip-windows':
if ( $is_windows ) {
$skip_tags[] = $tag;
}
break;
case '@skip-macos':
if ( $is_macos ) {
$skip_tags[] = $tag;
}
break;
case '@skip-linux':
if ( $is_linux ) {
$skip_tags[] = $tag;
}
break;
}
}
return $skip_tags;
}
$skip_tags = array_merge( $skip_tags, extension_tags( $features_folder ) );
$skip_tags = array_merge( $skip_tags, os_tags( $features_folder ) );
if ( ! empty( $skip_tags ) ) {
echo '--tags=~' . implode( '&&~', $skip_tags );
}