Skip to content

Commit ebf567c

Browse files
schlesseraclaude
andcommitted
Leave files of the scenario out of the installation cache
The installation cache holds everything a WordPress installation adds to the core files, and is created by whichever scenario installs WordPress first. Files that scenario created before installing, such as a wp-cli.yml, also ended up in the cache, and from there in every later scenario set up from it. Which scenario that is depends on the order in which they run, and differs between parallel workers. Leave out what was in the run directory before the installation started. Creating the subdirectory of an installation also no longer fails when the scenario created files in it already. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b533a56 commit ebf567c

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎features/testing.feature‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ Feature: Test that WP-CLI loads.
3737
sqlite
3838
"""
3939

40+
Scenario: Leave files a scenario created before installing out of the installation cache
41+
Given an empty directory
42+
And a scenario-files/wp-cli.yml file:
43+
"""
44+
user: admin
45+
"""
46+
And a WP installation in 'scenario-files'
47+
Then the scenario-files/wp-cli.yml file should exist
48+
49+
When I run `php -r "rename('scenario-files', 'first');"`
50+
51+
# Reuse the subdirectory to select the same installation cache.
52+
Given a WP installation in 'scenario-files'
53+
Then the scenario-files/wp-config.php file should exist
54+
And the scenario-files/wp-cli.yml file should not exist
55+
4056
@require-sqlite
4157
Scenario: Restore independent SQLite databases from the installation cache
4258
Given a WP installation in 'cached'

‎src/Context/FeatureContext.php‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1843,7 +1843,8 @@ public function download_wp( $subdir = '', $version = '' ): void {
18431843

18441844
$dest_dir = rtrim( $this->variables['RUN_DIR'], '/\\' ) . ( $subdir ? DIRECTORY_SEPARATOR . $subdir : '' );
18451845

1846-
if ( $subdir ) {
1846+
// The scenario may have created files in the subdirectory already.
1847+
if ( $subdir && ! is_dir( $dest_dir ) ) {
18471848
mkdir( $dest_dir, 0777, true /*recursive*/ );
18481849
}
18491850

@@ -1937,6 +1938,11 @@ public function install_wp( $subdir = '', $version = '' ): void {
19371938
$this->create_db();
19381939
}
19391940
$this->create_run_dir();
1941+
1942+
// Files the scenario created before installing belong to that scenario only, and must not
1943+
// end up in the installation cache that later scenarios are set up from.
1944+
$scenario_files = self::list_dir( '' !== $subdir ? ( $this->variables['RUN_DIR'] . "/$subdir" ) : $this->variables['RUN_DIR'] );
1945+
19401946
$this->download_wp( $subdir, $version );
19411947
$this->create_config( $subdir, $config_extra_php );
19421948

@@ -1969,6 +1975,7 @@ public function install_wp( $subdir = '', $version = '' ): void {
19691975
}
19701976

19711977
self::dir_diff_copy( $run_dir, self::$cache_dir, $install_cache_path );
1978+
self::remove_from_dir( $install_cache_path, $scenario_files );
19721979

19731980
if ( 'sqlite' !== self::$db_type ) {
19741981
$mysqldump_binary = Utils\get_sql_dump_command();
@@ -2239,6 +2246,52 @@ private static function log_run_times_after_scenario( $scope ): void {
22392246
}
22402247
}
22412248

2249+
/**
2250+
* List the files and directories within a directory, relative to it.
2251+
*
2252+
* @param string $dir
2253+
* @return array<string> Relative paths, directories after their contents. Empty if the directory does not exist.
2254+
*/
2255+
private static function list_dir( $dir ): array {
2256+
if ( ! is_dir( $dir ) ) {
2257+
return [];
2258+
}
2259+
2260+
$paths = [];
2261+
$iterator = new \RecursiveIteratorIterator(
2262+
new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ),
2263+
\RecursiveIteratorIterator::CHILD_FIRST
2264+
);
2265+
2266+
/**
2267+
* @var \SplFileInfo $item
2268+
*/
2269+
foreach ( $iterator as $item ) {
2270+
$paths[] = substr( $item->getPathname(), strlen( $dir ) + 1 );
2271+
}
2272+
2273+
return $paths;
2274+
}
2275+
2276+
/**
2277+
* Remove the given files from a directory, as well as the given directories once they are empty.
2278+
*
2279+
* @param string $dir
2280+
* @param array<string> $paths Relative paths, directories after their contents.
2281+
*/
2282+
private static function remove_from_dir( $dir, array $paths ): void {
2283+
foreach ( $paths as $path ) {
2284+
$full_path = "{$dir}/{$path}";
2285+
2286+
if ( is_dir( $full_path ) && ! is_link( $full_path ) ) {
2287+
// Only succeeds for directories that are empty by now.
2288+
@rmdir( $full_path );
2289+
} elseif ( file_exists( $full_path ) || is_link( $full_path ) ) {
2290+
unlink( $full_path );
2291+
}
2292+
}
2293+
}
2294+
22422295
/**
22432296
* Copy files in updated directory that are not in source directory to copy directory. ("Incremental backup".)
22442297
* Note: does not deal with changed files (ie does not compare file contents for changes), for speed reasons.

0 commit comments

Comments
 (0)