Skip to content

Commit

Permalink
Merge pull request #5 from sagikazarmark/import-path-support
Browse files Browse the repository at this point in the history
Add support for go_library import path
  • Loading branch information
brian-bice authored Sep 28, 2020
2 parents aca8655 + ffe9f93 commit 119aec9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
13 changes: 9 additions & 4 deletions domain/wollemi/service_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ func (this *Service) GoFormat(rewrite bool, paths []string) error {
target += ":" + name
}

internal[path] = "//" + target

if rule.Kind() == "go_copy" {
genfiles[path+".cp.go"] = target
importPath := rule.AttrString("import_path")
if importPath != "" {
external[importPath] = append(external[path], "//"+target)
} else {
internal[path] = "//" + target

if rule.Kind() == "go_copy" {
genfiles[path+".cp.go"] = target
}
}
case "go_get", "go_get_with_sources":
get := strings.TrimSuffix(rule.AttrString("get"), "/...")
Expand Down
61 changes: 61 additions & 0 deletions domain/wollemi/service_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,67 @@ func (t *ServiceSuite) TestService_GoFormat() {

require.NoError(t, wollemi.GoFormat(rewrite, []string{"app/server"}))
})

t.It("can resolve dependencies by import_path", func(t *T) {
data := t.GoFormatTestData()

cobra := "third_party/go/github.com/spf13/cobra/BUILD.plz"
data.Stat[cobra] = &FileInfo{
FileName: "BUILD.plz",
FileMode: os.FileMode(420),
}
data.Lstat[cobra] = data.Stat[cobra]

data.Walk = make([]string, 0, len(data.Walk))
for path, _ := range data.Lstat {
data.Walk = append(data.Walk, path)
}

sort.Strings(data.Walk)

data.Parse[cobra] = &please.BuildFile{
Path: cobra,
Stmt: []please.Expr{
please.NewCallExpr("go_library", []please.Expr{
please.NewAssignExpr("=", "name", "cobra"),
please.NewAssignExpr("=", "import_path", "github.com/spf13/cobra"),
}),
},
}

file := data.Parse["app/BUILD.plz"]
require.NotNil(t, file)

data.Stat[file.Path] = nil
data.Lstat[file.Path] = nil
data.Parse[file.Path] = nil
data.Write[file.Path] = file

app := file.GetRule("app")
require.NotNil(t, app)

test := file.GetRule("test")
require.NotNil(t, test)

app.SetAttr("visibility", please.NewListExpr("PUBLIC"))
app.SetAttr("deps", please.NewListExpr(
"//app/server",
"//third_party/go/github.com/spf13/cobra",
))

test.SetAttr("deps", please.NewListExpr(
"//app/server",
"//third_party/go/github.com/golang:mock",
"//third_party/go/github.com/spf13/cobra",
"//third_party/go/github.com/stretchr:testify",
))

t.MockGoFormat(data)

wollemi := t.New(gosrc, gopkg)

require.NoError(t, wollemi.GoFormat(rewrite, []string{"app"}))
})
}

func (t *ServiceSuite) MockGoFormat(td *GoFormatTestData) {
Expand Down

0 comments on commit 119aec9

Please sign in to comment.