From 80f1f6802857db4d74983ee7ffa0b83e2bbeeda4 Mon Sep 17 00:00:00 2001 From: Vladimir Moskva Date: Thu, 6 Jun 2024 16:03:50 +0200 Subject: [PATCH] Add support for substituting deps inside select statements (#1275) --- buildozer/buildozer_test.sh | 55 +++++++++++++++++++++++++++++++++++++ edit/edit.go | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) 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 {