Skip to content

Commit

Permalink
Merge pull request #19 from wp-cli/17-switch-back
Browse files Browse the repository at this point in the history
Switch back to `wp-cli/wp-cli:*`
  • Loading branch information
schlessera authored Jun 6, 2017
2 parents 0b6ecd3 + 98202a6 commit 6c65bd5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 27 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
]
},
"require": {
"wp-cli/wp-cli": "dev-master",
"wp-cli/wp-cli": "*",
"composer/composer": "^1.2.0"
},
"require-dev": {
Expand Down
58 changes: 49 additions & 9 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
require_once __DIR__ . '/../../php/utils.php';
require_once __DIR__ . '/../../php/WP_CLI/Process.php';
require_once __DIR__ . '/../../php/WP_CLI/ProcessRun.php';
require_once __DIR__ . '/../../vendor/autoload.php';
if ( file_exists( __DIR__ . '/../../vendor/autoload.php' ) ) {
require_once __DIR__ . '/../../vendor/autoload.php';
} else if ( file_exists( __DIR__ . '/../../../../autoload.php' ) ) {
require_once __DIR__ . '/../../../../autoload.php';
}
}

/**
Expand Down Expand Up @@ -69,6 +73,9 @@ private static function get_process_env_variables() {
if ( $config_path = getenv( 'WP_CLI_CONFIG_PATH' ) ) {
$env['WP_CLI_CONFIG_PATH'] = $config_path;
}
if ( $term = getenv( 'TERM' ) ) {
$env['TERM'] = $term;
}
return $env;
}

Expand Down Expand Up @@ -123,8 +130,8 @@ public function beforeScenario( $event ) {
public function afterScenario( $event ) {
if ( isset( $this->variables['RUN_DIR'] ) ) {
// remove altered WP install, unless there's an error
if ( $event->getResult() < 4 ) {
$this->proc( Utils\esc_cmd( 'rm -r %s', $this->variables['RUN_DIR'] ) )->run();
if ( $event->getResult() < 4 && 0 === strpos( $this->variables['RUN_DIR'], sys_get_temp_dir() ) ) {
$this->proc( Utils\esc_cmd( 'rm -rf %s', $this->variables['RUN_DIR'] ) )->run();
}
}

Expand Down Expand Up @@ -196,7 +203,11 @@ public function getHookDefinitionResources() {
}

public function replace_variables( $str ) {
return preg_replace_callback( '/\{([A-Z_]+)\}/', array( $this, '_replace_var' ), $str );
$ret = preg_replace_callback( '/\{([A-Z_]+)\}/', array( $this, '_replace_var' ), $str );
if ( false !== strpos( $str, '{WP_VERSION-' ) ) {
$ret = $this->_replace_wp_versions( $ret );
}
return $ret;
}

private function _replace_var( $matches ) {
Expand All @@ -209,6 +220,35 @@ private function _replace_var( $matches ) {
return $cmd;
}

// Substitute "{WP_VERSION-version-latest}" variables.
private function _replace_wp_versions( $str ) {
static $wp_versions = null;
if ( null === $wp_versions ) {
$wp_versions = array();

$response = Requests::get( 'https://api.wordpress.org/core/version-check/1.7/', null, array( 'timeout' => 30 ) );
if ( 200 === $response->status_code && ( $body = json_decode( $response->body ) ) && is_object( $body ) && isset( $body->offers ) && is_array( $body->offers ) ) {
// Latest version alias.
$wp_versions["{WP_VERSION-latest}"] = count( $body->offers ) ? $body->offers[0]->version : '';
foreach ( $body->offers as $offer ) {
$sub_ver = preg_replace( '/(^[0-9]+\.[0-9]+)\.[0-9]+$/', '$1', $offer->version );
$sub_ver_key = "{WP_VERSION-{$sub_ver}-latest}";

$main_ver = preg_replace( '/(^[0-9]+)\.[0-9]+$/', '$1', $sub_ver );
$main_ver_key = "{WP_VERSION-{$main_ver}-latest}";

if ( ! isset( $wp_versions[ $main_ver_key ] ) ) {
$wp_versions[ $main_ver_key ] = $offer->version;
}
if ( ! isset( $wp_versions[ $sub_ver_key ] ) ) {
$wp_versions[ $sub_ver_key ] = $offer->version;
}
}
}
}
return strtr( $str, $wp_versions );
}

public function create_run_dir() {
if ( !isset( $this->variables['RUN_DIR'] ) ) {
$this->variables['RUN_DIR'] = sys_get_temp_dir() . '/' . uniqid( "wp-cli-test-run-", TRUE );
Expand All @@ -219,12 +259,12 @@ public function create_run_dir() {
public function build_phar( $version = 'same' ) {
$this->variables['PHAR_PATH'] = $this->variables['RUN_DIR'] . '/' . uniqid( "wp-cli-build-", TRUE ) . '.phar';

// Test running against WP-CLI proper
$make_phar_path = __DIR__ . '/../../utils/make-phar.php';
// Test running against a package installed as a WP-CLI dependency
// WP-CLI installed as a project dependency
$make_phar_path = __DIR__ . '/../../../../../utils/make-phar.php';
if ( ! file_exists( $make_phar_path ) ) {
// Test running against a package installed as a WP-CLI dependency
// WP-CLI installed as a project dependency
$make_phar_path = __DIR__ . '/../../../../../utils/make-phar.php';
// Test running against WP-CLI proper
$make_phar_path = __DIR__ . '/../../utils/make-phar.php';
if ( ! file_exists( $make_phar_path ) ) {
// WP-CLI as a dependency of this project
$make_phar_path = __DIR__ . '/../../vendor/wp-cli/wp-cli/utils/make-phar.php';
Expand Down
82 changes: 66 additions & 16 deletions features/bootstrap/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) {
$old_pass = getenv( 'MYSQL_PWD' );
putenv( 'MYSQL_PWD=' . $pass );

$final_cmd = $cmd . assoc_args_to_str( $assoc_args );
$final_cmd = force_env_on_nix_systems( $cmd ) . assoc_args_to_str( $assoc_args );

$proc = proc_open( $final_cmd, $descriptors, $pipes );
if ( !$proc )
Expand All @@ -447,19 +447,6 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) {
* IMPORTANT: Automatic HTML escaping is disabled!
*/
function mustache_render( $template_name, $data = array() ) {
// Transform absolute path to relative path inside of Phar
if ( inside_phar() && 0 === stripos( $template_name, PHAR_STREAM_PREFIX ) ) {
$search = '';
$replace = '';
if ( file_exists( WP_CLI_ROOT . '/vendor/autoload.php' ) ) {
$search = dirname( __DIR__ );
$replace = WP_CLI_ROOT;
} elseif ( file_exists( dirname( dirname( WP_CLI_ROOT ) ) . '/autoload.php' ) ) {
$search = dirname( dirname( dirname( __DIR__ ) ) );
$replace = dirname( dirname( dirname( WP_CLI_ROOT ) ) );
}
$template_name = str_replace( $search, $replace, $template_name );
}
if ( ! file_exists( $template_name ) )
$template_name = WP_CLI_ROOT . "/templates/$template_name";

Expand Down Expand Up @@ -519,9 +506,11 @@ function parse_url( $url ) {

/**
* Check if we're running in a Windows environment (cmd.exe).
*
* @return bool
*/
function is_windows() {
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
return false !== ( $test_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' ) ) ? (bool) $test_is_windows : strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

/**
Expand Down Expand Up @@ -570,7 +559,7 @@ function http_request( $method, $url, $data = null, $headers = array(), $options
if ( inside_phar() ) {
// cURL can't read Phar archives
$options['verify'] = extract_from_phar(
WP_CLI_ROOT . '/vendor' . $cert_path );
WP_CLI_VENDOR_DIR . $cert_path );
} else {
foreach( get_vendor_paths() as $vendor_path ) {
if ( file_exists( $vendor_path . $cert_path ) ) {
Expand Down Expand Up @@ -900,6 +889,44 @@ function expand_globs( $paths, $flags = GLOB_BRACE ) {
return array_unique( $expanded );
}

/**
* Get the closest suggestion for a mis-typed target term amongst a list of
* options.
*
* Uses the Levenshtein algorithm to calculate the relative "distance" between
* terms.
*
* If the "distance" to the closest term is higher than the threshold, an empty
* string is returned.
*
* @param string $target Target term to get a suggestion for.
* @param array $options Array with possible options.
* @param int $threshold Threshold above which to return an empty string.
*
* @return string
*/
function get_suggestion( $target, array $options, $threshold = 2 ) {
if ( empty( $options ) ) {
return '';
}
foreach ( $options as $option ) {
$distance = levenshtein( $option, $target );
$levenshtein[ $option ] = $distance;
}

// Sort known command strings by distance to user entry.
asort( $levenshtein );

// Fetch the closest command string.
reset( $levenshtein );
$suggestion = key( $levenshtein );

// Only return a suggestion if below a given threshold.
return $levenshtein[ $suggestion ] <= $threshold && $suggestion !== $target
? (string) $suggestion
: '';
}

/**
* Get a Phar-safe version of a path.
*
Expand Down Expand Up @@ -955,3 +982,26 @@ function is_bundled_command( $command ) {
? array_key_exists( $command, $classes )
: false;
}

/**
* Maybe prefix command string with "/usr/bin/env".
* Removes (if there) if Windows, adds (if not there) if not.
*
* @param string $command
*
* @return string
*/
function force_env_on_nix_systems( $command ) {
$env_prefix = '/usr/bin/env ';
$env_prefix_len = strlen( $env_prefix );
if ( is_windows() ) {
if ( 0 === strncmp( $command, $env_prefix, $env_prefix_len ) ) {
$command = substr( $command, $env_prefix_len );
}
} else {
if ( 0 !== strncmp( $command, $env_prefix, $env_prefix_len ) ) {
$command = $env_prefix . $command;
}
}
return $command;
}
3 changes: 2 additions & 1 deletion features/steps/then.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ function ( $world, $stream ) {
}
);

$steps->Then( '/^(STDOUT|STDERR) should be a version string (<|<=|>|>=|==|=|!=|<>) ([+\w\.-]+)$/',
$steps->Then( '/^(STDOUT|STDERR) should be a version string (<|<=|>|>=|==|=|!=|<>) ([+\w.{}-]+)$/',
function ( $world, $stream, $operator, $goal_ver ) {
$goal_ver = $world->replace_variables( $goal_ver );
$stream = strtolower( $stream );
if ( false === version_compare( trim( $world->result->$stream, "\n" ), $goal_ver, $operator ) ) {
throw new Exception( $world->result );
Expand Down

0 comments on commit 6c65bd5

Please sign in to comment.