diff --git a/composer.json b/composer.json index 5b76a69bc..ac9a6bd8f 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,11 @@ }, "config": { "process-timeout": 7200, - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "johnpbloch/wordpress-core-installer": true + } }, "extra": { "branch-alias": { diff --git a/features/import.feature b/features/import.feature index 492970f76..a7039cb0e 100644 --- a/features/import.feature +++ b/features/import.feature @@ -306,3 +306,38 @@ Feature: Import content. """ (in file wordpress.000.xml) """ + + Scenario: Handling of non-existing files and directories + Given a WP install + And I run `wp plugin install --activate wordpress-importer` + And I run `wp export` + And save STDOUT 'Writing to file %s' as {EXPORT_FILE} + And an empty 'empty_test_directory' directory + + When I try `wp import non_existing_relative_file_path.xml --authors=skip` + Then STDERR should contain: + """ + Warning: + """ + Then the return code should be 1 + + When I try `wp import non_existing_relative_file_path.xml {EXPORT_FILE} --authors=skip` + Then STDERR should contain: + """ + Warning: + """ + Then the return code should be 0 + + When I try `wp import empty_test_directory --authors=skip` + Then STDERR should contain: + """ + Warning: + """ + Then the return code should be 1 + + When I try `wp import empty_test_directory non_existing_relative_file_path.xml --authors=skip` + Then STDERR should contain: + """ + Warning: + """ + Then the return code should be 1 diff --git a/src/Import_Command.php b/src/Import_Command.php index 03a3178ad..4ce1f2f2c 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -65,18 +65,32 @@ public function __invoke( $args, $assoc_args ) { if ( ! empty( $files ) ) { $new_args = array_merge( $new_args, $files ); } + + if ( empty( $files ) ) { + WP_CLI::warning( "No files found in the import directory '$arg'." ); + } } else { - if ( file_exists( $arg ) ) { + if ( ! file_exists( $arg ) ) { + WP_CLI::warning( "File '$arg' doesn't exist." ); + continue; + } + + if ( is_readable( $arg ) ) { $new_args[] = $arg; + continue; } + + WP_CLI::warning( "Cannot read file '$arg'." ); } } + + if ( empty( $new_args ) ) { + WP_CLI::error( 'Import failed due to missing or unreadable file/s.' ); + } + $args = $new_args; foreach ( $args as $file ) { - if ( ! is_readable( $file ) ) { - WP_CLI::warning( "Can't read '$file' file." ); - } $ret = $this->import_wxr( $file, $assoc_args );