Skip to content

Commit

Permalink
fix updateStmt makeslice panic (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcj committed Nov 14, 2022
1 parent 11593d9 commit 2d10029
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,11 @@ func updateStmt(oldStmt *[]bzl.Expr, inserts, deletes, stmts []*stmt) {
sort.Stable(byIndex(deletes))
sort.Stable(byIndex(inserts))
sort.Stable(byIndex(stmts))
newStmt := make([]bzl.Expr, 0, len(*oldStmt)-len(deletes)+len(inserts))
cap := len(*oldStmt) - len(deletes) + len(inserts)
if cap < 0 {
cap = 0
}
newStmt := make([]bzl.Expr, 0, cap)
var ii, di, si int
for i, stmt := range *oldStmt {
for ii < len(inserts) && inserts[ii].index == i {
Expand Down
18 changes: 18 additions & 0 deletions rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,24 @@ x_library(name = "bar")
}
}

func TestInsertDeleteSync(t *testing.T) {
old := []byte("")
f, err := LoadData(filepath.Join("old", "BUILD.bazel"), "", old)
if err != nil {
t.Fatal(err)
}

foo := NewRule("filegroup", "test")
foo.Insert(f)
foo.Delete()
f.Sync()
got := strings.TrimSpace(string(f.Format()))
want := ""
if got != want {
t.Errorf("got:\n%s\nwant:%s", got, want)
}
}

func TestSymbolsReturnsKeys(t *testing.T) {
f, err := LoadData(filepath.Join("load", "BUILD.bazel"), "", []byte(`load("a.bzl", "y", z = "a")`))
if err != nil {
Expand Down

0 comments on commit 2d10029

Please sign in to comment.