Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
*/
private static $run_dir;

/**
* The Directory that 'composer behat' is run from, assumed to always be the top level project folder
*/
private static $behat_run_dir;

/**
* Where WordPress core is downloaded to for caching, and which is copied to RUN_DIR during a "Given a WP installation" step. Lives until manually deleted.
*/
Expand Down Expand Up @@ -286,9 +291,10 @@

$path_separator = Utils\is_windows() ? ';' : ':';
$env = [
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
'BEHAT_RUN' => 1,
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
'BEHAT_RUN' => 1,
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
'TEST_RUN_DIR' => self::$behat_run_dir,

Check warning on line 297 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L294-L297

Added lines #L294 - L297 were not covered by tests
];

$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
Expand Down Expand Up @@ -371,6 +377,7 @@
'FRAMEWORK_ROOT' => realpath( $framework_root ),
'SRC_DIR' => realpath( dirname( dirname( __DIR__ ) ) ),
'PROJECT_DIR' => realpath( dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) ),
'TEST_RUN_DIR' => self::$behat_run_dir,

Check warning on line 380 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L380

Added line #L380 was not covered by tests
];

return $variables;
Expand Down Expand Up @@ -475,6 +482,7 @@
if ( false !== self::$log_run_times ) {
self::log_run_times_before_suite( $scope );
}
self::$behat_run_dir = getcwd();

Check warning on line 485 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L485

Added line #L485 was not covered by tests

$result = Process::create( 'wp cli info', null, self::get_process_env_variables() )->run_check();
echo "{$result->stdout}\n";
Expand Down Expand Up @@ -521,7 +529,6 @@
if ( self::$log_run_times ) {
self::log_run_times_before_scenario( $scope );
}

$this->variables = array_merge(
$this->variables,
self::get_behat_internal_variables()
Expand Down Expand Up @@ -693,8 +700,31 @@
*/
public function get_command_with_coverage( $cmd ) {
$with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' );

if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) {
return preg_replace( '/(^wp )|( wp )|(\/wp )/', '$1$2$3--require={SRC_DIR}/utils/generate-coverage.php ', $cmd );

$modify_command = function ( $part ) {
if ( preg_match( '/(^wp )|( wp )|(\/wp )/', $part ) ) {
$part = preg_replace( '/(^wp )|( wp )|(\/wp )/', '$1$2$3', $part );

Check warning on line 708 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L706-L708

Added lines #L706 - L708 were not covered by tests
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrsdizzie Just to clarify, does this now work with aliases?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes -- what fixes the previous alias problem is putting the --require at the end of the wp command instead of the beginning (since an alias must be the first thing after wp). All this extra code is needed because you can't just stick --require at the end of the full command because it might look like wp eval xyz | something else so you need to split on pipes (if present) and make sure its only put on the end of a wp command

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see it now. Tested with features/aliases.feature and it works as expected there.

Path resolution in wp-cli-tests itself works too, even within wp-cli-dev.

So I think this is good to go. You would need to approve it though :)


$require_path = '{TEST_RUN_DIR}/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php';
if ( ! file_exists( $this->variables['TEST_RUN_DIR'] . '/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php' ) ) {

Check warning on line 711 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L710-L711

Added lines #L710 - L711 were not covered by tests
// This file is not vendored inside the wp-cli-tests project
$require_path = '{TEST_RUN_DIR}/utils/generate-coverage.php';

Check warning on line 713 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L713

Added line #L713 was not covered by tests
}
$part .= " --require={$require_path}";

Check warning on line 715 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L715

Added line #L715 was not covered by tests

}
return $part;
};

Check warning on line 719 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L718-L719

Added lines #L718 - L719 were not covered by tests

if ( strpos( $cmd, '|' ) !== false ) {
$parts = explode( '|', $cmd );
$parts = array_map( $modify_command, $parts );
$cmd = implode( '|', $parts );

Check warning on line 724 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L721-L724

Added lines #L721 - L724 were not covered by tests
} else {
$cmd = $modify_command( $cmd );

Check warning on line 726 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L726

Added line #L726 was not covered by tests
}
}

return $cmd;
Expand Down
51 changes: 44 additions & 7 deletions utils/generate-coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,54 @@
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report\Clover;

$root_folder = realpath( dirname( __DIR__ ) );

$project_dir = (string) getenv( 'TEST_RUN_DIR' );

if ( ! class_exists( 'SebastianBergmann\CodeCoverage\Filter' ) ) {
require "{$root_folder}/vendor/autoload.php";
if ( ! file_exists( $project_dir . '/vendor/autoload.php' ) ) {
die( 'Could not load dependencies for generating code coverage' );
}
require "{$project_dir}/vendor/autoload.php";
}

$filtered_items = new CallbackFilterIterator(
new DirectoryIterator( $project_dir ),
function ( $file ) {
// Allow directories named "php" or "src"
if ( $file->isDir() && in_array( $file->getFilename(), [ 'php', 'src' ], true ) ) {
return true;
}

// Allow top-level files ending in "-command.php"
if ( $file->isFile() && false !== strpos( $file->getFilename(), '-command.php' ) ) {
return true;
}

return false;
}
);

$files = [];

foreach ( $filtered_items as $item ) {
if ( $item->isDir() ) {
foreach (
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $item->getPathname(), RecursiveDirectoryIterator::SKIP_DOTS )
) as $file
) {
if ( $file->isFile() && $file->getExtension() === 'php' ) {
$files[] = $file->getPathname();
}
}
} else {
$files[] = $item->getPathname();
}
}

$filter = new Filter();
$filter->includeDirectory( "{$root_folder}/includes" );
$filter->includeFiles( array( "{$root_folder}/plugin.php" ) );

$filter->includeFiles( $files );

$coverage = new CodeCoverage(
( new Selector() )->forLineCoverage( $filter ),
Expand All @@ -37,11 +76,9 @@
$coverage->start( $name );

register_shutdown_function(
static function () use ( $coverage, $feature, $scenario, $name ) {
static function () use ( $coverage, $feature, $scenario, $name, $project_dir ) {
$coverage->stop();

$project_dir = (string) getenv( 'BEHAT_PROJECT_DIR' );

$feature_suffix = preg_replace( '/[^a-z0-9]+/', '-', strtolower( $feature ) );
$scenario_suffix = preg_replace( '/[^a-z0-9]+/', '-', strtolower( $scenario ) );
$db_type = strtolower( getenv( 'WP_CLI_TEST_DBTYPE' ) );
Expand Down