diff --git a/features/search-replace.feature b/features/search-replace.feature index d354134b0..d2a1aaa82 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -195,13 +195,10 @@ Feature: Do global search/replace Scenario: Regex search/replace with a incorrect `--regex-flags` Given a WP install When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-flags='kppr'` - Then STDERR should contain: - """ - (Hello)\s(world) - """ - And STDERR should contain: + Then STDERR should be: """ - kppr + Error: The regex pattern '(Hello)\s(world)' with default delimiter 'chr(1)' and flags 'kppr' fails. + preg_match(): Unknown modifier 'k'. """ And the return code should be 1 @@ -403,35 +400,68 @@ Feature: Do global search/replace Then STDERR should be: """ Error: The regex '1HTTP://EXAMPLE.COM1i' fails. + preg_match(): Delimiter must not be alphanumeric or backslash. """ And the return code should be 1 When I try `wp search-replace 'regex error)' '' --regex` - Then STDERR should be: + Then STDERR should contain: """ Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and no flags fails. """ + And STDERR should contain: + """ + preg_match(): Compilation failed: + """ + And STDERR should contain: + """ + at offset 11 + """ And the return code should be 1 When I try `wp search-replace 'regex error)' '' --regex --regex-flags=u` - Then STDERR should be: + Then STDERR should contain: """ Error: The regex pattern 'regex error)' with default delimiter 'chr(1)' and flags 'u' fails. """ + And STDERR should contain: + """ + preg_match(): Compilation failed: + """ + And STDERR should contain: + """ + at offset 11 + """ And the return code should be 1 When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/` - Then STDERR should be: + Then STDERR should contain: """ Error: The regex '/regex error)/' fails. """ + And STDERR should contain: + """ + preg_match(): Compilation failed: + """ + And STDERR should contain: + """ + at offset 11 + """ And the return code should be 1 When I try `wp search-replace 'regex error)' '' --regex --regex-delimiter=/ --regex-flags=u` - Then STDERR should be: + Then STDERR should contain: """ Error: The regex '/regex error)/u' fails. """ + And STDERR should contain: + """ + preg_match(): Compilation failed: + """ + And STDERR should contain: + """ + at offset 11 + """ And the return code should be 1 Scenario: Formatting as count-only diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index 281748c59..82cf14176 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -203,13 +203,15 @@ public function __invoke( $args, $assoc_args ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Preventing a warning when testing the regex. if ( false === @preg_match( $search_regex, '' ) ) { + $error = error_get_last(); + $preg_error_message = ( ! empty( $error ) && array_key_exists( 'message', $error ) ) ? "\n{$error['message']}." : ''; if ( $default_regex_delimiter ) { $flags_msg = $this->regex_flags ? "flags '$this->regex_flags'" : 'no flags'; $msg = "The regex pattern '$old' with default delimiter 'chr(1)' and {$flags_msg} fails."; } else { $msg = "The regex '$search_regex' fails."; } - WP_CLI::error( $msg ); + WP_CLI::error( $msg . $preg_error_message ); } }