Skip to content

Commit f949754

Browse files
authored
Merge pull request #409 from wp-cli/feature/report-filtered-scenarios
Report the scenarios that are left out, and why
2 parents 8c99921 + 0a5404b commit f949754

3 files changed

Lines changed: 299 additions & 0 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,17 @@ they test.
384384
container and is only reachable over TCP. Set `WP_CLI_TEST_DBSOCKET` to point at the
385385
socket if it lives somewhere unusual.
386386
387+
Before the scenarios run, the scenarios that are left out are listed together with the reason,
388+
separating the ones the environment cannot run, such as those needing a PHP extension that is
389+
not loaded, from the ones that are not part of the configuration being tested, such as those
390+
for another database or WordPress version. On GitHub Actions, the list is also added to the
391+
job summary.
392+
393+
Set `WP_CLI_TEST_STRICT_REQUIREMENTS` where the environment is expected to provide everything
394+
the scenarios need, such as on CI. The run then stops before any scenario starts when some of
395+
them cannot run in the environment, or when the database cannot be reached, instead of leaving
396+
those scenarios out or falling back to SQLite.
397+
387398
#### WP-CLI Binary
388399
389400
You can run the tests against a specific WP-CLI binary, instead of using the one that has been built in your project's `vendor/bin` folder.

‎tests/tests/TestBehatTags.php‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,4 +600,87 @@ public function test_unused_tags_are_not_filtered_out(): void {
600600
$this->assertStringNotContainsString( '@require-wp-stable', (string) $output );
601601
$this->assertStringNotContainsString( '@require-mysql-socket', (string) $output );
602602
}
603+
604+
/**
605+
* Run the script and capture its output streams and exit code.
606+
*
607+
* @param array<string, string> $env Additional environment variables.
608+
* @return array{stdout: string, stderr: string, exit_code: int}
609+
*/
610+
private function run_behat_tags_script_with_streams( array $env = array() ): array {
611+
$behat_tags = dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR . 'utils' . DIRECTORY_SEPARATOR . 'behat-tags.php';
612+
$command = escapeshellarg( PHP_BINARY ) . ' -n ' . escapeshellarg( $behat_tags );
613+
614+
$env = array_merge(
615+
getenv(),
616+
array( 'BEHAT_FEATURES_FOLDER' => $this->temp_dir . DIRECTORY_SEPARATOR . 'features' ),
617+
$env
618+
);
619+
620+
$process = proc_open(
621+
$command,
622+
array(
623+
1 => array( 'pipe', 'w' ),
624+
2 => array( 'pipe', 'w' ),
625+
),
626+
$pipes,
627+
$this->temp_dir,
628+
$env
629+
);
630+
$this->assertIsResource( $process );
631+
632+
$stdout = (string) stream_get_contents( $pipes[1] );
633+
$stderr = (string) stream_get_contents( $pipes[2] );
634+
fclose( $pipes[1] );
635+
fclose( $pipes[2] );
636+
637+
return array(
638+
'stdout' => $stdout,
639+
'stderr' => $stderr,
640+
'exit_code' => proc_close( $process ),
641+
);
642+
}
643+
644+
public function test_filtered_scenarios_are_reported(): void {
645+
file_put_contents(
646+
$this->temp_dir . DIRECTORY_SEPARATOR . 'features' . DIRECTORY_SEPARATOR . 'report.feature',
647+
"Feature: Report\n\n @require-extension-wpclinonexistent\n Scenario: Needs an extension\n Given a step\n\n @broken\n Scenario: Broken\n Given a step\n\n Scenario: Runs\n Given a step\n \"\"\"\n Scenario: Not a scenario of this file\n \"\"\"\n"
648+
);
649+
650+
$result = $this->run_behat_tags_script_with_streams();
651+
652+
$this->assertSame( 0, $result['exit_code'] );
653+
$this->assertStringContainsString( '~@require-extension-wpclinonexistent', $result['stdout'] );
654+
$this->assertStringContainsString( 'Not running 2 of 3 scenarios:', $result['stderr'] );
655+
$this->assertStringContainsString( 'Not available in this environment:', $result['stderr'] );
656+
$this->assertStringContainsString( '@require-extension-wpclinonexistent, as the PHP extension "wpclinonexistent" is not loaded: ', $result['stderr'] );
657+
$this->assertStringContainsString( 'report.feature:4', $result['stderr'] );
658+
$this->assertStringContainsString( 'Not part of this configuration:', $result['stderr'] );
659+
$this->assertStringContainsString( 'report.feature:8', $result['stderr'] );
660+
}
661+
662+
public function test_strict_requirements_stop_before_the_run(): void {
663+
file_put_contents(
664+
$this->temp_dir . DIRECTORY_SEPARATOR . 'features' . DIRECTORY_SEPARATOR . 'strict.feature',
665+
"Feature: Strict\n\n @require-extension-wpclinonexistent\n Scenario: Needs an extension\n Given a step\n"
666+
);
667+
668+
$result = $this->run_behat_tags_script_with_streams( array( 'WP_CLI_TEST_STRICT_REQUIREMENTS' => '1' ) );
669+
670+
$this->assertSame( 1, $result['exit_code'] );
671+
$this->assertSame( '', $result['stdout'] );
672+
$this->assertStringContainsString( 'Stopping, as WP_CLI_TEST_STRICT_REQUIREMENTS is set and 1 scenarios cannot run in this environment.', $result['stderr'] );
673+
}
674+
675+
public function test_strict_requirements_allow_other_configurations(): void {
676+
file_put_contents(
677+
$this->temp_dir . DIRECTORY_SEPARATOR . 'features' . DIRECTORY_SEPARATOR . 'strict.feature',
678+
"Feature: Strict\n\n @broken\n Scenario: Broken\n Given a step\n"
679+
);
680+
681+
$result = $this->run_behat_tags_script_with_streams( array( 'WP_CLI_TEST_STRICT_REQUIREMENTS' => '1' ) );
682+
683+
$this->assertSame( 0, $result['exit_code'] );
684+
$this->assertStringContainsString( '~@broken', $result['stdout'] );
685+
}
603686
}

‎utils/behat-tags.php‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,214 @@ function os_tags( $features_folder = 'features' ) {
393393
return $skip_tags;
394394
}
395395

396+
/**
397+
* Get the scenarios of the feature files with the tags that apply to them.
398+
*
399+
* @param string $features_folder The folder where the feature files are located.
400+
* @return array<array{path: string, tags: array<string>}>
401+
*/
402+
function scenarios_with_tags( $features_folder = 'features' ) {
403+
$scenarios = array();
404+
$feature_files = glob( $features_folder . DIRECTORY_SEPARATOR . '*.feature' );
405+
406+
foreach ( $feature_files ? $feature_files : array() as $feature_file ) {
407+
$lines = file( $feature_file, FILE_IGNORE_NEW_LINES );
408+
$feature_tags = array();
409+
$pending_tags = array();
410+
$doc_string = null;
411+
412+
foreach ( $lines ? $lines : array() as $index => $line ) {
413+
$line = trim( $line );
414+
415+
// Doc strings can hold whole feature files, as written by scenarios for scaffolding.
416+
if ( null !== $doc_string ) {
417+
if ( 0 === strpos( $line, $doc_string ) ) {
418+
$doc_string = null;
419+
}
420+
continue;
421+
}
422+
if ( 0 === strpos( $line, '"""' ) || 0 === strpos( $line, '```' ) ) {
423+
$doc_string = substr( $line, 0, 3 );
424+
continue;
425+
}
426+
427+
if ( 0 === strpos( $line, '@' ) ) {
428+
preg_match_all( '/@[^\s@]+/', $line, $matches );
429+
$pending_tags = array_merge( $pending_tags, $matches[0] );
430+
} elseif ( preg_match( '/^Feature:/', $line ) ) {
431+
$feature_tags = $pending_tags;
432+
$pending_tags = array();
433+
} elseif ( preg_match( '/^(Scenario|Scenario Outline|Scenario Template|Example):/', $line ) ) {
434+
$scenarios[] = array(
435+
'path' => $feature_file . ':' . ( $index + 1 ),
436+
'tags' => array_merge( $feature_tags, $pending_tags ),
437+
);
438+
$pending_tags = array();
439+
} elseif ( '' !== $line && 0 !== strpos( $line, '#' ) ) {
440+
$pending_tags = array();
441+
}
442+
}
443+
}
444+
445+
return $scenarios;
446+
}
447+
448+
/**
449+
* Describe why scenarios carrying a skip tag are not run.
450+
*
451+
* @param string $tag The tag, without the leading tilde.
452+
* @param array $context What the environment provides: wp, php, db and os.
453+
* @return array{0: bool, 1: string} Whether the environment lacks something, and the reason.
454+
*/
455+
function describe_skip_tag( $tag, $context ) {
456+
if ( preg_match( '/^@require-extension-(.+)$/', $tag, $matches ) ) {
457+
return array( true, "the PHP extension \"{$matches[1]}\" is not loaded" );
458+
}
459+
460+
if ( '@require-mysql-socket' === $tag ) {
461+
return array( true, 'the database server cannot be reached through a socket' );
462+
}
463+
464+
if ( preg_match( '/^@(require|less-than)-wp-/', $tag ) || '@require-wp-stable' === $tag || '@broken-trunk' === $tag ) {
465+
return array( false, "WordPress {$context['wp']} is used" );
466+
}
467+
468+
if ( preg_match( '/^@(require|less-than)-php-/', $tag ) ) {
469+
return array( false, "PHP {$context['php']} is used" );
470+
}
471+
472+
if ( preg_match( '/^@(require|less-than|skip)-(mysql|mariadb|sqlite)/', $tag ) ) {
473+
return array( false, "the database is {$context['db']}" );
474+
}
475+
476+
if ( preg_match( '/^@(require|skip)-(windows|macos|linux)$/', $tag ) ) {
477+
return array( false, "the operating system is {$context['os']}" );
478+
}
479+
480+
if ( '@github-api' === $tag ) {
481+
return array( false, 'GITHUB_TOKEN is not set' );
482+
}
483+
484+
if ( '@require-object-cache' === $tag || '@skip-object-cache' === $tag ) {
485+
return array( false, 'WP_CLI_TEST_OBJECT_CACHE is ' . ( getenv( 'WP_CLI_TEST_OBJECT_CACHE' ) ? 'set' : 'not set' ) );
486+
}
487+
488+
if ( '@broken' === $tag ) {
489+
return array( false, 'they are known to be broken' );
490+
}
491+
492+
return array( false, 'it is excluded' );
493+
}
494+
495+
/**
496+
* Report the scenarios that the tag filter leaves out, and why.
497+
*
498+
* Scenarios that the environment cannot run are listed separately from the ones that are
499+
* not part of the configuration being tested, such as another database or WordPress version.
500+
*
501+
* @param array<string> $skip_tags
502+
* @param string $features_folder
503+
* @param array $context
504+
* @return int Number of scenarios left out because the environment lacks something.
505+
*/
506+
function report_filtered_scenarios( $skip_tags, $features_folder, $context ) {
507+
$scenarios = scenarios_with_tags( $features_folder );
508+
$groups = array();
509+
510+
foreach ( $scenarios as $scenario ) {
511+
$matching = array_values( array_intersect( $skip_tags, $scenario['tags'] ) );
512+
if ( $matching ) {
513+
$groups[ $matching[0] ][] = $scenario['path'];
514+
}
515+
}
516+
517+
if ( ! $groups ) {
518+
return 0;
519+
}
520+
521+
$sections = array(
522+
true => array(),
523+
false => array(),
524+
);
525+
$missing = 0;
526+
$total = 0;
527+
528+
foreach ( $groups as $tag => $paths ) {
529+
list( $lacks, $reason ) = describe_skip_tag( $tag, $context );
530+
$sections[ $lacks ][] = array( $tag, $reason, $paths );
531+
$total += count( $paths );
532+
if ( $lacks ) {
533+
$missing += count( $paths );
534+
}
535+
}
536+
537+
$titles = array(
538+
true => 'Not available in this environment',
539+
false => 'Not part of this configuration',
540+
);
541+
542+
$text = sprintf( "Not running %d of %d scenarios:\n", $total, count( $scenarios ) );
543+
$markdown = sprintf( "### Behat: %d of %d scenarios not run\n", $total, count( $scenarios ) );
544+
545+
foreach ( array( true, false ) as $lacks ) {
546+
if ( ! $sections[ $lacks ] ) {
547+
continue;
548+
}
549+
$text .= " {$titles[ $lacks ]}:\n";
550+
$markdown .= "\n**{$titles[ $lacks ]}**\n\n";
551+
foreach ( $sections[ $lacks ] as $section ) {
552+
list( $tag, $reason, $paths ) = $section;
553+
554+
$text .= sprintf( " %s, as %s: %s\n", $tag, $reason, implode( ', ', $paths ) );
555+
$markdown .= sprintf(
556+
"- `%s`, as %s (%d): %s\n",
557+
$tag,
558+
$reason,
559+
count( $paths ),
560+
implode(
561+
', ',
562+
array_map(
563+
function ( $path ) {
564+
return "`{$path}`";
565+
},
566+
$paths
567+
)
568+
)
569+
);
570+
}
571+
}
572+
573+
fwrite( STDERR, $text );
574+
575+
// Add the report to the summary of a GitHub Actions job, once per run.
576+
$summary = getenv( 'GITHUB_STEP_SUMMARY' );
577+
if ( $summary && ! getenv( 'WP_CLI_TEST_DEBUG_BEHAT_ENV' ) ) {
578+
file_put_contents( $summary, $markdown . "\n", FILE_APPEND );
579+
}
580+
581+
return $missing;
582+
}
583+
396584
$skip_tags = array_merge( $skip_tags, extension_tags( $features_folder ) );
397585
$skip_tags = array_merge( $skip_tags, os_tags( $features_folder ) );
398586

587+
$missing = report_filtered_scenarios(
588+
$skip_tags,
589+
$features_folder,
590+
array(
591+
'wp' => $wp_version ? $wp_version : 'latest',
592+
'php' => PHP_VERSION,
593+
'db' => trim( ( 'sqlite' === $db_type ? 'SQLite' : ( 'mariadb' === $db_type ? 'MariaDB' : 'MySQL' ) ) . ' ' . ( 'sqlite' === $db_type ? '' : $db_version ) ),
594+
'os' => PHP_OS_FAMILY,
595+
)
596+
);
597+
598+
// With strict requirements, everything the scenarios need has to be available.
599+
if ( $missing && getenv( 'WP_CLI_TEST_STRICT_REQUIREMENTS' ) ) {
600+
fwrite( STDERR, "Stopping, as WP_CLI_TEST_STRICT_REQUIREMENTS is set and {$missing} scenarios cannot run in this environment.\n" );
601+
exit( 1 );
602+
}
603+
399604
if ( ! empty( $skip_tags ) ) {
400605
echo '--tags=~' . implode( '&&~', $skip_tags );
401606
}

0 commit comments

Comments
 (0)