Skip to content

Commit dc5b101

Browse files
Copilotswissspidy
andcommitted
Add validation for empty strings and mixed argument usage
- Validate that --old and --new flags contain non-empty strings - Prevent simultaneous use of flags and positional arguments - Add comprehensive test scenarios for edge cases - Test coverage for: only --old, only --new, mixed usage, empty strings, single hyphen strings Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 77fb3eb commit dc5b101

2 files changed

Lines changed: 96 additions & 10 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,3 +1434,81 @@ Feature: Do global search/replace
14341434
--new
14351435
"""
14361436
And the return code should be 1
1437+
1438+
@require-mysql
1439+
Scenario: Error when only --old flag provided without --new
1440+
Given a WP install
1441+
1442+
When I try `wp search-replace --old='test-value'`
1443+
Then STDERR should contain:
1444+
"""
1445+
Please provide the <new> argument
1446+
"""
1447+
And the return code should be 1
1448+
1449+
@require-mysql
1450+
Scenario: Error when only --new flag provided without --old
1451+
Given a WP install
1452+
1453+
When I try `wp search-replace --new='test-value'`
1454+
Then STDERR should contain:
1455+
"""
1456+
Please provide the <old> argument
1457+
"""
1458+
And the return code should be 1
1459+
1460+
@require-mysql
1461+
Scenario: Error when both flags and positional arguments provided
1462+
Given a WP install
1463+
1464+
When I try `wp search-replace --old='flag-old' 'positional-old' 'positional-new'`
1465+
Then STDERR should contain:
1466+
"""
1467+
Cannot use both positional arguments and --old/--new flags
1468+
"""
1469+
And the return code should be 1
1470+
1471+
@require-mysql
1472+
Scenario: Error when empty string provided via --old flag
1473+
Given a WP install
1474+
1475+
When I try `wp search-replace --old='' --new='replacement'`
1476+
Then STDERR should contain:
1477+
"""
1478+
Please provide the <old> argument
1479+
"""
1480+
And the return code should be 1
1481+
1482+
@require-mysql
1483+
Scenario: Error when empty string provided via --new flag
1484+
Given a WP install
1485+
1486+
When I try `wp search-replace --old='search' --new=''`
1487+
Then STDERR should contain:
1488+
"""
1489+
Please provide the <new> argument
1490+
"""
1491+
And the return code should be 1
1492+
1493+
@require-mysql
1494+
Scenario: Search/replace string starting with single hyphen works with positional args
1495+
Given a WP install
1496+
And I run `wp post create --post_title="Test Post" --post_content="This is -single-hyphen content" --porcelain`
1497+
Then save STDOUT as {POST_ID}
1498+
1499+
When I run `wp search-replace '-single-hyphen' '-replaced-hyphen'`
1500+
Then STDOUT should contain:
1501+
"""
1502+
wp_posts
1503+
"""
1504+
And the return code should be 0
1505+
1506+
When I run `wp post get {POST_ID} --field=post_content`
1507+
Then STDOUT should contain:
1508+
"""
1509+
-replaced-hyphen
1510+
"""
1511+
And STDOUT should not contain:
1512+
"""
1513+
-single-hyphen
1514+
"""

‎src/Search_Replace_Command.php‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,24 +275,32 @@ public function __invoke( $args, $assoc_args ) {
275275

276276
// Support --old and --new flags as an alternative to positional arguments.
277277
// This allows users to search/replace strings that start with '--'.
278-
$old = Utils\get_flag_value( $assoc_args, 'old' );
279-
$new = Utils\get_flag_value( $assoc_args, 'new' );
278+
$old_flag = Utils\get_flag_value( $assoc_args, 'old' );
279+
$new_flag = Utils\get_flag_value( $assoc_args, 'new' );
280+
281+
// Check if both flags and positional arguments are provided.
282+
$has_flags = null !== $old_flag || null !== $new_flag;
283+
$has_args = ! empty( $args );
284+
if ( $has_flags && $has_args ) {
285+
WP_CLI::error( 'Cannot use both positional arguments and --old/--new flags. Please use one method or the other.' );
286+
}
280287

281-
// Fall back to positional arguments if flags not provided.
282-
if ( null === $old ) {
288+
// Determine old and new values based on flags or positional arguments.
289+
if ( null !== $old_flag || null !== $new_flag ) {
290+
$old = $old_flag;
291+
$new = $new_flag;
292+
} else {
283293
$old = array_shift( $args );
284-
}
285-
if ( null === $new ) {
286294
$new = array_shift( $args );
287295
}
288296

289-
// Validate that both old and new values are provided.
290-
if ( null === $old || null === $new ) {
297+
// Validate that both old and new values are provided and not empty.
298+
if ( null === $old || null === $new || '' === $old || '' === $new ) {
291299
$missing = array();
292-
if ( null === $old ) {
300+
if ( null === $old || '' === $old ) {
293301
$missing[] = '<old>';
294302
}
295-
if ( null === $new ) {
303+
if ( null === $new || '' === $new ) {
296304
$missing[] = '<new>';
297305
}
298306
$error_msg = count( $missing ) === 2

0 commit comments

Comments
 (0)