@@ -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+
399604if ( ! empty ( $ skip_tags ) ) {
400605 echo '--tags=~ ' . implode ( '&&~ ' , $ skip_tags );
401606}
0 commit comments