Skip to content

Commit d036236

Browse files
Merge branch 'master' into cs-phpcs-progress-meter
2 parents b9a1bf6 + f380eaa commit d036236

File tree

5 files changed

+120
-46
lines changed

5 files changed

+120
-46
lines changed

features/bootstrap/support.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
// Utility functions used by Behat steps
44

5+
function assertRegExp( $regex, $actual ) {
6+
if ( ! preg_match( $regex, $actual ) ) {
7+
throw new Exception( "Actual value: " . var_export( $actual, true ) );
8+
}
9+
}
10+
511
function assertEquals( $expected, $actual ) {
612
if ( $expected != $actual ) {
713
throw new Exception( "Actual value: " . var_export( $actual, true ) );

features/bootstrap/utils.php

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function extract_from_phar( $path ) {
3030

3131
register_shutdown_function(
3232
function() use ( $tmp_path ) {
33-
@unlink( $tmp_path );
33+
if ( file_exists( $tmp_path ) ) {
34+
unlink( $tmp_path );
35+
}
3436
}
3537
);
3638

@@ -58,7 +60,7 @@ function load_dependencies() {
5860
}
5961

6062
if ( ! $has_autoload ) {
61-
fputs( STDERR, "Internal error: Can't find Composer autoloader.\nTry running: composer install\n" );
63+
fwrite( STDERR, "Internal error: Can't find Composer autoloader.\nTry running: composer install\n" );
6264
exit( 3 );
6365
}
6466
}
@@ -138,7 +140,7 @@ function find_file_upward( $files, $dir = null, $stop_check = null ) {
138140
if ( is_null( $dir ) ) {
139141
$dir = getcwd();
140142
}
141-
while ( @is_readable( $dir ) ) {
143+
while ( is_readable( $dir ) ) {
142144
// Stop walking up when the supplied callable returns true being passed the $dir
143145
if ( is_callable( $stop_check ) && call_user_func( $stop_check, $dir ) ) {
144146
return null;
@@ -166,7 +168,7 @@ function is_path_absolute( $path ) {
166168
return true;
167169
}
168170

169-
return $path[0] === '/';
171+
return '/' === $path[0];
170172
}
171173

172174
/**
@@ -227,12 +229,12 @@ function locate_wp_config() {
227229
static $path;
228230

229231
if ( null === $path ) {
232+
$path = false;
233+
230234
if ( file_exists( ABSPATH . 'wp-config.php' ) ) {
231235
$path = ABSPATH . 'wp-config.php';
232236
} elseif ( file_exists( ABSPATH . '../wp-config.php' ) && ! file_exists( ABSPATH . '/../wp-settings.php' ) ) {
233237
$path = ABSPATH . '../wp-config.php';
234-
} else {
235-
$path = false;
236238
}
237239

238240
if ( $path ) {
@@ -244,7 +246,9 @@ function locate_wp_config() {
244246
}
245247

246248
function wp_version_compare( $since, $operator ) {
247-
return version_compare( str_replace( array( '-src' ), '', $GLOBALS['wp_version'] ), $since, $operator );
249+
$wp_version = str_replace( '-src', '', $GLOBALS['wp_version'] );
250+
$since = str_replace( '-src', '', $since );
251+
return version_compare( $wp_version, $since, $operator );
248252
}
249253

250254
/**
@@ -358,9 +362,9 @@ function launch_editor_for_input( $input, $filename = 'WP-CLI' ) {
358362
do {
359363
$tmpfile = basename( $filename );
360364
$tmpfile = preg_replace( '|\.[^.]*$|', '', $tmpfile );
361-
$tmpfile .= '-' . substr( md5( rand() ), 0, 6 );
365+
$tmpfile .= '-' . substr( md5( mt_rand() ), 0, 6 );
362366
$tmpfile = $tmpdir . $tmpfile . '.tmp';
363-
$fp = @fopen( $tmpfile, 'x' );
367+
$fp = fopen( $tmpfile, 'xb' );
364368
if ( ! $fp && is_writable( $tmpdir ) && file_exists( $tmpfile ) ) {
365369
$tmpfile = '';
366370
continue;
@@ -379,10 +383,10 @@ function launch_editor_for_input( $input, $filename = 'WP-CLI' ) {
379383

380384
$editor = getenv( 'EDITOR' );
381385
if ( ! $editor ) {
386+
$editor = 'vi';
387+
382388
if ( isset( $_SERVER['OS'] ) && false !== strpos( $_SERVER['OS'], 'indows' ) ) {
383389
$editor = 'notepad';
384-
} else {
385-
$editor = 'vi';
386390
}
387391
}
388392

@@ -416,9 +420,9 @@ function mysql_host_to_cli_args( $raw_host ) {
416420
list( $assoc_args['host'], $extra ) = $host_parts;
417421
$extra = trim( $extra );
418422
if ( is_numeric( $extra ) ) {
419-
$assoc_args['port'] = intval( $extra );
423+
$assoc_args['port'] = (int) $extra;
420424
$assoc_args['protocol'] = 'tcp';
421-
} elseif ( $extra !== '' ) {
425+
} elseif ( '' !== $extra ) {
422426
$assoc_args['socket'] = $extra;
423427
}
424428
} else {
@@ -436,7 +440,9 @@ function run_mysql_command( $cmd, $assoc_args, $descriptors = null ) {
436440
}
437441

438442
if ( isset( $assoc_args['host'] ) ) {
443+
//@codingStandardsIgnoreStart
439444
$assoc_args = array_merge( $assoc_args, mysql_host_to_cli_args( $assoc_args['host'] ) );
445+
//@codingStandardsIgnoreEnd
440446
}
441447

442448
$pass = $assoc_args['pass'];
@@ -581,6 +587,7 @@ function replace_path_consts( $source, $path ) {
581587
function http_request( $method, $url, $data = null, $headers = array(), $options = array() ) {
582588

583589
$cert_path = '/rmccue/requests/library/Requests/Transport/cacert.pem';
590+
$halt_on_error = ! isset( $options['halt_on_error'] ) || (bool) $options['halt_on_error'];
584591
if ( inside_phar() ) {
585592
// cURL can't read Phar archives
586593
$options['verify'] = extract_from_phar(
@@ -594,25 +601,36 @@ function http_request( $method, $url, $data = null, $headers = array(), $options
594601
}
595602
}
596603
if ( empty( $options['verify'] ) ) {
597-
WP_CLI::error( 'Cannot find SSL certificate.' );
604+
$error_msg = 'Cannot find SSL certificate.';
605+
if ( $halt_on_error ) {
606+
WP_CLI::error( $error_msg );
607+
}
608+
throw new \RuntimeException( $error_msg );
598609
}
599610
}
600611

601612
try {
602-
$request = \Requests::request( $url, $headers, $data, $method, $options );
603-
return $request;
613+
return \Requests::request( $url, $headers, $data, $method, $options );
604614
} catch ( \Requests_Exception $ex ) {
605615
// CURLE_SSL_CACERT_BADFILE only defined for PHP >= 7.
606616
if ( 'curlerror' !== $ex->getType() || ! in_array( curl_errno( $ex->getData() ), array( CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, 77 /*CURLE_SSL_CACERT_BADFILE*/ ), true ) ) {
607-
\WP_CLI::error( sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() ) );
617+
$error_msg = sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() );
618+
if ( $halt_on_error ) {
619+
WP_CLI::error( $error_msg );
620+
}
621+
throw new \RuntimeException( $error_msg, null, $ex );
608622
}
609623
// Handle SSL certificate issues gracefully
610624
\WP_CLI::warning( sprintf( "Re-trying without verify after failing to get verified url '%s' %s.", $url, $ex->getMessage() ) );
611625
$options['verify'] = false;
612626
try {
613627
return \Requests::request( $url, $headers, $data, $method, $options );
614628
} catch ( \Requests_Exception $ex ) {
615-
\WP_CLI::error( sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() ) );
629+
$error_msg = sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() );
630+
if ( $halt_on_error ) {
631+
WP_CLI::error( $error_msg );
632+
}
633+
throw new \RuntimeException( $error_msg, null, $ex );
616634
}
617635
}
618636
}
@@ -698,11 +716,13 @@ function get_named_sem_ver( $new_version, $original_version ) {
698716

699717
if ( ! is_null( $minor ) && Semver::satisfies( $new_version, "{$major}.{$minor}.x" ) ) {
700718
return 'patch';
701-
} elseif ( Semver::satisfies( $new_version, "{$major}.x.x" ) ) {
719+
}
720+
721+
if ( Semver::satisfies( $new_version, "{$major}.x.x" ) ) {
702722
return 'minor';
703-
} else {
704-
return 'major';
705723
}
724+
725+
return 'major';
706726
}
707727

708728
/**
@@ -770,16 +790,16 @@ function get_temp_dir() {
770790
return $temp;
771791
}
772792

793+
$temp = '/tmp/';
794+
773795
// `sys_get_temp_dir()` introduced PHP 5.2.1.
774796
if ( $try = sys_get_temp_dir() ) {
775797
$temp = trailingslashit( $try );
776798
} elseif ( $try = ini_get( 'upload_tmp_dir' ) ) {
777799
$temp = trailingslashit( $try );
778-
} else {
779-
$temp = '/tmp/';
780800
}
781801

782-
if ( ! @is_writable( $temp ) ) {
802+
if ( ! is_writable( $temp ) ) {
783803
\WP_CLI::warning( "Temp directory isn't writable: {$temp}" );
784804
}
785805

@@ -835,25 +855,28 @@ function parse_ssh_url( $url, $component = -1 ) {
835855
* @access public
836856
* @category Input
837857
*
838-
* @param string $noun Resource being affected (e.g. plugin)
839-
* @param string $verb Type of action happening to the noun (e.g. activate)
840-
* @param integer $total Total number of resource being affected.
841-
* @param integer $successes Number of successful operations.
842-
* @param integer $failures Number of failures.
858+
* @param string $noun Resource being affected (e.g. plugin)
859+
* @param string $verb Type of action happening to the noun (e.g. activate)
860+
* @param integer $total Total number of resource being affected.
861+
* @param integer $successes Number of successful operations.
862+
* @param integer $failures Number of failures.
863+
* @param null|integer $skips Optional. Number of skipped operations. Default null (don't show skips).
843864
*/
844-
function report_batch_operation_results( $noun, $verb, $total, $successes, $failures ) {
865+
function report_batch_operation_results( $noun, $verb, $total, $successes, $failures, $skips = null ) {
845866
$plural_noun = $noun . 's';
846867
$past_tense_verb = past_tense_verb( $verb );
847868
$past_tense_verb_upper = ucfirst( $past_tense_verb );
848869
if ( $failures ) {
870+
$failed_skipped_message = null === $skips ? '' : " ({$failures} failed" . ( $skips ? ", {$skips} skipped" : '' ) . ')';
849871
if ( $successes ) {
850-
WP_CLI::error( "Only {$past_tense_verb} {$successes} of {$total} {$plural_noun}." );
872+
WP_CLI::error( "Only {$past_tense_verb} {$successes} of {$total} {$plural_noun}{$failed_skipped_message}." );
851873
} else {
852-
WP_CLI::error( "No {$plural_noun} {$past_tense_verb}." );
874+
WP_CLI::error( "No {$plural_noun} {$past_tense_verb}{$failed_skipped_message}." );
853875
}
854876
} else {
855-
if ( $successes ) {
856-
WP_CLI::success( "{$past_tense_verb_upper} {$successes} of {$total} {$plural_noun}." );
877+
$skipped_message = $skips ? " ({$skips} skipped)" : '';
878+
if ( $successes || $skips ) {
879+
WP_CLI::success( "{$past_tense_verb_upper} {$successes} of {$total} {$plural_noun}{$skipped_message}." );
857880
} else {
858881
$message = $total > 1 ? ucfirst( $plural_noun ) : ucfirst( $noun );
859882
WP_CLI::success( "{$message} already {$past_tense_verb}." );
@@ -876,7 +899,7 @@ function parse_str_to_argv( $arguments ) {
876899
$argv = array_map(
877900
function( $arg ) {
878901
foreach ( array( '"', "'" ) as $char ) {
879-
if ( $char === substr( $arg, 0, 1 ) && $char === substr( $arg, -1 ) ) {
902+
if ( substr( $arg, 0, 1 ) === $char && substr( $arg, -1 ) === $char ) {
880903
$arg = substr( $arg, 1, -1 );
881904
break;
882905
}
@@ -916,14 +939,15 @@ function basename( $path, $suffix = '' ) {
916939
*
917940
* @return bool
918941
*/
942+
// @codingStandardsIgnoreLine
919943
function isPiped() {
920944
$shellPipe = getenv( 'SHELL_PIPE' );
921945

922-
if ( $shellPipe !== false ) {
946+
if ( false !== $shellPipe ) {
923947
return filter_var( $shellPipe, FILTER_VALIDATE_BOOLEAN );
924-
} else {
925-
return (function_exists( 'posix_isatty' ) && ! posix_isatty( STDOUT ));
926948
}
949+
950+
return (function_exists( 'posix_isatty' ) && ! posix_isatty( STDOUT ));
927951
}
928952

929953
/**
@@ -992,9 +1016,11 @@ function glob_brace( $pattern, $dummy_flags = null ) {
9921016
}
9931017
$current++;
9941018
} else {
995-
if ( ( '}' === $pattern[ $current ] && $depth-- === 0 ) || ( ',' === $pattern[ $current ] && 0 === $depth ) ) {
1019+
if ( ( '}' === $pattern[ $current ] && 0 === $depth-- ) || ( ',' === $pattern[ $current ] && 0 === $depth ) ) {
9961020
break;
997-
} elseif ( '{' === $pattern[ $current++ ] ) {
1021+
}
1022+
1023+
if ( '{' === $pattern[ $current++ ] ) {
9981024
$depth++;
9991025
}
10001026
}
@@ -1038,7 +1064,7 @@ function glob_brace( $pattern, $dummy_flags = null ) {
10381064
. substr( $pattern, $p, $next - $p )
10391065
. substr( $pattern, $rest + 1 );
10401066

1041-
if ( ( $result = glob_brace( $subpattern ) ) ) {
1067+
if ( $result = glob_brace( $subpattern ) ) {
10421068
$paths = array_merge( $paths, $result );
10431069
}
10441070

@@ -1070,6 +1096,31 @@ function glob_brace( $pattern, $dummy_flags = null ) {
10701096
* @return string
10711097
*/
10721098
function get_suggestion( $target, array $options, $threshold = 2 ) {
1099+
1100+
$suggestion_map = array(
1101+
'check' => 'check-update',
1102+
'clear' => 'flush',
1103+
'decrement' => 'decr',
1104+
'del' => 'delete',
1105+
'directory' => 'dir',
1106+
'exec' => 'eval',
1107+
'exec-file' => 'eval-file',
1108+
'increment' => 'incr',
1109+
'language' => 'locale',
1110+
'lang' => 'locale',
1111+
'new' => 'create',
1112+
'number' => 'count',
1113+
'remove' => 'delete',
1114+
'regen' => 'regenerate',
1115+
'rep' => 'replace',
1116+
'repl' => 'replace',
1117+
'v' => 'version',
1118+
);
1119+
1120+
if ( array_key_exists( $target, $suggestion_map ) ) {
1121+
return $suggestion_map[ $target ];
1122+
}
1123+
10731124
if ( empty( $options ) ) {
10741125
return '';
10751126
}

features/install-wp-tests.feature

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ Feature: Scaffold install-wp-tests.sh tests
115115
wp-trackback.php
116116
xmlrpc.php
117117
"""
118-
And the /tmp/behat-wordpress/wp-includes/version.php file should contain:
119-
"""
120-
-alpha-
121-
"""
118+
And the contents of the /tmp/behat-wordpress/wp-includes/version.php file should match /\-(alpha|beta[0-9]+)\-/
122119
And the {PLUGIN_DIR}/hello-world/phpunit.xml.dist file should exist
123120

124121
When I run `echo 'show databases' | mysql -u root`

features/steps/then.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function ( $world, $stream, $operator, $goal_ver ) {
153153
throw new Exception( $world->result );
154154
}
155155
}
156-
);
156+
);
157157

158158
$steps->Then( '/^the (.+) (file|directory) should (exist|not exist|be:|contain:|not contain:)$/',
159159
function ( $world, $path, $type, $action, $expected = null ) {
@@ -200,6 +200,25 @@ function ( $world, $path, $type, $action, $expected = null ) {
200200
}
201201
);
202202

203+
$steps->Then( '/^the contents of the (.+) file should match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
204+
function ( $world, $path, $expected ) {
205+
$path = $world->replace_variables( $path );
206+
// If it's a relative path, make it relative to the current test dir
207+
if ( '/' !== $path[0] ) {
208+
$path = $world->variables['RUN_DIR'] . "/$path";
209+
}
210+
$contents = file_get_contents( $path );
211+
assertRegExp( $expected, $contents );
212+
}
213+
);
214+
215+
$steps->Then( '/^(STDOUT|STDERR) should match (((\/.+\/)|(#.+#))([a-z]+)?)$/',
216+
function ( $world, $stream, $expected ) {
217+
$stream = strtolower( $stream );
218+
assertRegExp( $expected, $world->result->$stream );
219+
}
220+
);
221+
203222
$steps->Then( '/^an email should (be sent|not be sent)$/', function( $world, $expected ) {
204223
if ( 'be sent' === $expected ) {
205224
assertNotEquals( 0, $world->email_sends );

templates/install-wp-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ WP_VERSION=${5-latest}
1313
SKIP_DB_CREATE=${6-false}
1414

1515
TMPDIR=${TMPDIR-/tmp}
16+
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
1617
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
1718
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/}
1819

0 commit comments

Comments
 (0)