Skip to content

Commit 38a1afe

Browse files
schlesseraclaude
andcommitted
Leave what scenario symlinks pointed to out of the installation cache
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ebf567c commit 38a1afe

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎features/testing.feature‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ Feature: Test that WP-CLI loads.
5353
Then the scenario-files/wp-config.php file should exist
5454
And the scenario-files/wp-cli.yml file should not exist
5555

56+
@skip-windows
57+
Scenario: Leave what a scenario linked to before installing out of the installation cache
58+
Given an empty directory
59+
And a linked-files/wp-cli.yml file:
60+
"""
61+
user: admin
62+
"""
63+
When I run `php -r "mkdir('scenario-links'); symlink(getcwd() . '/linked-files', 'scenario-links/linked');"`
64+
65+
Given a WP installation in 'scenario-links'
66+
Then the scenario-links/linked/wp-cli.yml file should exist
67+
68+
When I run `php -r "rename('scenario-links', 'first');"`
69+
70+
# Reuse the subdirectory to select the same installation cache.
71+
Given a WP installation in 'scenario-links'
72+
Then the scenario-links/wp-config.php file should exist
73+
And the scenario-links/linked directory should not exist
74+
5675
@require-sqlite
5776
Scenario: Restore independent SQLite databases from the installation cache
5877
Given a WP installation in 'cached'

‎src/Context/FeatureContext.php‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,8 +2249,11 @@ private static function log_run_times_after_scenario( $scope ): void {
22492249
/**
22502250
* List the files and directories within a directory, relative to it.
22512251
*
2252+
* Symbolic links are listed, but not followed.
2253+
*
22522254
* @param string $dir
2253-
* @return array<string> Relative paths, directories after their contents. Empty if the directory does not exist.
2255+
* @return array<string, bool> Whether each entry is a symbolic link, keyed by relative path, directories after
2256+
* their contents. Empty if the directory does not exist.
22542257
*/
22552258
private static function list_dir( $dir ): array {
22562259
if ( ! is_dir( $dir ) ) {
@@ -2267,7 +2270,7 @@ private static function list_dir( $dir ): array {
22672270
* @var \SplFileInfo $item
22682271
*/
22692272
foreach ( $iterator as $item ) {
2270-
$paths[] = substr( $item->getPathname(), strlen( $dir ) + 1 );
2273+
$paths[ substr( $item->getPathname(), strlen( $dir ) + 1 ) ] = $item->isLink();
22712274
}
22722275

22732276
return $paths;
@@ -2276,14 +2279,19 @@ private static function list_dir( $dir ): array {
22762279
/**
22772280
* Remove the given files from a directory, as well as the given directories once they are empty.
22782281
*
2279-
* @param string $dir
2280-
* @param array<string> $paths Relative paths, directories after their contents.
2282+
* A copy of what a symbolic link pointed to is removed with all its contents.
2283+
*
2284+
* @param string $dir
2285+
* @param array<string, bool> $paths Whether each entry was a symbolic link, keyed by relative path, directories
2286+
* after their contents.
22812287
*/
22822288
private static function remove_from_dir( $dir, array $paths ): void {
2283-
foreach ( $paths as $path ) {
2289+
foreach ( $paths as $path => $was_link ) {
22842290
$full_path = "{$dir}/{$path}";
22852291

2286-
if ( is_dir( $full_path ) && ! is_link( $full_path ) ) {
2292+
if ( $was_link && is_dir( $full_path ) && ! is_link( $full_path ) ) {
2293+
self::remove_dir( $full_path );
2294+
} elseif ( is_dir( $full_path ) && ! is_link( $full_path ) ) {
22872295
// Only succeeds for directories that are empty by now.
22882296
@rmdir( $full_path );
22892297
} elseif ( file_exists( $full_path ) || is_link( $full_path ) ) {

0 commit comments

Comments
 (0)