Skip to content

Commit 05007cb

Browse files
authored
Merge pull request #411 from wp-cli/fix/install-cache-scenario-files
Leave files of the scenario out of the installation cache
2 parents b533a56 + c11d155 commit 05007cb

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎features/testing.feature‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@ 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+
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+
4075
@require-sqlite
4176
Scenario: Restore independent SQLite databases from the installation cache
4277
Given a WP installation in 'cached'

‎src/Context/FeatureContext.php‎

Lines changed: 63 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,61 @@ 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+
* Symbolic links are listed, but not followed.
2253+
*
2254+
* @param string $dir
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.
2257+
*/
2258+
private static function list_dir( $dir ): array {
2259+
$dir = rtrim( $dir, '/\\' );
2260+
if ( ! is_dir( $dir ) ) {
2261+
return [];
2262+
}
2263+
2264+
$paths = [];
2265+
$iterator = new \RecursiveIteratorIterator(
2266+
new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ),
2267+
\RecursiveIteratorIterator::CHILD_FIRST
2268+
);
2269+
2270+
/**
2271+
* @var \SplFileInfo $item
2272+
*/
2273+
foreach ( $iterator as $item ) {
2274+
$paths[ substr( $item->getPathname(), strlen( $dir ) + 1 ) ] = $item->isLink();
2275+
}
2276+
2277+
return $paths;
2278+
}
2279+
2280+
/**
2281+
* Remove the given files from a directory, as well as the given directories once they are empty.
2282+
*
2283+
* A copy of what a symbolic link pointed to is removed with all its contents.
2284+
*
2285+
* @param string $dir
2286+
* @param array<string, bool> $paths Whether each entry was a symbolic link, keyed by relative path, directories
2287+
* after their contents.
2288+
*/
2289+
private static function remove_from_dir( $dir, array $paths ): void {
2290+
foreach ( $paths as $path => $was_link ) {
2291+
$full_path = "{$dir}/{$path}";
2292+
2293+
if ( $was_link && is_dir( $full_path ) && ! is_link( $full_path ) ) {
2294+
self::remove_dir( $full_path );
2295+
} elseif ( is_dir( $full_path ) && ! is_link( $full_path ) ) {
2296+
// Only succeeds for directories that are empty by now.
2297+
@rmdir( $full_path );
2298+
} elseif ( file_exists( $full_path ) || is_link( $full_path ) ) {
2299+
unlink( $full_path );
2300+
}
2301+
}
2302+
}
2303+
22422304
/**
22432305
* Copy files in updated directory that are not in source directory to copy directory. ("Incremental backup".)
22442306
* Note: does not deal with changed files (ie does not compare file contents for changes), for speed reasons.

0 commit comments

Comments
 (0)