OSDN Git Service

behat: introduce Process class
authorscribu <mail@scribu.net>
Fri, 3 May 2013 18:45:09 +0000 (21:45 +0300)
committerscribu <mail@scribu.net>
Fri, 3 May 2013 22:09:53 +0000 (01:09 +0300)
The run_check() method is a shortcut for implicitly checking that a
command runs succesfully.

features/bootstrap/FeatureContext.php
features/bootstrap/Process.php [new file with mode: 0644]
features/steps/basic_steps.php

index 0404017..ab61ffe 100644 (file)
@@ -94,14 +94,14 @@ class FeatureContext extends BehatContext implements ClosuredContextInterface {
 
                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 ) {
@@ -122,7 +122,7 @@ class FeatureContext extends BehatContext implements ClosuredContextInterface {
                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 );
@@ -131,33 +131,7 @@ class FeatureContext extends BehatContext implements ClosuredContextInterface {
                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 ) {
@@ -175,25 +149,23 @@ class FeatureContext extends BehatContext implements ClosuredContextInterface {
                // 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 );
        }
 }
 
diff --git a/features/bootstrap/Process.php b/features/bootstrap/Process.php
new file mode 100644 (file)
index 0000000..0776a28
--- /dev/null
@@ -0,0 +1,58 @@
+<?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;
+       }
+}
+
index 0002a6c..3b6905e 100644 (file)
@@ -25,7 +25,7 @@ $steps->Given( '/^WP files$/',
 
 $steps->Given( '/^wp-config\.php$/',
        function ( $world ) {
-               $world->run( 'core config' );
+               $world->proc( 'core config' )->run_check();
        }
 );
 
@@ -50,7 +50,7 @@ $steps->Given( "/^a WP install in '([^\s]+)'$/",
 $steps->Given( '/^a WP multisite install$/',
        function ( $world ) {
                $world->wp_install();
-               $world->run( 'core install-network' );
+               $world->proc( 'core install-network' )->run_check();
        }
 );
 
@@ -96,29 +96,29 @@ $steps->Given( '/^a google-sitemap-generator-cli plugin zip$/',
 
 $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 );
        }
 );
 
@@ -127,7 +127,7 @@ $steps->When( '/^I run the previous command again$/',
                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();
        }
 );
 
@@ -136,7 +136,7 @@ $steps->When( '/^I try to import it$/',
                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();
        }
 );
 
@@ -234,7 +234,7 @@ $steps->Then( '/^STDOUT should be JSON containing:$/',
 
 $steps->Then( '/^STDOUT should be CSV containing:$/',
        function( $world, PyStringNode $expected ) {
-               
+
                $output        = $world->result->STDOUT;
                $expected      = $world->replace_variables( (string) $expected );