Skip to content

Commit

Permalink
Add support for substituting deps inside select statements (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmos authored Jun 6, 2024
1 parent 1429e15 commit 80f1f68
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
55 changes: 55 additions & 0 deletions buildozer/buildozer_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")'
Expand Down
2 changes: 1 addition & 1 deletion edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 80f1f68

Please sign in to comment.