From 3f69162ea91cd079fdfec75b759ff51ff6f886ea Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 11:42:52 +0300 Subject: [PATCH 1/8] Check if file exists and bail if not --- src/Import_Command.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Import_Command.php b/src/Import_Command.php index 03a3178ad..5ea8fde45 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -71,8 +71,13 @@ public function __invoke( $args, $assoc_args ) { } } } + $args = $new_args; + if ( empty( $args ) ) { + WP_CLI::error( "Import file doesn't exists." ); + } + foreach ( $args as $file ) { if ( ! is_readable( $file ) ) { WP_CLI::warning( "Can't read '$file' file." ); From 2afab3c4a027bd0a88eebd1c50ad887fd7b81542 Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 11:50:46 +0300 Subject: [PATCH 2/8] Check if file exists --- src/Import_Command.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index 5ea8fde45..3bc9419e7 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -71,7 +71,6 @@ public function __invoke( $args, $assoc_args ) { } } } - $args = $new_args; if ( empty( $args ) ) { From 09ad30f0a71df7def4021190e54267c6a7b7b53c Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:21:38 +0300 Subject: [PATCH 3/8] Improved error handling and warning messages --- src/Import_Command.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index 3bc9419e7..c438a1583 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -65,22 +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 import files found in '$arg'." ); + } } else { - if ( file_exists( $arg ) ) { + if ( ! file_exists( $arg ) ) { + WP_CLI::warning( " '$arg' doesn't exist." ); + continue; + } + + if ( is_readable( $arg ) ) { $new_args[] = $arg; + continue; } + + WP_CLI::warning( "Can't read '$file' file." ); } } - $args = $new_args; - if ( empty( $args ) ) { - WP_CLI::error( "Import file doesn't exists." ); + 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 ); From 5a3ad74c445bbd905c74519b67b0f5224b25b8df Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:33:34 +0300 Subject: [PATCH 4/8] Fix typo --- src/Import_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index c438a1583..6bbd9ea69 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -71,7 +71,7 @@ public function __invoke( $args, $assoc_args ) { } } else { if ( ! file_exists( $arg ) ) { - WP_CLI::warning( " '$arg' doesn't exist." ); + WP_CLI::warning( "'$arg' doesn't exist." ); continue; } @@ -80,7 +80,7 @@ public function __invoke( $args, $assoc_args ) { continue; } - WP_CLI::warning( "Can't read '$file' file." ); + WP_CLI::warning( "Can't read '$arg' file." ); } } From 6c2d9e24e96bcb58cfae4fe1a1f503c62ec0f146 Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:55:48 +0300 Subject: [PATCH 5/8] Improve warning message --- src/Import_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index 6bbd9ea69..495591a20 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -67,7 +67,7 @@ public function __invoke( $args, $assoc_args ) { } if ( empty( $files ) ) { - WP_CLI::warning( "No import files found in '$arg'." ); + WP_CLI::warning( "No files found in the import dir '$arg'." ); } } else { if ( ! file_exists( $arg ) ) { From d1daf0a5736921799b0e689574755c4024a49ccf Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 2 Jun 2022 13:40:56 +0100 Subject: [PATCH 6/8] Update feature to cover non existing files/dirs. Update the feature file to cover the messaging and status code around missing files and empty source directories. --- features/import.feature | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/features/import.feature b/features/import.feature index 492970f76..27e78f157 100644 --- a/features/import.feature +++ b/features/import.feature @@ -306,3 +306,38 @@ Feature: Import content. """ (in file wordpress.000.xml) """ + + Scenario: Handling of not 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 not_existing_relative_file_path.xml --authors=skip` + Then STDERR should contain: + """ + Warning: + """ + Then the return code should be 1 + + When I try `wp import not_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 not_existing_relative_file_path.xml --authors=skip` + Then STDERR should contain: + """ + Warning: + """ + Then the return code should be 1 From bb49c6b82f204ae37eef0ff899034841ffe8c1ae Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 11 Sep 2022 12:31:14 -0700 Subject: [PATCH 7/8] Tweaks to language --- features/import.feature | 8 ++++---- src/Import_Command.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/features/import.feature b/features/import.feature index 27e78f157..a7039cb0e 100644 --- a/features/import.feature +++ b/features/import.feature @@ -307,21 +307,21 @@ Feature: Import content. (in file wordpress.000.xml) """ - Scenario: Handling of not existing files and directories + 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 not_existing_relative_file_path.xml --authors=skip` + 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 not_existing_relative_file_path.xml {EXPORT_FILE} --authors=skip` + When I try `wp import non_existing_relative_file_path.xml {EXPORT_FILE} --authors=skip` Then STDERR should contain: """ Warning: @@ -335,7 +335,7 @@ Feature: Import content. """ Then the return code should be 1 - When I try `wp import empty_test_directory not_existing_relative_file_path.xml --authors=skip` + When I try `wp import empty_test_directory non_existing_relative_file_path.xml --authors=skip` Then STDERR should contain: """ Warning: diff --git a/src/Import_Command.php b/src/Import_Command.php index 495591a20..9202df682 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -67,11 +67,11 @@ public function __invoke( $args, $assoc_args ) { } if ( empty( $files ) ) { - WP_CLI::warning( "No files found in the import dir '$arg'." ); + WP_CLI::warning( "No files found in the import directory '$arg'." ); } } else { if ( ! file_exists( $arg ) ) { - WP_CLI::warning( "'$arg' doesn't exist." ); + WP_CLI::warning( "File '$arg' doesn't exist." ); continue; } @@ -80,7 +80,7 @@ public function __invoke( $args, $assoc_args ) { continue; } - WP_CLI::warning( "Can't read '$arg' file." ); + WP_CLI::warning( "Cannot read file '$arg'." ); } } From c269d55c5ebc0e3a62edd7f0bd7b5c660cece54d Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 11 Sep 2022 12:33:16 -0700 Subject: [PATCH 8/8] Fix linting issues --- composer.json | 6 +++++- src/Import_Command.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) 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/src/Import_Command.php b/src/Import_Command.php index 9202df682..4ce1f2f2c 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -85,7 +85,7 @@ public function __invoke( $args, $assoc_args ) { } if ( empty( $new_args ) ) { - WP_CLI::error( "Import failed due to missing or unreadable file/s." ); + WP_CLI::error( 'Import failed due to missing or unreadable file/s.' ); } $args = $new_args;