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

buildozer: correctly substitute custom string attrs #1246

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion edit/buildozer.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ func cmdSubstitute(opts *Options, env CmdEnvironment) (*build.File, error) {
continue
}
if newValue, ok := stringSubstitute(e.Value, oldRegexp, newTemplate); ok {
env.Rule.SetAttr(key, getAttrValueExpr(key, []string{newValue}, env))
env.Rule.SetAttr(key, getStringExpr(newValue, env.Pkg))
}
}
return env.File, nil
Expand Down
55 changes: 55 additions & 0 deletions edit/buildozer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,61 @@ func TestCmdSubstituteLoad(t *testing.T) {
}
}

func TestCmdSubstitute(t *testing.T) {
for i, tc := range []struct {
name string
args []string
buildFile string
expected string
}{
{
name: "empty_rule",
args: []string{"*", "^$", "x"},
buildFile: `cc_library()`,
expected: `cc_library()`,
},
{
name: "known_attr",
args: []string{"*", "^//(.*)$", "//foo/${1}"},
buildFile: `cc_library(deps = ["//bar/baz:quux"])`,
expected: `cc_library(deps = ["//foo/bar/baz:quux"])`,
},
{
name: "custom_attr",
args: []string{"*", "^//(.*)$", "//foo/${1}"},
buildFile: `cc_library(my_custom_attr = "//bar/baz:quux")`,
expected: `cc_library(my_custom_attr = "//foo/bar/baz:quux")`,
},
Comment on lines +436 to +441
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case covers the bug in question, the others are included just for fun.

I confirmed the test fails if my change is reverted.

{
name: "specific_rule",
args: []string{"deps", "^//(.*)$", "//foo/${1}"},
buildFile: `cc_library(deps = ["//bar"], fancy_deps = ["//bar/baz:quux"])`,
expected: `cc_library(
fancy_deps = ["//bar/baz:quux"],
deps = ["//foo/bar"],
)`,
},
} {
t.Run(tc.name, func(t *testing.T) {
bld, err := build.Parse("BUILD", []byte(tc.buildFile))
if err != nil {
t.Error(err)
return
}
env := CmdEnvironment{
File: bld,
Args: tc.args,
Rule: bld.RuleAt(1),
}
bld, _ = cmdSubstitute(NewOpts(), env)
got := strings.TrimSpace(string(build.Format(bld)))
if got != tc.expected {
t.Errorf("cmdSubstitute(%d):\ngot:\n%s\nexpected:\n%s", i, got, tc.expected)
}
})
}
}

func TestCmdDictAddSet_missingColon(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down