Skip to content

Commit 71cff61

Browse files
Merge pull request #35 from wp-cli/issue_1
Add logging of search-replace transformations.
2 parents 8c38510 + 1e3d105 commit 71cff61

File tree

4 files changed

+625
-9
lines changed

4 files changed

+625
-9
lines changed

features/search-replace-export.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Feature: Search / replace with file export
157157
When I try `wp search-replace example.com example.net --export=foo/bar.sql`
158158
Then STDERR should contain:
159159
"""
160-
Error: Unable to open "foo/bar.sql" for writing:
160+
Error: Unable to open export file "foo/bar.sql" for writing:
161161
"""
162162

163163
Scenario: Search / replace specific table

features/search-replace.feature

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,316 @@ Feature: Do global search/replace
618618
Success: Made 1 replacement.
619619
"""
620620
And STDERR should be empty
621+
622+
Scenario: Logging with simple replace
623+
Given a WP install
624+
625+
When I run `wp post create --post_title='Title_baz__baz_' --post_content='Content_baz_12345678901234567890_baz_12345678901234567890' --porcelain`
626+
Then save STDOUT as {POST_ID}
627+
628+
When I run `wp search-replace '_baz_' '_' --dry-run --log --before_context=10 --after_context=10`
629+
Then STDOUT should contain:
630+
"""
631+
Success: 2 replacements to be made.
632+
"""
633+
And STDOUT should end with a table containing rows:
634+
| Table | Column | Replacements | Type |
635+
| wp_posts | post_content | 1 | SQL |
636+
| wp_posts | post_title | 1 | SQL |
637+
638+
And STDOUT should contain:
639+
"""
640+
wp_posts.post_content:{POST_ID}
641+
< Content_baz_1234567890 [...] 1234567890_baz_1234567890
642+
> Content_1234567890 [...] 1234567890_1234567890
643+
"""
644+
And STDOUT should contain:
645+
"""
646+
wp_posts.post_title:{POST_ID}
647+
< Title_baz__baz_
648+
> Title__
649+
"""
650+
And STDERR should be empty
651+
652+
When I run `wp search-replace '_baz_' '' --dry-run --log=replace.log`
653+
Then STDOUT should contain:
654+
"""
655+
Success: 2 replacements to be made.
656+
"""
657+
And STDOUT should not contain:
658+
"""
659+
< Content
660+
"""
661+
And the replace.log file should contain:
662+
"""
663+
wp_posts.post_content:{POST_ID}
664+
< Content_baz_12345678901234567890_baz_12345678901234567890
665+
> Content1234567890123456789012345678901234567890
666+
"""
667+
And the replace.log file should contain:
668+
"""
669+
wp_posts.post_title:{POST_ID}
670+
< Title_baz__baz_
671+
> Title
672+
"""
673+
And STDERR should be empty
674+
675+
# kana with diacritic and decomposed "a" + umlaut.
676+
When I run `wp search-replace '_baz_' '_バäz_' --log=- --before_context=10 --after_context=20`
677+
Then STDOUT should contain:
678+
"""
679+
Success: Made 2 replacements.
680+
"""
681+
And STDOUT should contain:
682+
"""
683+
wp_posts.post_content:{POST_ID}
684+
< Content_baz_12345678901234567890 [...] 1234567890_baz_12345678901234567890
685+
> Content_バäz_12345678901234567890 [...] 1234567890_バäz_12345678901234567890
686+
"""
687+
And STDERR should be empty
688+
689+
# Testing UTF-8 context
690+
When I run `wp search-replace 'z_' 'zzzz_' --log --before_context=2 --after_context=1`
691+
Then STDOUT should contain:
692+
"""
693+
Success: Made 2 replacements.
694+
"""
695+
And STDOUT should contain:
696+
"""
697+
wp_posts.post_content:{POST_ID}
698+
< バäz_1 [...] バäz_1
699+
> バäzzzz_1 [...] バäzzzz_1
700+
"""
701+
And STDERR should be empty
702+
703+
When I run `wp option set foobar '_bar1_ _bar1_12345678901234567890123456789012345678901234567890_bar1_ _bar1_1234567890123456789012345678901234567890'`
704+
705+
When I run `wp search-replace '_bar1_' '_baz1_' wp_options --log`
706+
Then STDOUT should contain:
707+
"""
708+
< _bar1_ _bar1_1234567890123456789012345678901234567890 [...] 1234567890123456789012345678901234567890_bar1_ _bar1_1234567890123456789012345678901234567890
709+
> _baz1_ _baz1_1234567890123456789012345678901234567890 [...] 1234567890123456789012345678901234567890_baz1_ _baz1_1234567890123456789012345678901234567890
710+
"""
711+
And STDERR should be empty
712+
713+
When I run `wp option get foobar`
714+
Then STDOUT should be:
715+
"""
716+
_baz1_ _baz1_12345678901234567890123456789012345678901234567890_baz1_ _baz1_1234567890123456789012345678901234567890
717+
"""
718+
719+
When I run `wp search-replace '_baz1_' '_bar1_' wp_options --log --before_context=10 --after_context=10`
720+
Then STDOUT should contain:
721+
"""
722+
< _baz1_ _baz1_1234567890 [...] 1234567890_baz1_ _baz1_1234567890
723+
> _bar1_ _bar1_1234567890 [...] 1234567890_bar1_ _bar1_1234567890
724+
"""
725+
And STDERR should be empty
726+
727+
When I run `wp option set foobar2 '12345678901234567890_bar2_1234567890_bar2_ _bar2_ _bar2_'`
728+
729+
When I run `wp search-replace '_bar2_' '_baz2baz2_' wp_options --log --before_context=10 --after_context=10`
730+
Then STDOUT should contain:
731+
"""
732+
< 1234567890_bar2_1234567890 [...] 1234567890_bar2_ _bar2_ _bar2_
733+
> 1234567890_baz2baz2_1234567890 [...] 1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
734+
"""
735+
And STDERR should be empty
736+
737+
When I run `wp option get foobar2`
738+
Then STDOUT should be:
739+
"""
740+
12345678901234567890_baz2baz2_1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
741+
"""
742+
743+
When I run `wp search-replace '_baz2baz2_' '_barz2_' wp_options --log --before_context=10 --after_context=4`
744+
Then STDOUT should contain:
745+
"""
746+
< 1234567890_baz2baz2_1234 [...] 1234567890_baz2baz2_ _baz2baz2_ _baz2baz2_
747+
> 1234567890_barz2_1234 [...] 1234567890_barz2_ _barz2_ _barz2_
748+
"""
749+
And STDERR should be empty
750+
751+
When I run `wp option set foobar3 '_bar3 _bar3 _bar3 _bar3'`
752+
753+
When I run `wp search-replace '_bar3' 'baz3' wp_options --log`
754+
Then STDOUT should contain:
755+
"""
756+
< _bar3 _bar3 _bar3 _bar3
757+
> baz3 baz3 baz3 baz3
758+
"""
759+
And STDERR should be empty
760+
761+
When I run `wp option get foobar3`
762+
Then STDOUT should be:
763+
"""
764+
baz3 baz3 baz3 baz3
765+
"""
766+
767+
When I run `wp search-replace 'baz3' 'baz\3' wp_options --dry-run --log`
768+
Then STDOUT should contain:
769+
"""
770+
< baz3 baz3 baz3 baz3
771+
> baz\3 baz\3 baz\3 baz\3
772+
"""
773+
And STDERR should be empty
774+
775+
Scenario: Logging with regex replace
776+
Given a WP install
777+
778+
When I run `wp post create --post_title='Title_baz__boz_' --post_content='Content_baz_1234567890_bez_1234567890_biz_1234567890_boz_1234567890_buz_' --porcelain`
779+
Then save STDOUT as {POST_ID}
780+
781+
When I run `wp search-replace '_b[aeiou]z_' '_bz_' --regex --dry-run --log --before_context=11 --after_context=11`
782+
Then STDOUT should contain:
783+
"""
784+
Success: 2 replacements to be made.
785+
"""
786+
And STDOUT should end with a table containing rows:
787+
| Table | Column | Replacements | Type |
788+
| wp_posts | post_content | 1 | PHP |
789+
| wp_posts | post_title | 1 | PHP |
790+
791+
And STDOUT should contain:
792+
"""
793+
wp_posts.post_content:{POST_ID}
794+
< Content_baz_1234567890_bez_1234567890_biz_1234567890_boz_1234567890_buz_
795+
> Content_bz_1234567890_bz_1234567890_bz_1234567890_bz_1234567890_bz_
796+
"""
797+
And STDOUT should contain:
798+
"""
799+
wp_posts.post_title:{POST_ID}
800+
< Title_baz__boz_
801+
> Title_bz__bz_
802+
"""
803+
And STDERR should be empty
804+
805+
When I run `wp search-replace '_b([aeiou])z_' '_$1b\\1z_\0' --regex --log --before_context=11 --after_context=11`
806+
Then STDOUT should contain:
807+
"""
808+
Success: Made 2 replacements.
809+
"""
810+
811+
And STDOUT should contain:
812+
"""
813+
wp_posts.post_content:{POST_ID}
814+
< Content_baz_1234567890_bez_1234567890_biz_1234567890_boz_1234567890_buz_
815+
> Content_ab\1z__baz_1234567890_eb\1z__bez_1234567890_ib\1z__biz_1234567890_ob\1z__boz_1234567890_ub\1z__buz_
816+
"""
817+
And STDOUT should contain:
818+
"""
819+
wp_posts.post_title:{POST_ID}
820+
< Title_baz__boz_
821+
> Title_ab\1z__baz__ob\1z__boz_
822+
"""
823+
And STDERR should be empty
824+
825+
When I run `wp post get {POST_ID} --field=title`
826+
Then STDOUT should be:
827+
"""
828+
Title_ab\1z__baz__ob\1z__boz_
829+
"""
830+
831+
When I run `wp post get {POST_ID} --field=content`
832+
Then STDOUT should be:
833+
"""
834+
Content_ab\1z__baz_1234567890_eb\1z__bez_1234567890_ib\1z__biz_1234567890_ob\1z__boz_1234567890_ub\1z__buz_
835+
"""
836+
837+
Scenario: Logging with prefixes and custom colors
838+
Given a WP install
839+
840+
When I run `WP_CLI_SEARCH_REPLACE_LOG_PREFIXES='- ,+ ' wp search-replace Just Yet --dry-run --log`
841+
Then STDOUT should contain:
842+
"""
843+
- Just another WordPress site
844+
+ Yet another WordPress site
845+
"""
846+
And STDERR should be empty
847+
848+
When I run `WP_CLI_SEARCH_REPLACE_LOG_PREFIXES=',' wp search-replace Just Yet --dry-run --log`
849+
Then STDOUT should not contain:
850+
"""
851+
< Just
852+
"""
853+
And STDOUT should contain:
854+
"""
855+
Just
856+
"""
857+
And STDOUT should not contain:
858+
"""
859+
> Yet
860+
"""
861+
And STDOUT should contain:
862+
"""
863+
Yet
864+
"""
865+
And STDERR should be empty
866+
867+
When I run `SHELL_PIPE=0 wp search-replace WordPress WP --dry-run --log`
868+
Then STDOUT should contain:
869+
"""
870+
wp_options.option_value:
871+
"""
872+
And STDOUT should contain:
873+
"""
874+
< Just another WordPress site
875+
> Just another WP site
876+
"""
877+
And STDERR should be empty
878+
879+
When I run `SHELL_PIPE=0 WP_CLI_SEARCH_REPLACE_LOG_COLORS='%b,%r,%g' wp search-replace WordPress WP --dry-run --log`
880+
Then STDOUT should contain:
881+
"""
882+
wp_options.option_value:
883+
"""
884+
And STDOUT should contain:
885+
"""
886+
< Just another WordPress site
887+
> Just another WP site
888+
"""
889+
And STDERR should be empty
890+
891+
When I run `SHELL_PIPE=0 WP_CLI_SEARCH_REPLACE_LOG_COLORS='%b,%r,%g' wp search-replace WordPress WP --dry-run --log=replace.log`
892+
Then STDOUT should not contain:
893+
"""
894+
wp_options.option_value
895+
"""
896+
And the replace.log file should contain:
897+
"""
898+
wp_options.option_value:
899+
"""
900+
And the replace.log file should contain:
901+
"""
902+
< Just another WordPress site
903+
> Just another WP site
904+
"""
905+
And STDERR should be empty
906+
907+
When I run `SHELL_PIPE=0 wp search-replace WordPress WP --dry-run --log=replace.log`
908+
Then STDOUT should not contain:
909+
"""
910+
wp_options.option_value
911+
"""
912+
And the replace.log file should contain:
913+
"""
914+
wp_options.option_value:
915+
"""
916+
And the replace.log file should contain:
917+
"""
918+
< Just another WordPress site
919+
> Just another WP site
920+
"""
921+
And STDERR should be empty
922+
923+
When I run `SHELL_PIPE=0 WP_CLI_SEARCH_REPLACE_LOG_COLORS=',,' wp search-replace WordPress WP --dry-run --log`
924+
Then STDOUT should contain:
925+
"""
926+
wp_options.option_value:
927+
"""
928+
And STDOUT should contain:
929+
"""
930+
< Just another WordPress site
931+
> Just another WP site
932+
"""
933+
And STDERR should be empty

0 commit comments

Comments
 (0)