Skip to content

Commit

Permalink
Do not crash when a broken symlink is encountered
Browse files Browse the repository at this point in the history
 "Broken symlink at {$relative_filepath}. Target missing at {$item->getLinkTarget()}."
  • Loading branch information
BrianHenryIE committed Apr 30, 2024
1 parent e226f7d commit ada3bd4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
20 changes: 20 additions & 0 deletions features/distignore.feature
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,23 @@ Feature: Generate a distribution archive of a project
| targz | tar.gz | tar -zxvf | foo |
| zip | zip | unzip | bar7 |
| targz | tar.gz | tar -zxvf | bar8 |

Scenario: Does not crash when a broken symlink is encountered
# @see https://github.com/wp-cli/dist-archive-command/issues/86
Given an empty directory
And an empty foo/target-directory directory
And a foo/.distignore file:
"""
"""

When I run `ln -s {RUN_DIR}/foo/target-directory {RUN_DIR}/foo/symlink`
Then STDERR should be empty

When I run `rm -rf {RUN_DIR}/foo/target-directory`
Then STDERR should be empty

When I try `wp dist-archive foo`
Then STDERR should contain:
"""
Error: Broken symlink at /symlink. Target missing at
"""
16 changes: 12 additions & 4 deletions src/Dist_Archive_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,18 @@ private function get_file_list( $source_dir_path, $excluded = false ) {
*/
foreach ( $iterator as $item ) {
$relative_filepath = str_replace( $source_dir_path, '', $item->getPathname() );
if ( $this->checker->isPathIgnored( $relative_filepath ) ) {
$excluded_files[] = $relative_filepath;
} else {
$included_files[] = $relative_filepath;
try {
if ( $this->checker->isPathIgnored( $relative_filepath ) ) {
$excluded_files[] = $relative_filepath;
} else {
$included_files[] = $relative_filepath;
}
} catch ( \Inmarelibero\GitIgnoreChecker\Exception\InvalidArgumentException $exception ) {
if ( $item->isLink() && ! file_exists( readlink( $item->getPathname() ) ) ) {
WP_CLI::error( "Broken symlink at {$relative_filepath}. Target missing at {$item->getLinkTarget()}." );
} else {
WP_CLI::error( $exception->getMessage() );
}
}
}

Expand Down

0 comments on commit ada3bd4

Please sign in to comment.