Skip to content

Commit

Permalink
buildozer: correctly substitute custom string attrs (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
thumphries-stripe authored Apr 17, 2024
1 parent 09e2763 commit 2bc4b5e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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")`,
},
{
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

0 comments on commit 2bc4b5e

Please sign in to comment.