Skip to content

Commit c3d2513

Browse files
authored
Tests: Improve Windows compatibility (#200)
1 parent b9b72f0 commit c3d2513

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

lib/cli/Shell.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static public function columns() {
3131
$columns = null;
3232
}
3333
if ( null === $columns ) {
34-
if ( function_exists( 'exec' ) ) {
34+
if ( ! ( $columns = (int) getenv( 'COLUMNS' ) ) && function_exists( 'exec' ) ) {
3535
if ( self::is_windows() ) {
3636
// Cater for shells such as Cygwin and Git bash where `mode CON` returns an incorrect value for columns.
3737
if ( ( $shell = getenv( 'SHELL' ) ) && preg_match( '/(?:bash|zsh)(?:\.exe)?$/', $shell ) && getenv( 'TERM' ) ) {
@@ -49,15 +49,13 @@ static public function columns() {
4949
}
5050
}
5151
} else {
52-
if ( ! ( $columns = (int) getenv( 'COLUMNS' ) ) ) {
53-
$size = exec( '/usr/bin/env stty size 2>/dev/null' );
54-
if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) {
55-
$columns = (int) $matches[1];
56-
}
57-
if ( ! $columns ) {
58-
if ( getenv( 'TERM' ) ) {
59-
$columns = (int) exec( '/usr/bin/env tput cols 2>/dev/null' );
60-
}
52+
$size = exec( '/usr/bin/env stty size 2>/dev/null' );
53+
if ( '' !== $size && preg_match( '/[0-9]+ ([0-9]+)/', $size, $matches ) ) {
54+
$columns = (int) $matches[1];
55+
}
56+
if ( ! $columns ) {
57+
if ( getenv( 'TERM' ) ) {
58+
$columns = (int) exec( '/usr/bin/env tput cols 2>/dev/null' );
6159
}
6260
}
6361
}
@@ -114,7 +112,7 @@ static public function hide($hidden = true) {
114112
*/
115113
static public function is_windows() {
116114
$test_is_windows = getenv( 'WP_CLI_TEST_IS_WINDOWS' );
117-
if ( false !== $test_is_windows ) {
115+
if ( false !== $test_is_windows && '' !== $test_is_windows ) {
118116
return (bool) $test_is_windows;
119117
}
120118
return strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN';

tests/Test_Shell.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ function testColumns() {
4949
// Restore.
5050
putenv( false === $env_term ? 'TERM' : "TERM=$env_term" );
5151
putenv( false === $env_columns ? 'COLUMNS' : "COLUMNS=$env_columns" );
52-
putenv( false === $env_is_windows ? 'WP_CLI_TEST_IS_WINDOWS' : "WP_CLI_TEST_IS_WINDOWS=$env_is_windows" );
52+
if ( false === $env_is_windows ) {
53+
if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) {
54+
putenv( 'WP_CLI_TEST_IS_WINDOWS=' );
55+
} else {
56+
putenv( 'WP_CLI_TEST_IS_WINDOWS' );
57+
}
58+
} else {
59+
putenv( "WP_CLI_TEST_IS_WINDOWS=$env_is_windows" );
60+
}
5361
putenv( false === $env_shell_columns_reset ? 'PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET' : "PHP_CLI_TOOLS_TEST_SHELL_COLUMNS_RESET=$env_shell_columns_reset" );
5462
}
5563
}

tests/Test_Table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use cli\Colors;
44
use cli\Table;
55
use cli\Table\Ascii;
6+
use cli\Shell;
67
use WP_CLI\Tests\TestCase;
78

89
/**
@@ -85,7 +86,7 @@ public function test_column_odd_single_width_with_double_width() {
8586

8687
$strip_borders = function ( $a ) {
8788
return array_map( function ( $v ) {
88-
return substr( $v, 2, -2 );
89+
return substr( rtrim( $v, "\r" ), 2, -2 );
8990
}, $a );
9091
};
9192

@@ -96,7 +97,7 @@ public function test_column_odd_single_width_with_double_width() {
9697
$result = $strip_borders( explode( "\n", $out ) );
9798

9899
$this->assertSame( 3, count( $result ) );
99-
$this->assertSame( '1あいうえ ', $result[0] ); // 1 single width, 4 double-width, space = 10.
100+
$this->assertSame( '1あいうえ ', $result[0] ); // 1 single-width, 4 double-width, space = 10.
100101
$this->assertSame( 'おか2きくカ', $result[1] ); // 2 double-width, 1 single-width, 2 double-width, 1 half-width = 10.
101102
$this->assertSame( 'けこ ', $result[2] ); // 2 double-width, 8 spaces = 10.
102103

tests/Test_Table_Ascii.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ private function assertInOutEquals(array $input, $output) {
386386
* @param mixed $expected Expected output
387387
*/
388388
private function assertOutFileEqualsWith($expected) {
389-
$this->assertEquals($expected, file_get_contents($this->_mockFile));
389+
$actual = file_get_contents($this->_mockFile);
390+
$actual = str_replace("\r\n", "\n", $actual);
391+
$expected = str_replace("\r\n", "\n", $expected);
392+
$this->assertEquals($expected, $actual);
390393
}
391394
}

0 commit comments

Comments
 (0)