From 37556088271bb0e160e5ead29170e33fe202aa34 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 04:00:09 +0100 Subject: [PATCH 1/5] Add new function WP_CLI\Utils::maybe_prefix_env(). --- php/utils.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/php/utils.php b/php/utils.php index 986fd29235..8d5585857b 100644 --- a/php/utils.php +++ b/php/utils.php @@ -980,3 +980,17 @@ function is_bundled_command( $command ) { ? array_key_exists( $command, $classes ) : false; } + +/** + * Maybe prefix command string with "/usr/bin/env". + * + * @param string $command + * + * @return string + */ +function maybe_prefix_env( $command ) { + if ( ! is_windows() ) { + $command = '/usr/bin/env ' . $command; + } + return $command; +} From fb26c6bc6c3fbc2db37852a87f2fa0c65dc310f4 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 04:44:49 +0100 Subject: [PATCH 2/5] Use maybe_prefix_env() in FeatureContent::run_sql(). --- features/bootstrap/FeatureContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index ed863a6271..d14b31a56f 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -309,7 +309,7 @@ private function set_cache_dir() { } private static function run_sql( $sql ) { - Utils\run_mysql_command( '/usr/bin/env mysql --no-defaults', array( + Utils\run_mysql_command( Utils\maybe_prefix_env( 'mysql --no-defaults' ), array( 'execute' => $sql, 'host' => self::$db_settings['dbhost'], 'user' => self::$db_settings['dbuser'], From 4c0abd09650ce722b7ed2cec8c7e23b327b35e7f Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 15:10:21 +0100 Subject: [PATCH 3/5] Make Utils\maybe_prefix_env() flexible and call directly in run_mysql_command(). --- features/bootstrap/FeatureContext.php | 2 +- php/utils.php | 14 +++++++++++--- tests/test-utils.php | 5 ++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index d14b31a56f..ed863a6271 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -309,7 +309,7 @@ private function set_cache_dir() { } private static function run_sql( $sql ) { - Utils\run_mysql_command( Utils\maybe_prefix_env( 'mysql --no-defaults' ), array( + Utils\run_mysql_command( '/usr/bin/env mysql --no-defaults', array( 'execute' => $sql, 'host' => self::$db_settings['dbhost'], 'user' => self::$db_settings['dbuser'], diff --git a/php/utils.php b/php/utils.php index 8d5585857b..6b86fd8573 100644 --- a/php/utils.php +++ b/php/utils.php @@ -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 = maybe_prefix_env( $cmd ) . assoc_args_to_str( $assoc_args ); $proc = proc_open( $final_cmd, $descriptors, $pipes ); if ( !$proc ) @@ -989,8 +989,16 @@ function is_bundled_command( $command ) { * @return string */ function maybe_prefix_env( $command ) { - if ( ! is_windows() ) { - $command = '/usr/bin/env ' . $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; } diff --git a/tests/test-utils.php b/tests/test-utils.php index 9363c99d17..f727c03ad6 100644 --- a/tests/test-utils.php +++ b/tests/test-utils.php @@ -143,5 +143,8 @@ public function testAssocArgsToString() { ) ) ); } + public function testMaybePreviewEnv() { + $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( 'cmd' ) ); + $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( '/usr/bin/env cmd' ) ); + } } - From d2be9e877f62b565fdd6066657dd359463d68e98 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 15:55:25 +0100 Subject: [PATCH 4/5] Make maybe_prefix_env test OS sensitive. --- tests/test-utils.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/test-utils.php b/tests/test-utils.php index f727c03ad6..519029fd41 100644 --- a/tests/test-utils.php +++ b/tests/test-utils.php @@ -144,7 +144,12 @@ public function testAssocArgsToString() { } public function testMaybePreviewEnv() { - $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( 'cmd' ) ); - $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( '/usr/bin/env cmd' ) ); + if ( 0 === strncasecmp( 'WIN', PHP_OS, 3 ) ) { + $this->assertSame( 'cmd', Utils\maybe_prefix_env( 'cmd' ) ); + $this->assertSame( 'cmd', Utils\maybe_prefix_env( '/usr/bin/env cmd' ) ); + } else { + $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( 'cmd' ) ); + $this->assertSame( '/usr/bin/env cmd', Utils\maybe_prefix_env( '/usr/bin/env cmd' ) ); + } } } From c40a740c37bc1da19290cfd6f74e0949d11ff0c3 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 15:59:29 +0100 Subject: [PATCH 5/5] Update comment. --- php/utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/php/utils.php b/php/utils.php index 6b86fd8573..e4798e5410 100644 --- a/php/utils.php +++ b/php/utils.php @@ -983,6 +983,7 @@ function is_bundled_command( $command ) { /** * Maybe prefix command string with "/usr/bin/env". + * Removes (if there) if Windows, adds (if not there) if not. * * @param string $command *