|
7 | 7 | use Behat\Behat\Hook\Scope\BeforeScenarioScope;
|
8 | 8 | use Behat\Testwork\Hook\Scope\AfterSuiteScope;
|
9 | 9 | use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
|
| 10 | +use Behat\Behat\Hook\Scope\AfterFeatureScope; |
| 11 | +use Behat\Behat\Hook\Scope\BeforeFeatureScope; |
10 | 12 | use RuntimeException;
|
11 | 13 | use WP_CLI\Process;
|
12 | 14 | use WP_CLI\Utils;
|
@@ -110,6 +112,48 @@ class FeatureContext implements SnippetAcceptingContext {
|
110 | 112 |
|
111 | 113 | private $mocked_requests = [];
|
112 | 114 |
|
| 115 | + /** |
| 116 | + * The current feature. |
| 117 | + * |
| 118 | + * @var \Behat\Gherkin\Node\FeatureNode|null |
| 119 | + */ |
| 120 | + private static $feature; |
| 121 | + |
| 122 | + /** |
| 123 | + * The current scenario. |
| 124 | + * |
| 125 | + * @var \Behat\Gherkin\Node\ScenarioInterface|null |
| 126 | + */ |
| 127 | + private $scenario; |
| 128 | + |
| 129 | + /** |
| 130 | + * @BeforeFeature |
| 131 | + */ |
| 132 | + public static function store_feature( BeforeFeatureScope $scope ) { |
| 133 | + self::$feature = $scope->getFeature(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @BeforeScenario |
| 138 | + */ |
| 139 | + public function store_scenario( BeforeScenarioScope $scope ) { |
| 140 | + $this->scenario = $scope->getScenario(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @AfterScenario |
| 145 | + */ |
| 146 | + public function forget_scenario( AfterScenarioScope $scope ) { |
| 147 | + $this->scenario = null; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @AfterFeature |
| 152 | + */ |
| 153 | + public static function forget_feature( AfterFeatureScope $scope ) { |
| 154 | + self::$feature = null; |
| 155 | + } |
| 156 | + |
113 | 157 | /**
|
114 | 158 | * Get the path to the Composer vendor folder.
|
115 | 159 | *
|
@@ -333,9 +377,9 @@ private static function get_behat_internal_variables() {
|
333 | 377 | }
|
334 | 378 |
|
335 | 379 | /**
|
336 |
| - * Download and extract a single copy of the sqlite-database-integration plugin |
337 |
| - * for use in subsequent WordPress copies |
338 |
| - */ |
| 380 | + * Download and extract a single copy of the sqlite-database-integration plugin |
| 381 | + * for use in subsequent WordPress copies |
| 382 | + */ |
339 | 383 | private static function download_sqlite_plugin( $dir ) {
|
340 | 384 | $download_url = 'https://downloads.wordpress.org/plugin/sqlite-database-integration.zip';
|
341 | 385 | $download_location = $dir . '/sqlite-database-integration.zip';
|
@@ -370,9 +414,9 @@ private static function download_sqlite_plugin( $dir ) {
|
370 | 414 | }
|
371 | 415 |
|
372 | 416 | /**
|
373 |
| - * Given a WordPress installation with the sqlite-database-integration plugin, |
374 |
| - * configure it to use SQLite as the database by placing the db.php dropin file |
375 |
| - */ |
| 417 | + * Given a WordPress installation with the sqlite-database-integration plugin, |
| 418 | + * configure it to use SQLite as the database by placing the db.php dropin file |
| 419 | + */ |
376 | 420 | private static function configure_sqlite( $dir ) {
|
377 | 421 | $db_copy = $dir . '/wp-content/mu-plugins/sqlite-database-integration/db.copy';
|
378 | 422 | $db_dropin = $dir . '/wp-content/db.php';
|
@@ -639,6 +683,23 @@ public function __construct() {
|
639 | 683 | $this->set_cache_dir();
|
640 | 684 | }
|
641 | 685 |
|
| 686 | + /** |
| 687 | + * Enhances a `wp <command>` string with an additional `--require` for code coverage collection. |
| 688 | + * |
| 689 | + * Only applies if `WP_CLI_TEST_COVERAGE` is set. |
| 690 | + * |
| 691 | + * @param string $cmd Command string. |
| 692 | + * @return string Possibly enhanced command string. |
| 693 | + */ |
| 694 | + public function get_command_with_coverage( $cmd ) { |
| 695 | + $with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' ); |
| 696 | + if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) { |
| 697 | + return preg_replace( '/(^wp )|( wp )|(\/wp )/', '$1$2$3--require={SRC_DIR}/utils/generate-coverage.php ', $cmd ); |
| 698 | + } |
| 699 | + |
| 700 | + return $cmd; |
| 701 | + } |
| 702 | + |
642 | 703 | /**
|
643 | 704 | * Replace standard {VARIABLE_NAME} variables and the special {INVOKE_WP_CLI_WITH_PHP_ARGS-args} and {WP_VERSION-version-latest} variables.
|
644 | 705 | * Note that standard variable names can only contain uppercase letters, digits and underscores and cannot begin with a digit.
|
@@ -877,10 +938,25 @@ public function proc( $command, $assoc_args = [], $path = '' ) {
|
877 | 938 | }
|
878 | 939 |
|
879 | 940 | $env = self::get_process_env_variables();
|
| 941 | + |
880 | 942 | if ( isset( $this->variables['SUITE_CACHE_DIR'] ) ) {
|
881 | 943 | $env['WP_CLI_CACHE_DIR'] = $this->variables['SUITE_CACHE_DIR'];
|
882 | 944 | }
|
883 | 945 |
|
| 946 | + if ( isset( $this->variables['PROJECT_DIR'] ) ) { |
| 947 | + $env['BEHAT_PROJECT_DIR'] = $this->variables['PROJECT_DIR']; |
| 948 | + } |
| 949 | + |
| 950 | + if ( self::$feature ) { |
| 951 | + $env['BEHAT_FEATURE_TITLE'] = self::$feature->getTitle(); |
| 952 | + } |
| 953 | + |
| 954 | + if ( $this->scenario ) { |
| 955 | + $env['BEHAT_SCENARIO_TITLE'] = $this->scenario->getTitle(); |
| 956 | + } |
| 957 | + |
| 958 | + $env['WP_CLI_TEST_DBTYPE'] = self::$db_type; |
| 959 | + |
884 | 960 | if ( isset( $this->variables['RUN_DIR'] ) ) {
|
885 | 961 | $cwd = "{$this->variables['RUN_DIR']}/{$path}";
|
886 | 962 | } else {
|
@@ -1242,8 +1318,8 @@ private static function dir_diff_copy( $upd_dir, $src_dir, $cop_dir ) {
|
1242 | 1318 | }
|
1243 | 1319 | self::copy_dir( $upd_file, $cop_file );
|
1244 | 1320 | } elseif ( ! copy( $upd_file, $cop_file ) ) {
|
1245 |
| - $error = error_get_last(); |
1246 |
| - throw new RuntimeException( sprintf( "Failed to copy '%s' to '%s': %s. " . __FILE__ . ':' . __LINE__, $upd_file, $cop_file, $error['message'] ) ); |
| 1321 | + $error = error_get_last(); |
| 1322 | + throw new RuntimeException( sprintf( "Failed to copy '%s' to '%s': %s. " . __FILE__ . ':' . __LINE__, $upd_file, $cop_file, $error['message'] ) ); |
1247 | 1323 | }
|
1248 | 1324 | } elseif ( is_dir( $upd_file ) ) {
|
1249 | 1325 | self::dir_diff_copy( $upd_file, $src_file, $cop_file );
|
|
0 commit comments