Skip to content

Commit

Permalink
Improve error message on regex check (#131)
Browse files Browse the repository at this point in the history
Improve error message on regex check

Co-authored-by: tosite <n.teshima@pepabo.com>
  • Loading branch information
schlessera and tosite committed Dec 13, 2019
2 parents d53ae07 + 70e8255 commit f446ca1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
50 changes: 40 additions & 10 deletions features/search-replace.feature
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Search_Replace_Command.php
Expand Up @@ -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 );
}
}

Expand Down

0 comments on commit f446ca1

Please sign in to comment.