diff --git a/buildozer/buildozer_test.sh b/buildozer/buildozer_test.sh index 1aba85c92..ff4112bfb 100755 --- a/buildozer/buildozer_test.sh +++ b/buildozer/buildozer_test.sh @@ -809,6 +809,61 @@ function test_replace_in_all_attributes() { )' } +function test_substitute_dep() { + in='go_library( + name = "edit", + deps = [ + # Before-comment. + "//some:value", # Suffix comment. + "//some:value2", # Suffix comment. + "//buildifier:build", + ] +)' + run "$in" 'substitute deps //some:(.*) //new:${1}' '//pkg:edit' + assert_equals 'go_library( + name = "edit", + deps = [ + # Before-comment. + "//new:value", # Suffix comment. + "//new:value2", # Suffix comment. + "//buildifier:build", + ], +)' +} + +function test_substitute_dep_select() { + # Replace a dep inside a select statement + in='go_library( + name = "edit", + deps = [":dep"] + select({ + "//tools/some:condition": [ + "//some/other:value", + "//some/other:value2", + ], + "//tools/other:condition": [ + "//yet/another:value", + "//yet/another:value2", + ], + "//conditions:default": SOME_CONSTANT, + }), +)' + run "$in" 'substitute deps //some/other:(.*) :${1}' '//pkg:edit' + assert_equals 'go_library( + name = "edit", + deps = [":dep"] + select({ + "//tools/some:condition": [ + ":value", + ":value2", + ], + "//tools/other:condition": [ + "//yet/another:value", + "//yet/another:value2", + ], + "//conditions:default": SOME_CONSTANT, + }), +)' +} + function test_delete_rule_all() { in='cc_library(name = "all") cc_library(name = "b")' diff --git a/edit/edit.go b/edit/edit.go index 4a5b9ae06..b2d965320 100644 --- a/edit/edit.go +++ b/edit/edit.go @@ -754,7 +754,7 @@ func ListReplace(e build.Expr, old, value, pkg string) bool { // successful. func ListSubstitute(e build.Expr, oldRegexp *regexp.Regexp, newTemplate string) bool { substituted := false - for _, li := range AllLists(e) { + for _, li := range allListsIncludingSelects(e) { for k, elem := range li.List { str, ok := elem.(*build.StringExpr) if !ok {