Skip to content

Commit 01b9fa3

Browse files
swissspidyCopilot
andauthored
Tests: Improve Windows compatibility (#232)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent fad1969 commit 01b9fa3

5 files changed

Lines changed: 54 additions & 50 deletions

File tree

features/package-install.feature

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Feature: Install WP-CLI packages
243243
"""
244244
And the {PACKAGE_PATH}composer.json file should not contain:
245245
"""
246-
"wp-cli/google-sitemap-generator-cli": "dev-master"
246+
"wp-cli/google-sitemap-generator-cli":
247247
"""
248248
And the {PACKAGE_PATH}composer.json file should not contain:
249249
"""
@@ -474,8 +474,9 @@ Feature: Install WP-CLI packages
474474
Scenario: Install a package from Git using a shortened package identifier
475475
Given an empty directory
476476
477-
When I run `wp package install wp-cli-test/github-test-command`
478-
Then STDOUT should contain:
477+
When I try `wp package install wp-cli-test/github-test-command`
478+
Then the return code should be 0
479+
And STDOUT should contain:
479480
"""
480481
Installing package wp-cli-test/github-test-command (dev-master)
481482
"""
@@ -792,7 +793,7 @@ Feature: Install WP-CLI packages
792793
"""
793794
Warning: Package name mismatch...Updating from git name 'GeekPress/wp-rocket-cli' to composer.json name 'wp-media/wp-rocket-cli'.
794795
"""
795-
And STDOUT should match /Installing package wp-media\/wp-rocket-cli \(dev-/
796+
And STDOUT should match /Installing package wp-media\/wp-rocket-cli \([^)]+\)/
796797
# This path is sometimes changed on Macs to prefix with /private
797798
And STDOUT should contain:
798799
"""
@@ -957,7 +958,7 @@ Feature: Install WP-CLI packages
957958
958959
Scenario: Install a package from a local zip
959960
Given an empty directory
960-
And I run `wget -q -O google-sitemap-generator-cli.zip https://github.com/wp-cli/google-sitemap-generator-cli/archive/master.zip`
961+
And I run `curl -fSsL -o google-sitemap-generator-cli.zip https://github.com/wp-cli/google-sitemap-generator-cli/archive/main.zip`
961962
962963
When I run `wp package install google-sitemap-generator-cli.zip`
963964
Then STDOUT should contain:
@@ -1093,7 +1094,7 @@ Feature: Install WP-CLI packages
10931094
Error: Couldn't download package from 'https://github.com/wp-cli/google-sitemap-generator.zip' (HTTP code 404).
10941095
"""
10951096
1096-
When I run `wp package install https://github.com/wp-cli/google-sitemap-generator-cli/archive/master.zip`
1097+
When I run `wp package install https://github.com/wp-cli/google-sitemap-generator-cli/archive/main.zip`
10971098
Then STDOUT should contain:
10981099
"""
10991100
Installing package wp-cli/google-sitemap-generator-cli (dev-
@@ -1201,9 +1202,9 @@ Feature: Install WP-CLI packages
12011202
"""
12021203
{PACKAGE_PATH}composer.json to require the package...
12031204
"""
1205+
And STDOUT should match /Registering .*?path-command as a path repository\.\.\./
12041206
And STDOUT should contain:
12051207
"""
1206-
Registering {CURRENT_PATH}/path-command as a path repository...
12071208
Using Composer to install the package...
12081209
"""
12091210
And STDOUT should contain:
@@ -1290,9 +1291,9 @@ Feature: Install WP-CLI packages
12901291
"""
12911292
{PACKAGE_PATH}composer.json to require the package...
12921293
"""
1294+
And STDOUT should match /Registering .*?path-command as a path repository\.\.\./
12931295
And STDOUT should contain:
12941296
"""
1295-
Registering {CURRENT_PATH}/path-command as a path repository...
12961297
Using Composer to install the package...
12971298
"""
12981299
And STDOUT should contain:
@@ -1374,11 +1375,11 @@ Feature: Install WP-CLI packages
13741375
"""
13751376
And STDOUT should be empty
13761377
1377-
When I try `wp package install https://example.com/non-existent-zip-asdfasdf.zip`
1378+
When I try `wp package install http://example.com/non-existent-zip-asdfasdf.zip`
13781379
Then the return code should be 1
13791380
And STDERR should contain:
13801381
"""
1381-
Error: Couldn't download package from 'https://example.com/non-existent-zip-asdfasdf.zip'
1382+
Error: Couldn't download package from 'http://example.com/non-existent-zip-asdfasdf.zip'
13821383
"""
13831384
And STDOUT should be empty
13841385

features/package-update.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Feature: Update WP-CLI packages
6262
| name | update |
6363
| wp-cli-test/updateable-package | available |
6464

65-
When I run `sed -i.bak s/v1.0.0/\>=1.0.0/g {PACKAGE_PATH}/composer.json`
65+
When I run `wp eval "file_put_contents( '{PACKAGE_PATH}composer.json', str_replace( 'v1.0.0', '>=1.0.0', file_get_contents( '{PACKAGE_PATH}composer.json' ) ) );" --skip-wordpress`
6666
Then the return code should be 0
6767

6868
When I run `cat {PACKAGE_PATH}/composer.json`
@@ -114,7 +114,7 @@ Feature: Update WP-CLI packages
114114
Success: Package installed.
115115
"""
116116

117-
When I run `sed -i.bak s/v1.0.0/\>=1.0.0/g {PACKAGE_PATH}/composer.json`
117+
When I run `wp eval "file_put_contents( '{PACKAGE_PATH}composer.json', str_replace( 'v1.0.0', '>=1.0.0', file_get_contents( '{PACKAGE_PATH}composer.json' ) ) );" --skip-wordpress`
118118
Then the return code should be 0
119119

120120
When I run `wp package update wp-cli-test/updateable-package`
@@ -147,7 +147,7 @@ Feature: Update WP-CLI packages
147147
Success: Package installed.
148148
"""
149149

150-
When I run `sed -i.bak s/v1.0.0/\>=1.0.0/g {PACKAGE_PATH}/composer.json`
150+
When I run `wp eval "file_put_contents( '{PACKAGE_PATH}composer.json', str_replace( 'v1.0.0', '>=1.0.0', file_get_contents( '{PACKAGE_PATH}composer.json' ) ) );" --skip-wordpress`
151151
Then the return code should be 0
152152

153153
When I run `wp package update wp-cli-test/updateable-package danielbachhuber/wp-cli-reset-post-date-command`

src/Package_Command.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ public function install( $args, $assoc_args ) {
370370
$this->register_revert_shutdown_function( $json_path, $composer_backup, $revert );
371371

372372
// Add the 'require' to composer.json
373-
WP_CLI::log( sprintf( 'Updating %s to require the package...', $json_path ) );
373+
WP_CLI::log( sprintf( 'Updating %s to require the package...', Path::normalize( $json_path ) ) );
374374
$json_manipulator = new JsonManipulator( $composer_backup );
375375
$json_manipulator->addMainKey( 'name', 'wp-cli/wp-cli' );
376376
$json_manipulator->addMainKey( 'version', self::get_wp_cli_version_composer() );
@@ -538,12 +538,12 @@ public function list_( $args, $assoc_args ) {
538538
public function path( $args ) {
539539
$packages_dir = WP_CLI::get_runner()->get_packages_dir_path();
540540
if ( ! empty( $args ) ) {
541-
$packages_dir .= 'vendor/' . $args[0];
541+
$packages_dir .= 'vendor' . DIRECTORY_SEPARATOR . $args[0];
542542
if ( ! is_dir( $packages_dir ) ) {
543543
WP_CLI::error( 'Invalid package name.' );
544544
}
545545
}
546-
WP_CLI::line( $packages_dir );
546+
WP_CLI::line( Path::normalize( $packages_dir ) );
547547
}
548548

549549
/**
@@ -847,12 +847,12 @@ public function uninstall( $args, $assoc_args ) {
847847
$this->register_revert_shutdown_function( $json_path, $composer_backup, $revert );
848848

849849
// Remove the 'require' from composer.json.
850-
WP_CLI::log( sprintf( 'Removing require statement for package \'%s\' from %s', $package_name, $json_path ) );
850+
WP_CLI::log( sprintf( 'Removing require statement for package \'%s\' from %s', $package_name, Path::normalize( $json_path ) ) );
851851
$manipulator = new JsonManipulator( $composer_backup );
852852
$manipulator->removeSubNode( 'require', $package_name, true /*caseInsensitive*/ );
853853

854854
// Remove the 'repository' details from composer.json.
855-
WP_CLI::log( sprintf( 'Removing repository details from %s', $json_path ) );
855+
WP_CLI::log( sprintf( 'Removing repository details from %s', Path::normalize( $json_path ) ) );
856856
$manipulator->removeSubNode( 'repositories', $package_name, true /*caseInsensitive*/ );
857857

858858
file_put_contents( $json_path, $manipulator->getContents() );
@@ -1170,7 +1170,7 @@ private function get_installed_package_by_name( $package_name ) {
11701170
* @return array Two-element array containing package name and version.
11711171
*/
11721172
private static function get_package_name_and_version_from_dir_package( $dir_package ) {
1173-
$composer_file = $dir_package . '/composer.json';
1173+
$composer_file = Path::normalize( $dir_package . '/composer.json' );
11741174
if ( ! file_exists( $composer_file ) ) {
11751175
WP_CLI::error( sprintf( "Invalid package: composer.json file '%s' not found.", $composer_file ) );
11761176
}
@@ -1204,21 +1204,12 @@ private function get_composer_json_path() {
12041204

12051205
if ( null === $composer_path || getenv( 'WP_CLI_TEST_PACKAGE_GET_COMPOSER_JSON_PATH' ) ) {
12061206

1207-
if ( getenv( 'WP_CLI_PACKAGES_DIR' ) ) {
1208-
$composer_path = Path::trailingslashit( getenv( 'WP_CLI_PACKAGES_DIR' ) ) . 'composer.json';
1209-
} else {
1210-
$composer_path = Path::trailingslashit( Path::get_home_dir() ) . '.wp-cli/packages/composer.json';
1211-
}
1207+
$packages_dir = WP_CLI::get_runner()->get_packages_dir_path();
1208+
$composer_path = rtrim( $packages_dir, '/\\' ) . DIRECTORY_SEPARATOR . 'composer.json';
12121209

12131210
// `composer.json` and its directory might need to be created
12141211
if ( ! file_exists( $composer_path ) ) {
12151212
$composer_path = $this->create_default_composer_json( $composer_path );
1216-
} else {
1217-
$composer_path = realpath( $composer_path );
1218-
if ( false === $composer_path ) {
1219-
$error = error_get_last();
1220-
WP_CLI::error( sprintf( "Composer path '%s' for packages/composer.json not found: %s", $composer_path, $error['message'] ) );
1221-
}
12221213
}
12231214
}
12241215

@@ -1250,11 +1241,7 @@ private function create_default_composer_json( $composer_path ) {
12501241
}
12511242
}
12521243

1253-
$composer_dir = realpath( $composer_dir );
1254-
if ( false === $composer_dir ) {
1255-
$error = error_get_last();
1256-
WP_CLI::error( sprintf( "Composer directory '%s' for packages not found: %s", $composer_dir, $error['message'] ) );
1257-
}
1244+
// No realpath() here to preserve short names on Windows if applicable.
12581245

12591246
$composer_path = Path::trailingslashit( $composer_dir ) . Path::basename( $composer_path );
12601247

@@ -1358,6 +1345,10 @@ private function maybe_add_git_suffix( $package_name ) {
13581345
if ( preg_match( '#\.git(?::[^:]+)?$#i', $package_name ) ) {
13591346
return $package_name;
13601347
}
1348+
// Skip if it looks like a zip file.
1349+
if ( preg_match( '#\.zip$#i', $package_name ) ) {
1350+
return $package_name;
1351+
}
13611352
// Append .git for GitHub/GitLab HTTPS or SSH URLs, preserving any :version suffix.
13621353
// Pattern: (https?://github.com/<user>/<repo>[...subgroups...] or git@github.com:<user>/<repo>[...subgroups...])(:version)?
13631354
if ( preg_match( '#^((?:https?://(?:github|gitlab)\.com/|git@(?:github|gitlab)\.com:)[^/:]+(?:/[^/:]+)*)(:.*)?$#i', $package_name, $matches ) ) {

tests/phpunit/ComposerJsonTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ public function set_up() {
3737
}
3838
$class_wp_cli_capture_exit->setValue( null, true );
3939

40-
$this->temp_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-package-composer-json-', true ) . '/';
40+
$temp_dir = Utils\get_temp_dir();
41+
if ( Utils\is_windows() ) {
42+
$temp_dir = realpath( $temp_dir ) ?: $temp_dir;
43+
$temp_dir = str_replace( '\\', '/', $temp_dir );
44+
}
45+
$this->temp_dir = rtrim( $temp_dir, '/' ) . '/' . uniqid( 'wp-cli-test-package-composer-json-', true ) . '/';
4146
mkdir( $this->temp_dir );
4247
}
4348

@@ -80,7 +85,7 @@ public function test_create_default_composer_json() {
8085
// Succeed.
8186
$expected = $this->temp_dir . 'packages/composer.json';
8287
$actual = $create_default_composer_json->invoke( $package, $expected );
83-
$this->assertSame( $expected, $this->mac_safe_path( $actual ) );
88+
$this->assertSame( $this->mac_safe_path( realpath( $expected ) ?: $expected ), $this->mac_safe_path( realpath( $actual ) ?: $actual ) );
8489
$this->assertTrue( false !== strpos( file_get_contents( $actual ), 'wp-cli/wp-cli' ) );
8590
unlink( $actual );
8691
rmdir( dirname( $actual ) );
@@ -105,7 +110,7 @@ public function test_get_composer_json_path() {
105110
putenv( 'WP_CLI_PACKAGES_DIR' );
106111
$expected = $this->temp_dir . '.wp-cli/packages/composer.json';
107112
$actual = $get_composer_json_path->invoke( $package );
108-
$this->assertSame( $expected, $this->mac_safe_path( $actual ) );
113+
$this->assertSame( $this->mac_safe_path( realpath( $expected ) ?: $expected ), $this->mac_safe_path( realpath( $actual ) ?: $actual ) );
109114
$this->assertTrue( false !== strpos( file_get_contents( $actual ), 'wp-cli/wp-cli' ) );
110115
unlink( $actual );
111116
rmdir( dirname( $actual ) );
@@ -115,7 +120,7 @@ public function test_get_composer_json_path() {
115120
putenv( 'WP_CLI_PACKAGES_DIR=' . $this->temp_dir . 'packages' );
116121
$expected = $this->temp_dir . 'packages/composer.json';
117122
$actual = $get_composer_json_path->invoke( $package );
118-
$this->assertSame( $expected, $this->mac_safe_path( $actual ) );
123+
$this->assertSame( $this->mac_safe_path( realpath( $expected ) ?: $expected ), $this->mac_safe_path( realpath( $actual ) ?: $actual ) );
119124
$this->assertTrue( false !== strpos( file_get_contents( $actual ), 'wp-cli/wp-cli' ) );
120125
unlink( $actual );
121126
rmdir( dirname( $actual ) );
@@ -126,7 +131,7 @@ public function test_get_composer_json_path() {
126131
mkdir( $this->temp_dir . 'packages' );
127132
touch( $expected );
128133
$actual = $get_composer_json_path->invoke( $package );
129-
$this->assertSame( $expected, $this->mac_safe_path( $actual ) );
134+
$this->assertSame( $this->mac_safe_path( realpath( $expected ) ?: $expected ), $this->mac_safe_path( realpath( $actual ) ?: $actual ) );
130135
$this->assertSame( 0, filesize( $actual ) );
131136
unlink( $actual );
132137
rmdir( dirname( $actual ) );
@@ -170,7 +175,7 @@ public function test_get_composer_json_path_backup_decoded() {
170175
// Succeed with newly created.
171176
$expected = $this->temp_dir . 'packages/composer.json';
172177
list( $actual, $content, $decoded ) = $get_composer_json_path_backup_decoded->invoke( $package );
173-
$this->assertSame( $expected, $this->mac_safe_path( $actual ) );
178+
$this->assertSame( $this->mac_safe_path( realpath( $expected ) ?: $expected ), $this->mac_safe_path( realpath( $actual ) ?: $actual ) );
174179
$this->assertTrue( false !== strpos( file_get_contents( $actual ), 'wp-cli/wp-cli' ) );
175180
$this->assertSame( file_get_contents( $actual ), $content );
176181
$this->assertFalse( empty( $decoded ) );
@@ -182,7 +187,7 @@ public function test_get_composer_json_path_backup_decoded() {
182187
mkdir( $this->temp_dir . 'packages' );
183188
file_put_contents( $expected, '{}' );
184189
list( $actual, $content, $decoded ) = $get_composer_json_path_backup_decoded->invoke( $package );
185-
$this->assertSame( $expected, $this->mac_safe_path( $actual ) );
190+
$this->assertSame( $this->mac_safe_path( realpath( $expected ) ?: $expected ), $this->mac_safe_path( realpath( $actual ) ?: $actual ) );
186191
$this->assertSame( '{}', $content );
187192
$this->assertTrue( empty( $decoded ) );
188193
unlink( $expected );
@@ -193,6 +198,8 @@ public function test_get_composer_json_path_backup_decoded() {
193198
}
194199

195200
private function mac_safe_path( $path ) {
196-
return preg_replace( '#^/private/(var|tmp)/#i', '/$1/', $path );
201+
$path = \WP_CLI\Path::normalize( $path );
202+
$path = preg_replace( '#^/private/(var|tmp)/#i', '/$1/', $path );
203+
return $path;
197204
}
198205
}

tests/phpunit/JsonManipulatorTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testAddLink($json, $type, $package, $constraint, $expected)
2525
{
2626
$manipulator = new JsonManipulator($json);
2727
$this->assertTrue($manipulator->addLink($type, $package, $constraint));
28-
$this->assertEquals($expected, $manipulator->getContents());
28+
$this->assertJsonEquals($expected, $manipulator->getContents());
2929
}
3030

3131
public static function linkProvider()
@@ -1297,7 +1297,7 @@ public function testAddLinkAndSortPackages($json, $type, $package, $constraint,
12971297
{
12981298
$manipulator = new JsonManipulator($json);
12991299
$this->assertTrue($manipulator->addLink($type, $package, $constraint, $sortPackages));
1300-
$this->assertEquals($expected, $manipulator->getContents());
1300+
$this->assertJsonEquals($expected, $manipulator->getContents());
13011301
}
13021302

13031303
public static function providerAddLinkAndSortPackages()
@@ -1380,7 +1380,7 @@ public function testRemoveSubNode($json, $name, $expected, $expectedContent = nu
13801380

13811381
$this->assertEquals($expected, $manipulator->removeSubNode('repositories', $name));
13821382
if (null !== $expectedContent) {
1383-
$this->assertEquals($expectedContent, $manipulator->getContents());
1383+
$this->assertJsonEquals($expectedContent, $manipulator->getContents());
13841384
}
13851385
}
13861386

@@ -2167,10 +2167,11 @@ public function testAddMainKeyWithContentHavingDollarSignFollowedByDigit2()
21672167
$manipulator = new JsonManipulator('{}');
21682168

21692169
$this->assertTrue($manipulator->addMainKey('foo', '$1bar'));
2170-
$this->assertEquals('{
2170+
$expected = '{
21712171
"foo": "$1bar"
21722172
}
2173-
', $manipulator->getContents());
2173+
';
2174+
$this->assertEquals( preg_replace( '/\R/', "\n", $expected ), preg_replace( '/\R/', "\n", $manipulator->getContents() ) );
21742175
}
21752176

21762177
public function testUpdateMainKey()
@@ -2301,9 +2302,10 @@ public function testRemoveMainKey()
23012302

23022303
$this->assertTrue($manipulator->removeMainKey('require'));
23032304
$this->assertTrue($manipulator->removeMainKey('require-dev'));
2304-
$this->assertEquals('{
2305+
$expected = '{
23052306
}
2306-
', $manipulator->getContents());
2307+
';
2308+
$this->assertEquals( preg_replace( '/\R/', "\n", $expected ), preg_replace( '/\R/', "\n", $manipulator->getContents() ) );
23072309
}
23082310

23092311
public function testIndentDetection()
@@ -2576,4 +2578,7 @@ public static function providerRemoveSubNodeCaseInsensitive()
25762578
}
25772579
// WP_CLI: end caseInsensitive.
25782580

2581+
private function assertJsonEquals( $expected, $actual ) {
2582+
$this->assertEquals( preg_replace( '/\R/', "\n", $expected ), preg_replace( '/\R/', "\n", $actual ) );
2583+
}
25792584
}

0 commit comments

Comments
 (0)