Skip to content

Commit 64db9e3

Browse files
Merge pull request #25 from wp-cli/23-invalid-directory
Error when invalid path provided; catch exceptions when unreadable
2 parents 98da69f + f9228f6 commit 64db9e3

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

features/find.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,12 @@ Feature: Find WordPress installs on the filesystem
152152
"""
153153
0
154154
"""
155+
156+
Scenario: Invalid path specified
157+
Given an empty directory
158+
159+
When I try `wp find foo`
160+
Then STDERR should be:
161+
"""
162+
Error: Invalid path specified.
163+
"""

src/Find_Command.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class Find_Command {
141141
public function __invoke( $args, $assoc_args ) {
142142
list( $path ) = $args;
143143
$this->base_path = realpath( $path );
144+
if ( ! $this->base_path ) {
145+
WP_CLI::error( 'Invalid path specified.' );
146+
}
144147
$this->skip_ignored_paths = Utils\get_flag_value( $assoc_args, 'skip-ignored-paths' );
145148
$this->max_depth = Utils\get_flag_value( $assoc_args, 'max_depth', false );
146149
$this->verbose = Utils\get_flag_value( $assoc_args, 'verbose' );
@@ -196,7 +199,12 @@ private function recurse_directory( $path ) {
196199
// Check all files and directories of this path to recurse
197200
// into subdirectories.
198201
$this->log( "Recusing into {$path}" );
199-
$iterator = new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS );
202+
try {
203+
$iterator = new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS );
204+
} catch( Exception $e ) {
205+
$this->log( "Exception thrown '{$e->getMessage()}'. Skipping recursion into {$path}" );
206+
return;
207+
}
200208
foreach( $iterator as $file_info ) {
201209
if ( $file_info->isDir() ) {
202210
$this->current_depth++;

0 commit comments

Comments
 (0)