diff --git a/lib/cli/Shell.php b/lib/cli/Shell.php index b9ae380..9afb257 100755 --- a/lib/cli/Shell.php +++ b/lib/cli/Shell.php @@ -31,7 +31,7 @@ static public function columns() { $columns = null; } if ( null === $columns ) { - if ( function_exists( 'exec' ) ) { + if ( ! ( $columns = (int) getenv( 'COLUMNS' ) ) && 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' ) ) { @@ -49,15 +49,13 @@ static public function columns() { } } } else { - if ( ! ( $columns = (int) getenv( 'COLUMNS' ) ) ) { - $size = exec( '/usr/bin/env stty size 2>/dev/null' ); - if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) { - $columns = (int) $matches[1]; - } - if ( ! $columns ) { - if ( getenv( 'TERM' ) ) { - $columns = (int) exec( '/usr/bin/env tput cols 2>/dev/null' ); - } + $size = exec( '/usr/bin/env stty size 2>/dev/null' ); + if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) { + $columns = (int) $matches[1]; + } + if ( ! $columns ) { + if ( getenv( 'TERM' ) ) { + $columns = (int) exec( '/usr/bin/env tput cols 2>/dev/null' ); } } } @@ -114,7 +112,7 @@ static public function hide($hidden = true) { */ static public function is_windows() { $test_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' ); - if ( false !== $test_is_windows ) { + if ( false !== $test_is_windows && '' !== $test_is_windows ) { return (bool) $test_is_windows; } return strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN'; diff --git a/tests/Test_Shell.php b/tests/Test_Shell.php index d2bc71e..f793027 100644 --- a/tests/Test_Shell.php +++ b/tests/Test_Shell.php @@ -49,7 +49,15 @@ function testColumns() { // 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" ); + if ( false === $env_is_windows ) { + if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) { + putenv( 'WP_CLI_TEST_IS_WINDOWS=' ); + } else { + putenv( 'WP_CLI_TEST_IS_WINDOWS' ); + } + } else { + putenv( "WP_CLI_TEST_IS_WINDOWS=$env_is_windows" ); + } putenv( false === $env_shell_columns_reset ? 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' : "PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET=$env_shell_columns_reset" ); } } diff --git a/tests/Test_Table.php b/tests/Test_Table.php index ca03f71..eb6a2eb 100644 --- a/tests/Test_Table.php +++ b/tests/Test_Table.php @@ -3,6 +3,7 @@ use cli\Colors; use cli\Table; use cli\Table\Ascii; +use cli\Shell; use WP_CLI\Tests\TestCase; /** @@ -85,7 +86,7 @@ public function test_column_odd_single_width_with_double_width() { $strip_borders = function ( $a ) { return array_map( function ( $v ) { - return substr( $v, 2, -2 ); + return substr( rtrim( $v, "\r" ), 2, -2 ); }, $a ); }; @@ -96,7 +97,7 @@ public function test_column_odd_single_width_with_double_width() { $result = $strip_borders( explode( "\n", $out ) ); $this->assertSame( 3, count( $result ) ); - $this->assertSame( '1あいうえ ', $result[0] ); // 1 single width, 4 double-width, space = 10. + $this->assertSame( '1あいうえ ', $result[0] ); // 1 single-width, 4 double-width, space = 10. $this->assertSame( 'おか2きくカ', $result[1] ); // 2 double-width, 1 single-width, 2 double-width, 1 half-width = 10. $this->assertSame( 'けこ ', $result[2] ); // 2 double-width, 8 spaces = 10. diff --git a/tests/Test_Table_Ascii.php b/tests/Test_Table_Ascii.php index 3e683b8..39cc99f 100644 --- a/tests/Test_Table_Ascii.php +++ b/tests/Test_Table_Ascii.php @@ -386,6 +386,9 @@ private function assertInOutEquals(array $input, $output) { * @param mixed $expected Expected output */ private function assertOutFileEqualsWith($expected) { - $this->assertEquals($expected, file_get_contents($this->_mockFile)); + $actual = file_get_contents($this->_mockFile); + $actual = str_replace("\r\n", "\n", $actual); + $expected = str_replace("\r\n", "\n", $expected); + $this->assertEquals($expected, $actual); } }