Skip to content

Commit 9ff648c

Browse files
committed
Add behat tags
1 parent 6cc33eb commit 9ff648c

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tests/tests/TestBehatTags.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,62 @@ public function test_behat_tags_db_version(): void {
314314
$output = $this->run_behat_tags_script();
315315
$this->assertSame( $expected, $output );
316316
}
317+
318+
public function test_behat_tags_os(): void {
319+
$env_github_token = getenv( 'GITHUB_TOKEN' );
320+
$db_type = getenv( 'WP_CLI_TEST_DBTYPE' );
321+
322+
putenv( 'GITHUB_TOKEN' );
323+
324+
file_put_contents( $this->temp_dir . DIRECTORY_SEPARATOR . 'features' . DIRECTORY_SEPARATOR . 'os.feature', '@require-windows @skip-windows @require-macos @skip-macos @require-linux @skip-linux' );
325+
326+
$expecteds = array();
327+
328+
switch ( $db_type ) {
329+
case 'mariadb':
330+
$expecteds[] = '~@require-mysql';
331+
$expecteds[] = '~@require-sqlite';
332+
break;
333+
case 'sqlite':
334+
$expecteds[] = '~@require-mariadb';
335+
$expecteds[] = '~@require-mysql';
336+
$expecteds[] = '~@require-mysql-or-mariadb';
337+
break;
338+
case 'mysql':
339+
default:
340+
$expecteds[] = '~@require-mariadb';
341+
$expecteds[] = '~@require-sqlite';
342+
break;
343+
}
344+
345+
// `PHP_OS_FAMILY` is available since PHP 7.2.
346+
$is_windows = 'Windows' === PHP_OS_FAMILY;
347+
$is_macos = 'Darwin' === PHP_OS_FAMILY;
348+
$is_linux = 'Linux' === PHP_OS_FAMILY;
349+
350+
if ( ! $is_windows ) {
351+
$expecteds[] = '~@require-windows';
352+
}
353+
if ( $is_windows ) {
354+
$expecteds[] = '~@skip-windows';
355+
}
356+
if ( ! $is_macos ) {
357+
$expecteds[] = '~@require-macos';
358+
}
359+
if ( $is_macos ) {
360+
$expecteds[] = '~@skip-macos';
361+
}
362+
if ( ! $is_linux ) {
363+
$expecteds[] = '~@require-linux';
364+
}
365+
if ( $is_linux ) {
366+
$expecteds[] = '~@skip-linux';
367+
}
368+
369+
$expected = '--tags=' . implode( '&&', array_merge( array( '~@github-api', '~@broken' ), $expecteds ) );
370+
$output = $this->run_behat_tags_script();
371+
$this->assertSame( $expected, $output );
372+
373+
putenv( false === $env_github_token ? 'GITHUB_TOKEN' : "GITHUB_TOKEN=$env_github_token" );
374+
}
317375
}

utils/behat-tags.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,75 @@ function extension_tags( $features_folder = 'features' ) {
151151
return $skip_tags;
152152
}
153153

154+
/**
155+
* An array of tags for excluding tests based on the operating system.
156+
*
157+
* @param string $features_folder The folder where the feature files are located.
158+
* @return array
159+
*/
160+
function os_tags( $features_folder = 'features' ) {
161+
$os_tags = array();
162+
$feature_files = glob( $features_folder . DIRECTORY_SEPARATOR . '*.feature' );
163+
if ( ! empty( $feature_files ) ) {
164+
foreach ( $feature_files as $feature_file ) {
165+
$contents = (string) file_get_contents( $feature_file );
166+
if ( preg_match_all( '/@(require-(windows|macos|linux)|skip-(windows|macos|linux))/', $contents, $matches ) ) {
167+
$os_tags = array_merge( $os_tags, $matches[0] );
168+
}
169+
}
170+
$os_tags = array_unique( $os_tags );
171+
}
172+
173+
if ( empty( $os_tags ) ) {
174+
return array();
175+
}
176+
177+
$skip_tags = array();
178+
179+
$is_windows = 'Windows' === PHP_OS_FAMILY;
180+
$is_macos = 'Darwin' === PHP_OS_FAMILY;
181+
$is_linux = 'Linux' === PHP_OS_FAMILY;
182+
183+
foreach ( $os_tags as $tag ) {
184+
switch ( $tag ) {
185+
case '@require-windows':
186+
if ( ! $is_windows ) {
187+
$skip_tags[] = $tag;
188+
}
189+
break;
190+
case '@require-macos':
191+
if ( ! $is_macos ) {
192+
$skip_tags[] = $tag;
193+
}
194+
break;
195+
case '@require-linux':
196+
if ( ! $is_linux ) {
197+
$skip_tags[] = $tag;
198+
}
199+
break;
200+
case '@skip-windows':
201+
if ( $is_windows ) {
202+
$skip_tags[] = $tag;
203+
}
204+
break;
205+
case '@skip-macos':
206+
if ( $is_macos ) {
207+
$skip_tags[] = $tag;
208+
}
209+
break;
210+
case '@skip-linux':
211+
if ( $is_linux ) {
212+
$skip_tags[] = $tag;
213+
}
214+
break;
215+
}
216+
}
217+
218+
return $skip_tags;
219+
}
220+
154221
$skip_tags = array_merge( $skip_tags, extension_tags( $features_folder ) );
222+
$skip_tags = array_merge( $skip_tags, os_tags( $features_folder ) );
155223

156224
if ( ! empty( $skip_tags ) ) {
157225
echo '--tags=~' . implode( '&&~', $skip_tags );

0 commit comments

Comments
 (0)