if ( !$path ) {
$path = sys_get_temp_dir() . '/wp-cli-test-cache';
- system( Utils\esc_cmd( 'mkdir -p %s', $path ) );
+ Process::create( Utils\esc_cmd( 'mkdir -p %s', $path ) )->run_check();
}
return $path . '/' . $file;
}
public function download_file( $url, $path ) {
- system( Utils\esc_cmd( 'curl -sSL %s > %s', $url, $path ) );
+ Process::create( Utils\esc_cmd( 'curl -sSL %s > %s', $url, $path ) )->run_check();
}
private static function run_sql( $sql ) {
self::run_sql( "DROP DATABASE IF EXISTS $dbname" );
}
- public function run( $command, $assoc_args = array(), $subdir = '' ) {
+ public function proc( $command, $assoc_args = array() ) {
if ( isset( self::$additional_args[ $command ] ) ) {
$assoc_args = array_merge( self::$additional_args[ $command ],
$assoc_args );
if ( !empty( $assoc_args ) )
$command .= Utils\assoc_args_to_str( $assoc_args );
- $subdir = $this->get_path( $subdir );
-
- $cmd = __DIR__ . "/../../bin/wp $command";
-
- $descriptors = array(
- 0 => STDIN,
- 1 => array( 'pipe', 'w' ),
- 2 => array( 'pipe', 'w' ),
- );
-
- $proc = proc_open( $cmd, $descriptors, $pipes, $subdir );
-
- $STDOUT = stream_get_contents( $pipes[1] );
- fclose( $pipes[1] );
-
- $STDERR = stream_get_contents( $pipes[2] );
- fclose( $pipes[2] );
-
- $r = (object) array(
- 'command' => $command,
- 'STDOUT' => $STDOUT,
- 'STDERR' => $STDERR,
- 'return_code' => proc_close( $proc ),
- 'cwd' => $this->install_dir
- );
-
- return $r;
+ return Process::create( __DIR__ . "/../../bin/wp $command", $this->install_dir );
}
public function move_files( $src, $dest ) {
// Ideally, we'd cache at the HTTP layer for more reliable tests
$cache_dir = sys_get_temp_dir() . '/wp-cli-test-core-download-cache';
- $r = $this->run( 'core download', array(
+ $r = $this->proc( 'core download', array(
'path' => $cache_dir
- ) );
+ ) )->run();
$dest_dir = $this->get_path( $subdir );
if ( $subdir ) mkdir( $dest_dir );
- $cmd = Utils\esc_cmd( "cp -r %s/* %s", $cache_dir, $dest_dir );
-
- system( $cmd );
+ Process::create( Utils\esc_cmd( "cp -r %s/* %s", $cache_dir, $dest_dir ) )->run_check();
}
public function wp_install( $subdir = '' ) {
$this->create_db();
$this->create_empty_dir();
$this->download_wordpress_files( $subdir );
- $this->run( 'core config', array(), $subdir );
- $this->run( 'core install', array(), $subdir );
+ $this->proc( 'core config' )->run_check( $subdir );
+ $this->proc( 'core install' )->run_check( $subdir );
}
}
--- /dev/null
+<?php
+
+class Process {
+
+ public static function create( $command, $cwd = null ) {
+ $proc = new self;
+
+ $proc->command = $command;
+ $proc->cwd = $cwd;
+
+ return $proc;
+ }
+
+ private $command, $cwd;
+
+ private function __construct() {}
+
+ public function run( $subdir = '' ) {
+ $cwd = $this->cwd;
+ if ( $subdir ) {
+ $cwd .= '/' . $subdir;
+ }
+
+ $descriptors = array(
+ 0 => STDIN,
+ 1 => array( 'pipe', 'w' ),
+ 2 => array( 'pipe', 'w' ),
+ );
+
+ $proc = proc_open( $this->command, $descriptors, $pipes, $cwd );
+
+ $STDOUT = stream_get_contents( $pipes[1] );
+ fclose( $pipes[1] );
+
+ $STDERR = stream_get_contents( $pipes[2] );
+ fclose( $pipes[2] );
+
+ return (object) array(
+ 'STDOUT' => $STDOUT,
+ 'STDERR' => $STDERR,
+ 'return_code' => proc_close( $proc ),
+ 'command' => $this->command,
+ 'cwd' => $cwd
+ );
+ }
+
+ public function run_check( $subdir = '' ) {
+ $r = $this->run( $subdir );
+
+ if ( $r->return_code ) {
+ throw new \RuntimeException( sprintf( "%s: %s\ncwd: %s",
+ $r->command, $r->STDERR, $r->cwd ) );
+ }
+
+ return $r;
+ }
+}
+
$steps->Given( '/^wp-config\.php$/',
function ( $world ) {
- $world->run( 'core config' );
+ $world->proc( 'core config' )->run_check();
}
);
$steps->Given( '/^a WP multisite install$/',
function ( $world ) {
$world->wp_install();
- $world->run( 'core install-network' );
+ $world->proc( 'core install-network' )->run_check();
}
);
$steps->Given( '/^a large image file$/',
function ( $world ) {
- $image_file = 'http://wordpresswallpaper.com/wp-content/gallery/photo-based-wallpaper/1058.jpg';
+ $image_file = 'http://wordpresswallpaper.com/wp-content/gallery/photo-based-wallpaper/1058.jpg';
- $world->variables['DOWNLOADED_IMAGE'] = $world->get_cache_path( 'wallpaper.jpg' );
+ $world->variables['DOWNLOADED_IMAGE'] = $world->get_cache_path( 'wallpaper.jpg' );
- $world->download_file( $image_file, $world->variables['DOWNLOADED_IMAGE'] );
+ $world->download_file( $image_file, $world->variables['DOWNLOADED_IMAGE'] );
}
);
$steps->When( '/^I run `wp`$/',
function ( $world ) {
- $world->result = $world->run( '' );
+ $world->result = $world->proc( '' )->run();
}
);
$steps->When( '/^I run `wp (.+)`$/',
function ( $world, $cmd ) {
- $world->result = $world->run( $world->replace_variables( $cmd ) );
+ $world->result = $world->proc( $world->replace_variables( $cmd ) )->run();
}
);
$steps->When( "/^I run `wp (.+)` from '([^\s]+)'$/",
function ( $world, $cmd, $subdir ) {
- $world->result = $world->run( $world->replace_variables( $cmd ), array(), $subdir );
+ $world->result = $world->proc( $world->replace_variables( $cmd ) )->run( $subdir );
}
);
if ( !isset( $world->result ) )
throw new \Exception( 'No previous command.' );
- $world->result = $world->run( $world->result->command );
+ $world->result = Process::create( $world->result->command, $world->result->cwd )->run();
}
);
if ( !isset( $world->variables['DOWNLOADED_IMAGE'] ) )
throw new \Exception( 'Cached image not available.' );
- $world->result = $world->run( 'media import ' . $world->variables['DOWNLOADED_IMAGE'] . ' --post_id=1 --featured_image' );
+ $world->result = $world->proc( 'media import ' . $world->variables['DOWNLOADED_IMAGE'] . ' --post_id=1 --featured_image' )->run();
}
);
$steps->Then( '/^STDOUT should be CSV containing:$/',
function( $world, PyStringNode $expected ) {
-
+
$output = $world->result->STDOUT;
$expected = $world->replace_variables( (string) $expected );