Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for substituting deps inside select statements #1275

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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