From 65b93d0eab0d63966ecf2a4f4579f8f7214ac63b Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 17:37:55 +0100 Subject: [PATCH 1/5] Cater for Windows bash. Use function_exists. Check TERM. Test arg. --- lib/cli/Shell.php | 38 +++++++++++++++++++++++++++----------- tests/test-shell.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 tests/test-shell.php diff --git a/lib/cli/Shell.php b/lib/cli/Shell.php index 1adee79..111c759 100755 --- a/lib/cli/Shell.php +++ b/lib/cli/Shell.php @@ -21,27 +21,43 @@ class Shell { /** * Returns the number of columns the current shell has for display. * + * @param mixed $test For testing only. + * * @return int The number of columns. * @todo Test on more systems. */ - static public function columns() { + static public function columns( $test = null ) { static $columns; + if ( null !== $test ) { + $columns = null; + } if ( null === $columns ) { - if (self::is_windows() ) { - $output = array(); - exec('mode CON', $output); - foreach ($output as $line) { - if (preg_match('/Columns:( )*([0-9]+)/', $line, $matches)) { - $columns = (int)$matches[2]; - break; + if ( function_exists( 'exec' ) ) { + if ( self::is_windows( 'WIN' === $test ) ) { + // Cater for shells such as Cygwin and Git bash where `mode CON` returns an incorrect value for columns. + if ( ( $shell = getenv( 'SHELL' ) ) && preg_match( '/(?:bash|zsh)(?:.exe)?$/', $shell ) && getenv( 'TERM' ) ) { + $columns = (int) exec( 'tput cols' ); + } + if ( ! $columns ) { + $return_var = -1; + $output = array(); + exec( 'mode CON', $output, $return_var ); + if ( 0 === $return_var && $output ) { + // Look for second line ending in ": " (searching for "Columns:" will fail on non-English locales). + if ( preg_match( '/:\s*[0-9]+\n[^:]+:\s*([0-9]+)\n/', implode( "\n", $output ), $matches ) ) { + $columns = (int) $matches[1]; + } + } + } + } else { + if ( getenv( 'TERM' ) ) { + $columns = (int) exec( '/usr/bin/env tput cols' ); } } - } else if (!preg_match('/(^|,)(\s*)?exec(\s*)?(,|$)/', ini_get('disable_functions'))) { - $columns = (int) exec('/usr/bin/env tput cols'); } - if ( !$columns ) { + if ( ! $columns ) { $columns = 80; // default width of cmd window on Windows OS } } diff --git a/tests/test-shell.php b/tests/test-shell.php new file mode 100644 index 0000000..b00051d --- /dev/null +++ b/tests/test-shell.php @@ -0,0 +1,39 @@ +assertSame( 80, $columns ); + $columns = cli\Shell::columns( 'WIN' /*test*/ ); + $this->assertSame( 80, $columns ); + + // TERM and COLUMNS should result in whatever COLUMNS is. + + putenv( 'TERM=vt100' ); + putenv( 'COLUMNS=100' ); + $columns = cli\Shell::columns( true /*test*/ ); + $this->assertSame( 100, $columns ); + $columns = cli\Shell::columns( 'WIN' /*test*/ ); + $this->assertSame( 100, $columns ); + + // Restore. + putenv( false === $env_term ? 'TERM' : "TERM=$env_term" ); + putenv( false === $env_columns ? 'COLUMNS' : "COLUMNS=$env_columns" ); + } +} From 1e227ab0207ea8c8d49070e69ac8a89fb1fb1e73 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 19:51:09 +0100 Subject: [PATCH 2/5] Use env vars instead of test args. --- lib/cli/Shell.php | 8 +++----- tests/test-shell.php | 22 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/cli/Shell.php b/lib/cli/Shell.php index 111c759..7e525af 100755 --- a/lib/cli/Shell.php +++ b/lib/cli/Shell.php @@ -21,20 +21,18 @@ class Shell { /** * Returns the number of columns the current shell has for display. * - * @param mixed $test For testing only. - * * @return int The number of columns. * @todo Test on more systems. */ - static public function columns( $test = null ) { + static public function columns() { static $columns; - if ( null !== $test ) { + if ( false !== ( $env_reset = getenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET' ) ) && $env_reset ) { $columns = null; } if ( null === $columns ) { if ( function_exists( 'exec' ) ) { - if ( self::is_windows( 'WIN' === $test ) ) { + if ( self::is_windows() ) { // Cater for shells such as Cygwin and Git bash where `mode CON` returns an incorrect value for columns. if ( ( $shell = getenv( 'SHELL' ) ) && preg_match( '/(?:bash|zsh)(?:.exe)?$/', $shell ) && getenv( 'TERM' ) ) { $columns = (int) exec( 'tput cols' ); diff --git a/tests/test-shell.php b/tests/test-shell.php index b00051d..ddaef92 100644 --- a/tests/test-shell.php +++ b/tests/test-shell.php @@ -14,26 +14,40 @@ function testColumns() { // Save. $env_term = getenv( 'TERM' ); $env_columns = getenv( 'COLUMNS' ); + $env_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' ); + $env_shell_columns_reset = getenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET' ); + + putenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET=1' ); // No TERM should result in default 80. putenv( 'TERM' ); - $columns = cli\Shell::columns( true /*test*/ ); + + putenv( 'WP_CLI_TEST_IS_WINDOWS=0' ); + $columns = cli\Shell::columns(); $this->assertSame( 80, $columns ); - $columns = cli\Shell::columns( 'WIN' /*test*/ ); + + putenv( 'WP_CLI_TEST_IS_WINDOWS=1' ); + $columns = cli\Shell::columns(); $this->assertSame( 80, $columns ); // TERM and COLUMNS should result in whatever COLUMNS is. putenv( 'TERM=vt100' ); putenv( 'COLUMNS=100' ); - $columns = cli\Shell::columns( true /*test*/ ); + + putenv( 'WP_CLI_TEST_IS_WINDOWS=0' ); + $columns = cli\Shell::columns(); $this->assertSame( 100, $columns ); - $columns = cli\Shell::columns( 'WIN' /*test*/ ); + + putenv( 'WP_CLI_TEST_IS_WINDOWS=1' ); + $columns = cli\Shell::columns(); $this->assertSame( 100, $columns ); // Restore. putenv( false === $env_term ? 'TERM' : "TERM=$env_term" ); putenv( false === $env_columns ? 'COLUMNS' : "COLUMNS=$env_columns" ); + putenv( false === $env_is_windows ? 'WP_CLI_TEST_IS_WINDOWS' : "WP_CLI_TEST_IS_WINDOWS=$env_is_windows" ); + putenv( false === $env_shell_columns_reset ? 'WP_CLI_TEST_SHELL_COLUMNS_RESET' : "WP_CLI_TEST_SHELL_COLUMNS_RESET=$env_shell_columns_reset" ); } } From 8471ed138d838b0b8102e204685e8f472b5a6e90 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 5 Jun 2017 20:28:25 +0100 Subject: [PATCH 3/5] No need to overdo WP_CLI_TEST_SHELL_COLUMNS_RESET check. --- lib/cli/Shell.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli/Shell.php b/lib/cli/Shell.php index 7e525af..b4f8f7a 100755 --- a/lib/cli/Shell.php +++ b/lib/cli/Shell.php @@ -27,7 +27,7 @@ class Shell { static public function columns() { static $columns; - if ( false !== ( $env_reset = getenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET' ) ) && $env_reset ) { + if ( getenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET' ) ) { $columns = null; } if ( null === $columns ) { From 3361cc1182d0900a8f5174a1c4da6cd80fc3d734 Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 6 Jun 2017 17:40:36 +0100 Subject: [PATCH 4/5] Fix WP_CLI_TEST_SHELL_COLUMNS_RESET -> PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET --- lib/cli/Shell.php | 2 +- tests/test-shell.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cli/Shell.php b/lib/cli/Shell.php index b4f8f7a..39894d1 100755 --- a/lib/cli/Shell.php +++ b/lib/cli/Shell.php @@ -27,7 +27,7 @@ class Shell { static public function columns() { static $columns; - if ( getenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET' ) ) { + if ( getenv( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' ) ) { $columns = null; } if ( null === $columns ) { diff --git a/tests/test-shell.php b/tests/test-shell.php index ddaef92..702948c 100644 --- a/tests/test-shell.php +++ b/tests/test-shell.php @@ -15,9 +15,9 @@ function testColumns() { $env_term = getenv( 'TERM' ); $env_columns = getenv( 'COLUMNS' ); $env_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' ); - $env_shell_columns_reset = getenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET' ); + $env_shell_columns_reset = getenv( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' ); - putenv( 'WP_CLI_TEST_SHELL_COLUMNS_RESET=1' ); + putenv( 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET=1' ); // No TERM should result in default 80. @@ -48,6 +48,6 @@ function testColumns() { putenv( false === $env_term ? 'TERM' : "TERM=$env_term" ); putenv( false === $env_columns ? 'COLUMNS' : "COLUMNS=$env_columns" ); putenv( false === $env_is_windows ? 'WP_CLI_TEST_IS_WINDOWS' : "WP_CLI_TEST_IS_WINDOWS=$env_is_windows" ); - putenv( false === $env_shell_columns_reset ? 'WP_CLI_TEST_SHELL_COLUMNS_RESET' : "WP_CLI_TEST_SHELL_COLUMNS_RESET=$env_shell_columns_reset" ); + putenv( false === $env_shell_columns_reset ? 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' : "PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET=$env_shell_columns_reset" ); } } From 314caecc406c858b361626483eafee466eb2ef5e Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 6 Jun 2017 17:46:00 +0100 Subject: [PATCH 5/5] Dot in ".exe" preg should be escaped. --- lib/cli/Shell.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli/Shell.php b/lib/cli/Shell.php index 39894d1..7b3f9bb 100755 --- a/lib/cli/Shell.php +++ b/lib/cli/Shell.php @@ -34,7 +34,7 @@ static public function columns() { if ( function_exists( 'exec' ) ) { if ( self::is_windows() ) { // Cater for shells such as Cygwin and Git bash where `mode CON` returns an incorrect value for columns. - if ( ( $shell = getenv( 'SHELL' ) ) && preg_match( '/(?:bash|zsh)(?:.exe)?$/', $shell ) && getenv( 'TERM' ) ) { + if ( ( $shell = getenv( 'SHELL' ) ) && preg_match( '/(?:bash|zsh)(?:\.exe)?$/', $shell ) && getenv( 'TERM' ) ) { $columns = (int) exec( 'tput cols' ); } if ( ! $columns ) {